Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I am developing a windows application.In that one form contains multiple datagridivew which displays the table content from mysql table. In form i have button which links to another windows form and their i am inserting values to the table. After inserting when i click on Save button, it should save in the table and also updates in the datagridview.

If i call the display() method after inserting values to the table, it is displaying N number of times.

C#
public void display()
        {
            try
            {
                connection = new MySqlConnection(connection_string);
                connection.Open();
                
                //Code to Display Customer Details in Home Page
                dataAdapter_Customer=new MySqlDataAdapter(string.Format("select cusname,balance, contact, phone1,email from new_customer where busname=?p1"),connection);
                dataAdapter_Customer.SelectCommand.Parameters.AddWithValue("?p1", busnamevar);
                commandBuilder_Customer=new MySqlCommandBuilder(dataAdapter_Customer);
                dataTable_Customer.Locale=System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter_Customer.Fill(dataTable_Customer);
                bindingSource=new BindingSource();
                bindingSource.DataSource=dataTable_Customer;
                dataGridView1.DataSource=bindingSource;
               

                //Code to Display Supplier Details in Home Page
                dataAdapter_Supplier = new MySqlDataAdapter(string.Format("select suppliername,total_balance,contact,phone1,email from new_supplier where businessname=?p1"), connection);
                dataAdapter_Supplier.SelectCommand.Parameters.AddWithValue("?p1", busnamevar);
                commandBuilder_Supplier = new MySqlCommandBuilder(dataAdapter_Supplier);
                dataTable_Customer.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter_Supplier.Fill(dataTable_Supplier);
                bindingSource1 = new BindingSource();
                bindingSource1.DataSource = dataTable_Supplier;
                dataGridView3.DataSource = bindingSource1;
                                
                connection.Close();              
            }

            catch(MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


If the table contains 4 rows, when i call display() method, it is showing 8 rows, like that it is increasing when ever i click on refresh button.

I think i have to clean after displaying the contents.
Posted
Updated 31-Jul-13 20:37pm
v2

Before binding with Grid view, reset the Grid view with NULL.
 
Share this answer
 
YOu can put

Gridview.datasource = null;
Gridview.Databind();


after you insert the data to database....
 
Share this answer
 
Comments
SrinivasTiru 1-Aug-13 2:48am    
Thanks for your reply. I am using Windows Application, so DataBind() method is not available, and i tried with setting datasource with nll, but its not working. Now not displaying anything.
VICK 1-Aug-13 2:55am    
Oopss.. SOrry dude...
Hi,

as you said you are working on window app.

you can use

dataGridView1.Rows.Clear();

before binding gridview.
 
Share this answer
 
Comments
Member 13037527 12-Mar-17 0:19am    
I am also getting same problem. But I added that what you told. It is working now. Thanks
You should remove all datatablecollection as well as dataset that you have added the data before you do it again. Example:

C#
DataTableCollection dtc = new DataSet().Tables;//reset the data table collection
 dataGridView1.Rows.Clear();//reset the array "Rows" of datagridView

 dtc = dataConn.getQuery("select * from Tables", null).Tables;
 foreach (DataRow row in dtc[0].Rows)
 {
     dataGridView1.Rows.Add(row["data"].ToString()
 }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900