question

PengfeiLiu-9437 avatar image
0 Votes"
PengfeiLiu-9437 asked PengfeiLiu-9437 commented

Datagridview becomes empty once its datasource data is changed

Hi,

My Winform application has a datagridview dgvTag and a button btnGen in a User Control. Clicking the button will generate the data in a datatable dtTag which is set as the data source of the datagridview. The code is like this:

         private void btnGen_Click(object sender, EventArgs e)
         {
             dtTag.Reset();
             dtTag.Columns.Add("A", typeof(string));
             dtTag.Columns.Add("B", typeof(string));
             dtTag.Columns.Add("C", typeof(string));
             dtTag.Columns.Add("D", typeof(string));
             dtTag.Columns.Add("E", typeof(string));
             dtTag.Columns.Add("F", typeof(string));
             dtTag.Columns.Add("G", typeof(string));
 //put the code here to generate the tags
 dgvTag.DataSource = dtTag;
             MessageBox.Show("Database generated.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
         }

Somehow, this code only works for the first time. If the button was clicked for the 2nd time, the datagridview becomes empty while only column headers are still there. I am sure the datatable dtTag is not not empty. Could you please help on this? Thanks.




windows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Does your code insert rows? Then check if this sequence helps (instead of line 12): dgvTag.DataSource = null; dgvTag.DataSource = dtTag.


0 Votes 0 ·

Hi Viorel-1,

Yes. The code add new row like this:

dtTag.Rows.Add(new object[] { "TYPE", "SCOPE", "NAME", "DESCRIPTION", "DATATYPE", "SPECIFIER", "ATTRIBUTES" });

I tried your code. But got the same result, meaning the 1st click will populate the data in the datagridview and the 2nd click will clear the data.

0 Votes 0 ·
Viorel-1 avatar image
1 Vote"
Viorel-1 answered PengfeiLiu-9437 commented

The next sequence seems to work:

dtTag.Clear( );
dtTag.Columns.Clear( );

Use it instead of dtTag.Reset( ), which appears to cause issues.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, Viorel. The code works like charm.

0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered PengfeiLiu-9437 commented

Hi PengfeiLiu-9437,
I made a test with your code and it worked fine.
Please refer to the following code:

 private void button1_Click(object sender, EventArgs e)
 {
     DataTable dtTag = new DataTable();
     dtTag.Reset();
     dtTag.Columns.Add("A", typeof(string));
     dtTag.Columns.Add("B", typeof(string));
     dtTag.Columns.Add("C", typeof(string));
     dtTag.Columns.Add("D", typeof(string));
     dtTag.Columns.Add("E", typeof(string));
     dtTag.Columns.Add("F", typeof(string));
     dtTag.Columns.Add("G", typeof(string));
     //put the code here to generate the tags
     String connectionString = "your connectionString";
    
     // Create a new data adapter based on the specified query.
     SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from test", connectionString);
    
     // Create a command builder to generate SQL update, insert, and
     // delete commands based on selectCommand.
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
    
     // Populate a new data table and bind it to the BindingSource.                
     dataAdapter.Fill(dtTag);       
     dgvTag.DataSource = dtTag;
     MessageBox.Show("Database generated.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Daniel,

What should I put here to replace "your connectionString"? I have no idea on how to use SQL.

I guess the "test" in ""select * from test"" also to be replaced by something else. What is it?

Thanks.

0 Votes 0 ·