0

I'm having some trouble here.

I got a DataTable from my database and I want to use it to be the datasource of my GridView.

The problem: when I set the datasource of this GridView with my DataTable, nothing happens. But if I set the datasource of another component (like ListItem), the data is shown.

What do I do with it? Follow the code below:

<!-- Here I have a ListView. When I set the datasource, the data is showed. -->
<asp:ListView ID="listviewCustomers" runat="server">
    <ItemTemplate>
        <asp:Label runat="server" ID="myLabel" text="<%# bind('Name') %>" />
    </ItemTemplate>
</asp:ListView>


<!-- Here I have a GridView. When I set the datasource, nothing happens. -->
<asp:GridView runat="server" ID="gridviewPassageiros">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
            <asp:Label runat="server" ID="myLabel" text="<%# bind('Name') %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
3
  • You mave have to paste your code behind or parts of it. Commented Jul 26, 2012 at 11:11
  • You are setting up Datasource but are you binding it? Commented Jul 26, 2012 at 11:13
  • Yes, the DataSource is setted and the "DataBind" method is called. Commented Jul 26, 2012 at 11:55

4 Answers 4

1

The most simple case could be a missing call to "DataBind" of "gridviewPassageiros". Another option is to add dummy-content.

Code-Behind

public void Page_OnLoad(object sender, EventArgs eventArgs)
{
  if (!IsPostback)
  {
    gridviewPassageiros.DataSource = GetPassageiros();
    gridviewPassageiros.DataBind();
  }
}

ASPX:

<asp:GridView runat="server" ID="gridviewPassageiros">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
            <h1>TEST</h1>
            <asp:Label runat="server" ID="myLabel" text="<%# bind('Name') %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODE FROM COMMENT:

DAOPassenger objDAOPassenger = new DAOPassenger(); 
gridviewPassageiros.DataSource = objDAOPassenger.GetAllPassangers(); 
gridviewPassageiros.DataBind(); 

have you checked, the result of GetAllPassangers?

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, mo. Thanks for your reply. Neither this way - putting the <h1> tags into template - the data is displayed. I've already called the DataBind method, and nothing happened. Any other idea? Thanks!
please show the portion of your code where you assign the "DataSource"-Property, this might help us to help you.
Here is the code where I set the "DataSource" property: DAOPassenger objDAOPassenger = new DAOPassenger(); gridviewPassageiros.DataSource = objDAOPassenger.GetAllPassangers(); gridviewPassageiros.DataBind(); And here I have the code for display my GridView: <asp:GridView runat="server" ID="gridviewPassangers" emptyDataText="None."> <Columns> <asp:TemplateField> <asp:Label runat="server" ID="labelPassangerName" text="<%# bind('Name') %>" /> </asp:TemplateField> </Columns> </asp:GridView> Any idea?
indent it by 4 chars or use the 'button' in the toolbar
in comments it works a bit different :) use ` to escapse code or (have a look at the help)[stackoverflow.com/editing-help#comment-formatting]
0

Try using

<asp:Label ID="myLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>

Also make sure you bind the data correctly

eg

 if (dt.Rows.Count>0) 
 {
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
 }  

1 Comment

Thanks for the reply, huMpty duMpty! Well, the data is correctly setted, but the gridview don't display! So strange, huh?
0

I see two Problem in your Code: try this

Text='<%# Bind("Name") %>'

insted of

text="<%# bind('Name') %>"

also i can't see the DataSourceID in your Code

Hope I Helped

1 Comment

Thanks for your reply, Ibrahem! Neither this way the gridview is displayed. The message from "EmptyDataText" property also ins't displayed. So strange. Any idea?
0

DataSourceID is missing .. that is no data source is provided to your grid.

2 Comments

you just need a DataSoureID if you use a DataSourceProvider.
Thanks for your reply, Amrita. I use the same DataTable to set the DataSource of a ListView, and the data is displayed, but in my GridView nothing happens. That's my big problem! Hahaha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.