Datagridview remove all columns
Asked Answered
S

5

7

Is it possible in a single hit to remove all columns from a datagrid?

I know I could loop though and remove them one by one, or remove the whole thing and create it.

But it is possible to simply clear columns and leave the grid in place so users can start fresh. i.e. put it back to its original state.

OK here is the code I have

private void PopulateGrid()
    {
        UserVGrid.AutoGenerateColumns = false;
        UserVGrid.Columns.Clear();
        List<string> _values = populate.GetVaribles;
        foreach (var line in _values)
        {
            if (!line.Contains("Startind"))
            {
                string[] newline = line.Split('=');
                DataGridViewColumn newColumn = new DataGridViewTextBoxColumn(); 
                newColumn.Name = newline[0]; 
                newColumn.HeaderText = newline[1]; 
                newColumn.ToolTipText = newline[2]; 
                UserVGrid.Columns.Add(newColumn); 

            }
        }

so lets assume _values has apple, pear as values

running this method once and i get a datagrid with two colums named apple and pear.

running it a second time this time with _values containg char , table.
And i end up with 4 colums apple, pear, chair and table. What I want is the second time it is run it clears the grid fist and then applied the new colums.

Cheers

Aaron

Scrogan answered 15/9, 2011 at 12:37 Comment(0)
J
32

The DataGridView columns collection has a Clear() method.

dataGridView1.Columns.Clear();

This doesn't work with a bound datagridview and autogenerated columns - in that case simply replace the datasource with null or with an empty datasource:

dataGridView1.DataSource = null;

or turn off autogeneration and create the columns in code:

dataGridView1.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(col);

These manually generated columns will be removed by the Clear() method.

Jackson answered 15/9, 2011 at 12:40 Comment(4)
Although, are you instead trying to remove all data but leave the grid as is? The part of the question asking to put the grid back in its original state wasn't very clear.Jackson
sorry, what i actuly want to do is remove the coloum it self not just the data within it. Basicly the dataview gets populate from a text file config, at the moment if i open it twice i get duplicates. so i want some code to remove all current colums in the data view.Scrogan
@Scrogan in that case my answer does exactly what you want - with the obvious caveat around autogenerated columnsJackson
Oh, jsut relised it was a two fold issue, First i needed to be able to clear the colums, but also i was adding to the array i was passing each time. so no wonder i was gettign doubble the colums. But thank you as that clear method is exactly what i wanted.Scrogan
E
0

Here's what worked for me

 List<string> lstOverride = new List<string>();
                lstOverride.Add("IssuerOnly");
                lstOverride.Add("TickerOnly");
                lstOverride.Add("All");
                lstOverride.Add("Private Equity");
                lstOverride.Add("Ignore");
                DataGridViewComboBoxColumn cmb = new     DataGridViewComboBoxColumn();
                cmb.HeaderText = "Override";
                cmb.Name = "cmb";
                cmb.DataSource = lstOverride;
                cmb.DataPropertyName = "Override";
                dgvWebData.Columns.Add(cmb);

I added a DataGridViewComboBoxColumn and had set the DataPropertyName of it to the column I needed to have the drop down.

Expanse answered 6/8, 2015 at 12:44 Comment(0)
L
0

Clear the datatable

If ds.Tables.Count > 0 Then
    ds.Tables(0).Columns.Clear()
    ds.Tables(0).Rows.Clear()
End If

and set the datagridview datasource to nothing

dg.DataSource = Nothing
Leolaleoline answered 2/10, 2015 at 15:43 Comment(0)
T
0

for removing a columns of dataGridView you can do like that "P_Comment" is Columns Name

        dataGridView1.Columns.Remove("p_Comment");

and for remove all columns you can use

dataGridView1.Columns.Clear();
Ticklish answered 14/11, 2017 at 10:6 Comment(0)
K
0

This allowed me to delete all the data and the column headers from a datagridview

            ds2.Tables.Clear();
            dataGridView1.DataSource = null;
            dataGridView1.Columns.Clear();
Klecka answered 16/12, 2020 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.