C# 从 DataGridView 中删除列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8930727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Column from DataGridView
提问by craig1231
I have a database that has a Users table and I present the data in a DataGridView. I would like to remove 4 columns, but the code I have (referenced from MSDN) seems to append the columns at the end. How can I totally REMOVE the columns?
我有一个包含用户表的数据库,我在 DataGridView 中显示数据。我想删除 4 列,但我拥有的代码(从 MSDN 引用)似乎在末尾附加了这些列。我怎样才能完全删除列?
So this is how the DGV looks without the columns removed
所以这就是未移除列的 DGV 的外观


The Code I use to TRY and remove the columns
我用来尝试和删除列的代码
RadarServerEntities rse = new RadarServerEntities();
gvUsers.DataSource = rse.Users;
gvUsers.Columns.Remove("ID");
gvUsers.Columns.Remove("InsertDate");
gvUsers.Columns.Remove("Connections");
gvUsers.Columns.Remove("MachineID");
The result
结果


I would like to get rid of the last 4 columns, so why isnt my code doing it?
我想去掉最后 4 列,那为什么我的代码不这样做呢?
Many Thanks :)
非常感谢 :)
采纳答案by Alexander R
I tend to just hide the fields instead.
我倾向于只是隐藏字段。
gvUsers.Columns["ID"].Visibility = false;
Et cetera.
等等。
回答by Gabriel GM
if you don't want to create the columns automatically when you bind your DataSource, you need to set gvUsers.AutoGenerateColumns = false;
如果您不想在绑定时自动创建列DataSource,则需要设置gvUsers.AutoGenerateColumns = false;
RadarServerEntities rse = new RadarServerEntities();
gvUsers.AutoGenerateColumns = false;
gvUsers.DataSource = rse.Users;
回答by A.Denno
you could also use gvUsers.Columns.RemoveAt(IndexOfColumn);
你也可以使用 gvUsers.Columns.RemoveAt(IndexOfColumn);
回答by Rashad.Z
DataGridViewColumn DataGridViewColumnSelected;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex !=-1 && e.RowIndex == -1)
{
DataGridViewColumnSelected = dataGridView1.Columns[e.ColumnIndex] as DataGridViewColumn;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool bHandled = false;
switch (keyData)
{
case Keys.Delete:
if (DataGridViewColumnSelected != null)
{
this.dataGridView1.Columns.RemoveAt(DataGridViewColumnSelected.Index);
//dataGridView1.Columns[DataGridViewColumnSelected.Name].Visible = false; // case of just hiding the column
}
break;
}
return bHandled;
}
回答by Soul Reaver
To actually remove automatically generated column you have to turn off automatic generation after data binding.
要实际删除自动生成的列,您必须在数据绑定后关闭自动生成。
So the code would be:
所以代码将是:
RadarServerEntities rse = new RadarServerEntities();
gvUsers.DataSource = rse.Users;
gvUsers.AutoGenerateColumns = false;
gvUsers.Columns.Remove("ID");
gvUsers.Columns.Remove("InsertDate");
gvUsers.Columns.Remove("Connections");
gvUsers.Columns.Remove("MachineID");
I haven't checked what exactly happens, but probably the moment DGV becomes visible, missing columns are re-autogenerated.
我还没有检查到底发生了什么,但可能当 DGV 变得可见时,丢失的列会重新自动生成。
So with this solution you have columns generated at the moment of binding data, then you turn it off and remove columns that you don't need. Missing columns can't be re-generated.
因此,使用此解决方案,您可以在绑定数据时生成列,然后将其关闭并删除不需要的列。无法重新生成缺失的列。
回答by Yokii Rod
If what you want is to remove from a DataGridViewis the column you have selected then this code is for you.
如果您想要从DataGridView您选择的列中删除,那么此代码适合您。
Place this code in the delete button and ready to delete the column you have selected.
将此代码放在删除按钮中并准备删除您选择的列。
int rowIndex = TuDataGrigView1.CurrentCell.RowIndex;
TuDataGrigView1.Rows.RemoveAt(rowIndex);

