Clear a datagridview in C #

2

I have tried all this already and nothing has served. All are excluding rows and columns. What I need is to clean up the content written on them.

datagridview.datasource=null;
datagridview.resetbindings();
datagridview.Rows.Clear()
DataGridView1.DataSource=null; //Remover a datasource
DataGridView1.Columns.Clear(); //Remover as colunas
DataGridView1.Rows.Clear();    //Remover as linhas
DataGridView1.Refresh();    //Para a grid se actualizar
    
asked by anonymous 14.08.2014 / 20:54

6 answers

1

Hello everyone, I solved my problem like this: Clear a C # dataDrid

dataGridView1.DataSource = null;

Then just do it:

dataGridView1.Columns.Add("Coluna", "Coluna");//Acrescenta colunas
dataGridView1.Columns.Add("Coluna2", "Coluna2");//Acrescenta colunas
dataGridView1.Rows.Add("A", "B");//Acrescenta Linhas
    
15.08.2014 / 15:59
3

You need to go through the Rows and clean each cell as follows:

foreach (var row in dataGridView1.Rows)
{
    foreach (var cell in row.Cells)
    {
        cell.Value = ""; // ou qualquer outro valor que signifique 'limpar' no seu contexto
    }
}
    
15.08.2014 / 03:33
1

At first the other forms described above did not work out here, so ...

This removes all rows values etc., (gray pattern).

for (int i = 0; i < dataGridView1.RowCount; i++)
{
   dataGridView1.Rows[i].DataGridView.Columns.Clear();
}
    
20.07.2017 / 17:35
0
        //LIMPAR GRID
        dataGridView1.Rows.Clear();
        dataGridView1.Refresh();
    
22.03.2015 / 19:56
0

Generally I use DataSet (Ds) as DataSource in DataGridView (DGV) and when I want to clean the grid I use

DGV.DataSouce= Ds;
Ds.Clear(); //Retira os valores da tabela mantendo os campos

So I can both clean the DGV and update it. I hope I have helped

    
13.12.2015 / 19:20
0

The lower command clears lines except the grid header

dataGridView2.Rows.Clear();
    
25.12.2016 / 15:44