Datagridview not updating/refreshing

前端 未结 5 1495
忘掉有多难
忘掉有多难 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.

    0 讨论(0)
  • 2020-12-17 21:00

    You need to reset the binding on your bindingsource.

    bindingSource.ResetBindings(false);
    
    0 讨论(0)
  • 2020-12-17 21:08

    You have to call this function after every deletion(Re-bind Grid).

    void BindGrid()
    {
    YourDataGridView.DataSource = dataset;
    YourDataGridView.DataBind();
    }
    
    0 讨论(0)
  • 2020-12-17 21:13

    If database is updated and you want to refresh DataGridView, call this:

    this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>);
    

    For example: Where is name of your table (for example Customers) and is name of your database (for example MyDB).

    this.CustomersTableAdapter.Fill(this.MyDBDataSet.Customers);
    
    0 讨论(0)
  • 2020-12-17 21:16

    This is a very simple process.

    1.) Create a Binding Source

    2.) Set the Datasource for this object to your Dataset Table.

    3.) Set The datasource for your DatagridView as the Binding source Object.

    Code Example:

    Dataset ds = new Dataset();
    BindingSource bs = new BindingSource()
    bs.Datasource = ds.Table[0];
    DatagridView.Datasource = bs;
    

    Now, Any changes you make in the DataTable will ripple through to your GridView automatically.

    0 讨论(0)
提交回复
热议问题