DataGridView中删除所有列 [英] Datagridview remove all columns

查看:2777
本文介绍了DataGridView中删除所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是在一个单一的打击有可能从数据网格中删除所有列?

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.

确定这里是代码我有

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

            }
        }



所以让我们假设_values有苹果,梨作为值

so lets assume _values has apple, pear as values

运行一次这个方法,我也得到一个名为苹果和梨2 colums一个DataGrid。

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

运行它第二次这次_values containg字符,表。结果
和我结束了4 colums苹果,梨,椅子和桌子。我要的是它运行它清除电网拳头,然后应用新colums第二次。

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.

干杯

亚伦

推荐答案

DataGridView的列集合有一个明确的( 。)方法

The DataGridView columns collection has a Clear() method.

dataGridView1.Columns.Clear();

这不绑定的DataGridView和自动生成列工作 - 在这种情况下,只需更换与数据源空或空的数据源:

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.

这篇关于DataGridView中删除所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆