0

My gridview source code is following

<asp:GridView ID="g1" runat="server"></asp:GridView>

and my code behind is,

bindGridView(ds.Tables[0], g1, lblErrorActivityGrid, lblActivityGridCount);

private void bindGridView(DataTable dt, GridView grd, Label lblError, Label GrdRowCount)
    {
        grd.DataSource = dt;
        grd.DataBind();
    }

my question is why my gridview is not displaying data? I am using dataset and manually adding data in my dataset and then returning table at 0th table.

Please help. Thanks in advance

2
  • Did you check your table has data or not ? Commented Dec 23, 2013 at 6:20
  • yes my table has data Commented Dec 23, 2013 at 6:45

2 Answers 2

2

In markup set AutogenerateColumns="true" :

<asp:GridView ID="g1" AutoGenerateColumns= "true" runat="server"></asp:GridView>

Here's how I have tested your code:

In my markup I have the gridview.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="g1" AutoGenerateColumns= "true" runat="server"></asp:GridView>
        <br />
        <asp:Label ID="lblErrorActivityGrid" runat="server" Text="Error Activity Grid"></asp:Label>        
        <br />
        <asp:Label ID="lblActivityGridCount" runat="server" Text="Activity Grid Count"></asp:Label>
    </div>
    </form>
</body>
</html>

In my code, in page load I am adding a DataTable to a DataSet and calling bindGridView method:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Column1", typeof(string));
        dt.Columns.Add("Column2", typeof(string));
        ds.Tables.Add(dt);
        for (int i=0; i<5; i++)
        {
            DataRow nw = ds.Tables[0].NewRow();
            nw[0] = (i + 1).ToString();
            nw["Column1"] = (i + 1).ToString();
            nw["Column2"] = String.Format("Item {0}", i);
            ds.Tables[0].Rows.Add(nw);
        }
        bindGridView(ds.Tables[0], g1, lblErrorActivityGrid, lblActivityGridCount);
    }
}


private void bindGridView(DataTable dt, GridView grd, Label lblError, Label GrdRowCount)
{
    grd.DataSource = dt;
    grd.DataBind();
}

And here's the grid shows in my browser:

enter image description here

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

9 Comments

Set a breakpoint at grd.DataSource = dt; and see the data in dt. If it is empty, set a breakpoint atbindGridView(ds.Tables[0], g1, lblErrorActivityGrid, lblActivityGridCount); and see ds in dataset viewer. Check if this is the table you wnat to bind with gridview.
my datatable is not empty. It's full. It has many records. more than 50 records
Why do you say more than 50 records? In debug, in breakpoint at grd.DataSource = dt; it shows exact number of records. Did you check there? To make sure that you are accessing correct table from dataset.
Have you somewhere set the visibility of your gridview as false?..:)
well, what I am saying is that it's showing records. So it is not empty
|
0

Try to Set

grd.DataSource = dt.DefaultView;

you must set the View to the Gridview from the DataTable..

Make sure the Table has Data.

1 Comment

I have tried grd.DataSource=dt.DefaultView; It's still not working

Your Answer

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