简体   繁体   English

从 DataGridView 中删除列

[英]Remove Column from DataGridView

I have a database that has a Users table and I present the data in a DataGridView.我有一个包含用户表的数据库,我在 DataGridView 中显示数据。 I would like to remove 4 columns, but the code I have (referenced from MSDN) seems to append the columns at the end.我想删除 4 列,但我拥有的代码(从 MSDN 引用)似乎在末尾附加了这些列。 How can I totally REMOVE the columns?我怎样才能完全删除列?

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 :)非常感谢 :)

I tend to just hide the fields instead.我倾向于只是隐藏字段。

gvUsers.Columns["ID"].Visibility = false;

Et cetera.等等。

你也可以使用gvUsers.Columns.RemoveAt(IndexOfColumn);

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;

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.无法重新生成缺失的列。

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

I think you have to Modify DataTable before passing it to DGV我认为您必须在将 DataTable 传递给 DGV 之前对其进行修改

as follow:如下:

RadarServerEntities rse = new RadarServerEntities();
rse.Users.Columns.Remove("ID");
rse.Users.Columns.Remove("InsertDate");
rse.Users.Columns.Remove("Connections");
rse.Users.Columns.Remove("MachineID");
dgvUsers.DataSource=rse.Users;

this method worked for me这种方法对我有用

If what you want is to remove from a DataGridView is 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM