locked
Filling dataGridView from DataSet table RRS feed

  • Question

  • Hi, 

    im trying to get data from dataset to a gridview , like this:

    string sql = "select * from lide";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet("lide");
                conn.Open();
                adapter.Fill(ds, "lide");
                conn.Close();
                dataGridView1.DataSource = ds.Tables["lide"];

    But somehow it does not work. the error is on line :

    dataGridView1.DataSource = ds.Tables["lide"];

    and it says :

    Object reference not set to an instance of an object.

    any ideas?

    thanks ,

    Ondra

    Tuesday, October 2, 2012 5:02 PM

Answers

  • Hello ondra, with DataSet you need to specify a DataMember name, in your case the table name:

    string sql = "select * from lide";
    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();
    ds.Tables.Add("lide");
    adapter.Fill(ds, "lide");
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = "lide";

    Regards,

    Miguel.

    • Marked as answer by Jason Dot Wang Wednesday, October 10, 2012 9:06 AM
    Wednesday, October 3, 2012 1:49 PM

All replies

  • Try this instead

                string sql = "select * from lide";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                conn.Open();
                adapter.Fill(dt);
                ds.Tables.Add(dt);
                conn.Close();
                dataGridView1.DataSource = ds.Tables[0]; // or you can pass dt directly

    If you prefer to call the table by the name instead the index(recommended for the readability) you can assign the name when you instantiate the table.

    DataTable dt = new DataTable("lide");


    Web Developer



    • Edited by Norkk Tuesday, October 2, 2012 5:17 PM
    • Proposed as answer by mkruluts Tuesday, October 2, 2012 5:19 PM
    Tuesday, October 2, 2012 5:16 PM
  • Doesnt work :(
    Tuesday, October 2, 2012 5:29 PM
  • adapter.Fill(ds, "lide");

    The problem is this line!

    2nd parameter "lide" is not set to any object (it must be a table name that exists), you simply write some name. If you want this to work, you have to do:

                string sql = "select * from lide";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                DataTable table1 = new DataTable("lide");
                conn.Open();
                adapter.Fill(ds, "lide");
                conn.Close();
                dataGridView1.DataSource = ds.Tables["lide"];



    Mitja

    Tuesday, October 2, 2012 5:32 PM
  • After that line

    adapter.Fill(dt);

    Create this, and put one breakpoint

    int count = dt.Rows.Count;

    And tell me which value count have. If have 0 that's is your problem, your select don't return anything. Because it's probably the reason to my code don't work.


    Web Developer


    • Edited by Norkk Tuesday, October 2, 2012 5:33 PM
    Tuesday, October 2, 2012 5:32 PM
  • But you actually dont even need a DataSet, which is only a connection of multiple dataTables. I assume you only need one datatable, so this code will do just fine:

    DataTable table = new DataTable("lide");
    using(SqlConnection conn = new SqlConnection"(connString"))
    {
        string query = @"select * from lide";
        using( SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
            adapter.Fill(table);
    }
    dataGridView1.DataSource = table;

    This will do!

    Mitja

    Tuesday, October 2, 2012 5:36 PM
  • But you actually dont even need a DataSet, which is only a connection of multiple dataTables. I assume you only need one datatable, so this code will do just fine:

    DataTable table = new DataTable("lide");
    using(SqlConnection conn = new SqlConnection"(connString"))
    {
        string query = @"select * from lide";
        using( SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
            adapter.Fill(table);
    }
    dataGridView1.DataSource = table;

    This will do
    I already point that, but just to add to your code, if he choose for that option, he doesn't need to create a table with name "lide" he can pass the instance of this table directly, exactly as you're doiing.

    Web Developer

    Tuesday, October 2, 2012 5:37 PM
  •  string sql = "select * from lide";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                conn.Open();
                adapter.Fill(dt);
                conn.Close();
                dataGridView1.DataSource = dt;
    in this code i dont use dataset and its all the same ..
    Tuesday, October 2, 2012 9:19 PM
  • it says 2, so I guess the dataset is filled allright...
    Tuesday, October 2, 2012 9:22 PM
  • If dataTable has data (rows) this code should work I suppose.

    And if you checked there are rows in dataTable (you said there are 2), the databinding should occur.

    Try changing binding to:

     dataGridView1.DataSource = new DataBinding(dt, null);

    I really dont know what the dataGridView is not populated with the data from dataTable.



    Mitja

    Tuesday, October 2, 2012 9:26 PM
  • this might be the problem, I cant create a DataBinding object, its not known for visual studio. In which package can I find it ?
    Tuesday, October 2, 2012 9:32 PM
  • Its from ControlBindingCollection.

    But even without explicitly defining BindingSource it should work.

    Where exactly do you do this code, in WinForms, WPF,..?


    Mitja

    Tuesday, October 2, 2012 10:37 PM
  • winforms 
    Wednesday, October 3, 2012 1:28 PM
  • Are you sure that your tables has values? This was supposed to work.

    dataGridView1.DataSource = dt;
    dataGridView1.DataBind();

    Can you put a breakpoint and see if there are rows? And additionaly can you put your entire code ?


    Web Developer

    Wednesday, October 3, 2012 1:32 PM
  • Hello ondra, with DataSet you need to specify a DataMember name, in your case the table name:

    string sql = "select * from lide";
    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();
    ds.Tables.Add("lide");
    adapter.Fill(ds, "lide");
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = "lide";

    Regards,

    Miguel.

    • Marked as answer by Jason Dot Wang Wednesday, October 10, 2012 9:06 AM
    Wednesday, October 3, 2012 1:49 PM
  • How do I define the width of the columns after filling them?...

    my hui : my thread

    Thursday, October 12, 2017 1:43 PM