Datagridview remove all columns

白昼怎懂夜的黑 提交于 2019-12-04 07:59:33

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.

Sumit Agrawal

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.

pskaarup

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!