开发者

Datagridview remove all columns

开发者 https://www.devze.com 2023-04-05 06:10 出处:网络
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.

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


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.


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.


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


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();


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

            ds2.Tables.Clear();
            dataGridView1.DataSource = null;
            dataGridView1.Columns.Clear();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号