Can't bind gridview to datatable in C# web form

  • Thread starter Thread starter WVSooner
  • Start date Start date
W

WVSooner

Guest
Hello all. I can't figure out how to bind a gridview to a datatable on a web form in Visual Studio. I am using SQL to grab data from a table and then want that info to show in a gridview, but cannot figure out how to do it. I should be simple but I can't get it. Here is the code i'm trying

SqlConnection conn = new SqlConnection("Connection String");
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = new DataTable();

string SQL = @"Select* from myTable";

conn.Open();
da = new SqlDataAdapter(SQL, conn);
ds = new DataSet();
da.Fill(ds, "tblCopy");
dt = ds.Tables["tblCopy"];
conn.Close();

In the the Designer I add the GridView by dragging a GridView onto the web form. Set the ID to GV and keep the DataSourceID property blank.

Then in code I add...

GV.DataSource = dt;


There is info in the table but not in the GridView.


I check it using...


if (dt.Rows.Count > 0)
{
infoLBL.Text = "dt has info.";
}
else
{
infoLBL.Text = "Unable to collect database.";
}


if (GV.Rows.Count > 0)
{
infoLBL.Text = "GV has info.";
}
else
{
infoLBL.Text = "Unable to populate grid.";
}

The table will have info but the datagrid never does.

Thanks anyone for any help.

Continue reading...
 
Back
Top