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

  • Question

  • User1214317495 posted
    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;
    GV.DataBind();

    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.
    Thursday, January 24, 2019 6:56 PM

Answers

  • User-943250815 posted

    Just in case, is your GridView property AutoGenerateColumns="False", if yes set it to True. Also is your Gridview just basic one, I mean not columns defined?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 24, 2019 10:25 PM

All replies

  • User-2054057000 posted

    Try setting a breakpoint then move step by step with F10 key. This will help you to get the exact error message. Once you get the error message search in google or update your question with the error message you get, so that developers here can give you an accurate solution.

    Thursday, January 24, 2019 8:45 PM
  • User-943250815 posted

    Just in case, is your GridView property AutoGenerateColumns="False", if yes set it to True. Also is your Gridview just basic one, I mean not columns defined?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 24, 2019 10:25 PM
  • User1214317495 posted

    Just in case, is your GridView property AutoGenerateColumns="False", if yes set it to True. Also is your Gridview just basic one, I mean not columns defined?

    It is a basic grid but I had the AutoGenerateColumns to false. Once I changed that I could see the grid. 

    Why would you ever want the AutoGenerateColumns set to false if it keeps data from showing in the gridview?

    Thank you!

    Friday, January 25, 2019 1:26 AM
  • User-943250815 posted

    A Gridview can show not only data, but some more resources like, buttons, links, images, or even a specially formatted column that can be a result of a formula or formatted date other than as provided by datasource.
    For such scenarios, we define our columns using BindField or Template Field, these are just examples, there are more.

    But basically for a short run, just to get data & show, AutoGenerateColumns default is True, so just do not set it to false

    Friday, January 25, 2019 1:52 AM