Datagridview not updating/refreshing

前端 未结 5 1514
忘掉有多难
忘掉有多难 2020-12-17 20:25

I have a DataGridView made of a DataSet of a table from the DB. When I delete a row, it is updated in the database but it is not removed from the GridView. Only when I rest

5条回答
  •  悲&欢浪女
    2020-12-17 20:56

    Hope this helps you?

    if you are showing your table in dgv and for deleting something from that table you have a button on your win form. you chose let's say ID and click "delete" button for delting an item from db table. Here is that code:

    private void btn_Delete_Click(object sender, EventArgs e)
            {
                int selectedCellCount = dgv.GetCellCount(DataGridViewElementStates.Selected);
    
                if (selectedCellCount > 0)
                {
                    string selection;
    
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        selection = dgv.SelectedCells[i].Value.ToString();
                        string qs_delete = "DELETE FROM yor_table WHERE id = '" + selection + "';";
                        try
                        {
                            conn = new MySqlConnection(cs);
                            conn.Open();
    
                            cmd = new MySqlCommand();
                            cmd.Connection = conn;
                            cmd.CommandText = qs_delete;
                            cmd.ExecuteNonQuery();
    
                            conn.Close();
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                            finally
                            {
                               if (conn != null) conn.Close();
                             }
                         }
                    }    
    
    //don't forget to load your table again in dgv
                string qs_select = "SELECT * FROM your_table";
    
                System.Data.DataTable dataTable = new System.Data.DataTable();
                dataTable.Clear();
                dgv.DataSource = dataTable;
    
                try
                {
                    conn = new MySqlConnection(cs);
                    cmd = new MySqlCommand(qs_select, conn);
                    conn.Open();
    
                    da = new MySqlDataAdapter(cmd);
                    da.Fill(dataTable);
    
                    cb = new MySqlCommandBuilder(da);
    
                    dgv.DataSource = dataTable;
                    dgv.DataMember = dataTable.TableName;
                    dgv.AutoResizeColumns();
    
                    conn.Close();
                }
                catch (Exception ex)
                {
                     MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (conn != null) conn.Close();
                }
             }
    

    You'll notice here that i have MySQL db but don't bother yourself with that.

提交回复
热议问题