DataGridView.Clear()

前端 未结 19 1829
夕颜
夕颜 2020-12-02 20:01

Here comes the trouble. I want to delete all rows from datagridview. This how i add rows:

private void ReadCompleteCallback(object clientHandle, Opc.Da.ItemV         


        
相关标签:
19条回答
  • 2020-12-02 20:31

    Try this Code

    private void button_Click(object sender, EventArgs e) {
        if (this.dgInv.DataSource != null) {
            this.dgInv.DataSource = null;
        } else {
            this.dgInv.Rows.Clear();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 20:32

    If you want clear a dataGridView not binded to DataSource but manually loaded you can use this simple code:

    datagridview.Rows.Clear();
    datagridview.Columns.Clear();
    
    0 讨论(0)
  • 2020-12-02 20:36

    all you need to do is clear your datatable before you fill it... and then just set it as you dgv's datasource

    0 讨论(0)
  • 2020-12-02 20:37

    I don't like messing with the DataSource personally so after discussing the issue with an IT friend I was able to discover this way which is simple and doesn't effect the DataSource. Hope this helps!

    foreach (DataGridViewRow row in dataGridView1.Rows) 
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            cell.Value = "";
        }
    }
    
    0 讨论(0)
  • 2020-12-02 20:39

    Simply if your are trying to rebind your Data Grid View just code this:

    DataGridView1.DataSource = DataSet.Tables["TableName"];
    

    OR

    DataGridView.DataSource = DataTable;
    

    else if you trying to to clear your Data Grid View just code this:

    DataGridView.DataSource = null;
    DataGridView.Rows.Clear();
    DataGridView.Refresh();
    

    and add any method or event to bind Data Grid View Again below this line of code....

    0 讨论(0)
  • 2020-12-02 20:41

    Basically below code line is only useful in data binding scenarios

    datagridview.DataSource = null; 
    

    otherwise below is the used when no data binding and datagridview is populated manually

    dataGridView1.Rows.Clear();
    dataGridView1.Refresh();
    

    So, check first what you are doing.

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