Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In button click am filling gridview using datatable as datasource,and also am adding button in cell of gridview.

here the problem is:

when i click button
1) gridview data should be refreshed.
gridview,Refresh();
is not refreshing .

2)
i can change the values of gridview ,and click the button in corresponding row, that row should be updatd in datsbase also
C#
<pre lang="c#"> private void btnedittxok_Click(object sender, EventArgs e)
        {
//            this.dataGridView1.EndEdit();

//this.dataGridView1.Refresh();
           
            if (txtedittxcustomer.Text != "")
            {
                String date1 = dateTimePicker1.Text;
                String[] date = Regex.Split(date1, "[/: ]");
                StringBuilder sb = new StringBuilder();
                sb.Append(date[2]);
                sb.Append("-");
                sb.Append(date[0]);
                sb.Append("-");
                sb.Append(date[1]);
                String fromdate = sb.ToString();


                String date2 = dateTimePicker2.Text;
                String[] date3 = Regex.Split(date2, "[/: ]");
                StringBuilder sb1 = new StringBuilder();
                sb1.Append(date3[2]);
                sb1.Append("-");
                sb1.Append(date3[0]);
                sb1.Append("-");
                sb1.Append(date3[1]);
                String todate = sb1.ToString();

                String customer = txtedittxcustomer.Text;
                String card = txtedittxcard.Text;


                using (MySqlConnection con = new MySqlConnection(ConnectionString))
                {
                    query = "select * from transaction where DATE(paid_date)BETWEEN '" + fromdate + "'AND'" + todate + "' AND customer_id='" + customer + "' ";
                    con.Open();
                    da = new MySqlDataAdapter(query, con);

                   //da.Fill(ds);
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                    int dtRows = dt.Rows.Count;
                    if (dtRows == 0)
                    {
                        MessageBox.Show("No Records");
                    }
                    else
                        groupBox2.Visible = true;
                    //dataGridView1.DataMember = ds.Tables[0].ToString();
                    DataGridViewButtonColumn btnEdit = new DataGridViewButtonColumn();
                    dataGridView1.Columns.Add(btnEdit);
                    btnEdit.HeaderText = "Edit";
                    btnEdit.Text = "Edit";
                    btnEdit.Name = "btnEdit";
                    btnEdit.UseColumnTextForButtonValue = true;

                }
              

            }
            else
                MessageBox.Show("Please enter Customer ID");
        }
Posted

1 solution

In the Update button Click Event,

First Update the new values to the DB and then call the method that loads the GridView, it will update the data to the DB and reflect your updated data in the Grid as well

In btnUpdate_Click Event,


C#
{
// Method for updating new values to DB
   UpdateDB();

//Method to Load GridView()
FillGrid();

}
 
Share this answer
 

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