C# 如何隐藏 dataGridView 中的特定列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15090093/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:01:01  来源:igfitidea点击:

How can I hide a specific column form a dataGridView?

c#winforms

提问by Rika

How can I hide a specific Column in DataGridviewalong with its header while being able to use its value ? ( just doesn't get shown ) . The following code doesn't work.

如何DataGridview在能够使用其值的同时隐藏特定列及其标题?(只是没有显示)。以下代码不起作用。

 gridview.Rows[e.RowIndex].Cells[11].Visible = false;

采纳答案by duDE

Try this:

尝试这个:

gridview.Columns["ColumnName"].Visible = false;

http://msdn.microsoft.com/en-us/library/0c24a0d7.aspx

http://msdn.microsoft.com/en-us/library/0c24a0d7.aspx

回答by Stuart

Add the column name/s to the DataKeyNamesof the grid view...

将列名称添加到DataKeyNames网格视图的...

<asp:gridview id="GridView1" runat="server" datakeynames="ColumnName" onselectedindexchange="GridView1_SelectedIndexChanged"></asp:gridview>

you can then access the datakeys in the code behind...

然后您可以访问背后代码中的数据键...

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    textbox.Text = GridView1.DataKeys[GridView1.SelectedIndex].Values["ColumnName"].ToString();
}

You can add more DataKeyNamesby separating with a comma. Then you don't need to add the column to the GridViewat all if you don't want to.

您可以DataKeyNames通过用逗号分隔来添加更多内容。然后,如果您不想,则根本不需要将该列添加GridView到 。

回答by Sohail Malik

This is 100% correct solution.... this.dataGridView1.Columns[0].Visible = false;

这是 100% 正确的解决方案.... this.dataGridView1.Columns[0].Visible = false;

回答by Ramgy Borja

if you want to hide gridview column, you should have a condition..

如果你想隐藏 gridview 列,你应该有一个条件..

if(userlevel != 'Administrator') { GridView.Columns["ColumnName"].Visible = false; }

if(userlevel != 'Administrator') { GridView.Columns["ColumnName"].Visible = false; }