none
Datagridview getting databinded from designer but not from codebehind

    Question

  • Hello,

    I have two datagridviews in my code. One is at the designer and the other is created at codebehind. I have a data table which is binded to a binding source and then that binding source is binded to each of the datagridviews.

    dgvDesigner.DataSource = bs;

    dgvCodeBehind.DataSource = bs;

    The designer one gets the data and shows it too, but as I debug I get to see that the Codebehind one didn't have any data. The row count was found to be zero for the codebehind one. dgvCodebehind had all the properties that the dgvDesigner had. I don't get to solve the problem. Please help.

    • Moved by CoolDadTxMVP Friday, March 30, 2012 2:20 PM Winforms related (From:Visual C# General)
    Wednesday, March 28, 2012 6:24 AM

Answers

  • This is the code that I used for the datagridview created in C# and getting it binded to a datatable.

    DataGridView dgv = new DataGridView(); SqlDataAdapter da = new SqlDataAdapter(myQuery, myConnection);//data adapter DataTable dt = new DataTable(); da.Fill(dt);//filling the data table BindingSource bs = new BindingSource(); bs.DataSource = dt;//binding the datatable to the binding source dgv.DataSource = bs;//binding the datagridview to the binding source

    int r = dgv.RowCount;//for getting the row count of the datagridview


    As I debug the code, I see 6 columns and 13 rows in the data table. But as I get to the last line, I see the row count of the datagridview is zero!

    However, If I bind that binding source "bs" to the datagridview created on designer then the row count of that datagridview gets to 13 correctly!

    Hi Danish,
    How is it going? Is the issue solved?
    You need to add the DataGridView to the Form before you call RowCount poperty. What’s more, you will get 0 until the form finish Initialization.
    Please change you code to

    private void Form1_Load(object sender, EventArgs e)
    {
    
        DataGridView dgv = new DataGridView();
        SqlDataAdapter da = new SqlDataAdapter(myQuery, myConnection);//data adapter 
        DataTable dt = new DataTable();
        da.Fill(dt);//filling the data table
        BindingSource bs = new BindingSource();
        bs.DataSource = dt;//binding the datatable to the binding source
        dgv.DataSource = bs;//binding the datagridview to the binding source
    
        this.Controls.Add(dgv); // add this row
        int r = dgv.RowCount;
    }

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 03, 2012 9:33 AM

All replies

  • Have you called the DataBind() method?

    dgvDesigner.DataSource = bs;
    dgvDesigner.DataBind();


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Wednesday, March 28, 2012 7:38 AM
  • The DataBind() method works with the gridview control in asp.net but in windows forms applications, datagridview is used. Datagridview control has no DataBind() method.

    If dt is my data table, I use the following lines to bind my datagridview

    BindingSource bs = new BindingSource();            bs.DataSource = dt; 
    dgv.DataSource = bs;

    Wednesday, March 28, 2012 10:00 AM
  • Where does your datatable come from?

    If it came from SQL server directly, I think it should be OK.

    If it came from the same data Binding source with the other dataGridView, we must be sure we have fill the data to the datatable.

    I have a similar code sinppet for your refernce:

       /// <summary>
       ///testDataSet is a data source I created with the Visual Studio. It has a datatable named test. 
       /// </summary>
     testDataSet.testDataTable dt = new testDataSet.testDataTable();
                testDataSetTableAdapters.testTableAdapter da = new testDataSetTableAdapters.testTableAdapter();
    
                da.Fill(dt);//we must fill the data to the datatable, or it will be empty. 
    
                BindingSource bs = new BindingSource();
                bs.DataSource = dt;
                this.dataGridView1.DataSource = bs;

    Hope it helps.

    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 30, 2012 2:33 AM
  • The datatable has the data in it that I checked in through debugging. The problem is that that the designer's datagridview gets filled from that datatable but from the same datatable the codebehind one doesn't. I have checked using the different binding source as well.
    Friday, March 30, 2012 7:58 AM
  • Could you show me more code you used now?

    I still can't reproduce this situation.  I am using the VS created a dataset and fill one datagridview, and then use the same dataset I fill a datatable, which binding to a datagridview too. They are all working well on my machine. 

    If possible, could you send me a test project? It will be very benefit for us to help you find the root cause. 

    Thanks for your understanding.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 30, 2012 8:30 AM
  • Danish,

    Be aware that the name CodeBehind was (wrongly) used in ASP.Net. In C# things are done in code and the presentation can be in XAML, in code, or HTML.

    Because you write about the DataGridView you use windows forms that is the only place where it exist and then the code created by the designer is in the ourprogram.designer.cs file, so you can simply copy that from there.


    Success
    Cor

    Friday, March 30, 2012 8:32 AM
  • This is the code that I used for the datagridview created in C# and getting it binded to a datatable.

    DataGridView dgv = new DataGridView(); SqlDataAdapter da = new SqlDataAdapter(myQuery, myConnection);//data adapter DataTable dt = new DataTable(); da.Fill(dt);//filling the data table BindingSource bs = new BindingSource(); bs.DataSource = dt;//binding the datatable to the binding source dgv.DataSource = bs;//binding the datagridview to the binding source

    int r = dgv.RowCount;//for getting the row count of the datagridview


    As I debug the code, I see 6 columns and 13 rows in the data table. But as I get to the last line, I see the row count of the datagridview is zero!

    However, If I bind that binding source "bs" to the datagridview created on designer then the row count of that datagridview gets to 13 correctly!

    Friday, March 30, 2012 10:35 AM
  • Dear Cor, Thanks for the correction. I did copy all the properties from the designer generated code to the datagridview I created in code but still it made no difference at all. 
    Friday, March 30, 2012 10:44 AM
  • I dont see your point using a Bindind Source.

    i have a simple example working perfectly maybe it help you

             try
                {
                    // call my class
                    Client cli = new Client();
                    // get my data on database
                    dataGridView1.DataSource = cli.getData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(" Error : " + ex.Message.ToString());
                }
    
            public DataTable getData()
            {
                DataTable dt = new DataTable();
                OdbcDataAdapter da = new OdbcDataAdapter("select * from client", conn);
                try
                {
                    conn.Open();
                    da.Fill(dt);
                }
                catch (OdbcException ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
    
                return dt;
            }
    Hope this helps.

    Friday, March 30, 2012 11:41 AM
  • Yes, we can use this without using the binding source. Even if I bind the datagridview directly to the datatable, It doesn't have any data.
    Friday, March 30, 2012 12:21 PM
  • Yes, we can use this without using the binding source. Even if I bind the datagridview directly to the datatable, It doesn't have any data.
    Can you show all code? because if you try the code that i sent to you, and even that doesnt work, its possible the problem be something else.
    Friday, March 30, 2012 12:27 PM
  • This is the code that I used for the datagridview created in C# and getting it binded to a datatable.

    DataGridView dgv = new DataGridView(); SqlDataAdapter da = new SqlDataAdapter(myQuery, myConnection);//data adapter DataTable dt = new DataTable(); da.Fill(dt);//filling the data table BindingSource bs = new BindingSource(); bs.DataSource = dt;//binding the datatable to the binding source dgv.DataSource = bs;//binding the datagridview to the binding source

    int r = dgv.RowCount;//for getting the row count of the datagridview


    As I debug the code, I see 6 columns and 13 rows in the data table. But as I get to the last line, I see the row count of the datagridview is zero!

    However, If I bind that binding source "bs" to the datagridview created on designer then the row count of that datagridview gets to 13 correctly!

    Hi Danish,
    How is it going? Is the issue solved?
    You need to add the DataGridView to the Form before you call RowCount poperty. What’s more, you will get 0 until the form finish Initialization.
    Please change you code to

    private void Form1_Load(object sender, EventArgs e)
    {
    
        DataGridView dgv = new DataGridView();
        SqlDataAdapter da = new SqlDataAdapter(myQuery, myConnection);//data adapter 
        DataTable dt = new DataTable();
        da.Fill(dt);//filling the data table
        BindingSource bs = new BindingSource();
        bs.DataSource = dt;//binding the datatable to the binding source
        dgv.DataSource = bs;//binding the datagridview to the binding source
    
        this.Controls.Add(dgv); // add this row
        int r = dgv.RowCount;
    }

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 03, 2012 9:33 AM