简体   繁体   English

需要在Winform中禁用数据网格视图中的列

[英]Need to Disable a column in a Data Grid View in Winform

I have a data grid view ( say 5 columns ) . 我有一个数据网格视图(比如5列)。 User has the option to disable/enable 1 particular column of the data table (using radio buttons). 用户可以选择禁用/启用数据表的1个特定列(使用单选按钮)。 So How should I go about doing it? 那我该怎么做呢?

When I select to disable the column, I need to make it non editable, change the color of the column so that user can understand that the column is disabled and any other suggestion are also welcome. 当我选择禁用列时,我需要使其不可编辑,更改列的颜色,以便用户可以理解该列已被禁用,并且欢迎任何其他建议。

Set the column's ReadOnly property to true to make it non-editable. 将列的ReadOnly属性设置为true以使其不可编辑。 And alter its DefaultCellStyle.BackColor (and/or ForeColor) to make it obvious to the user. 并更改其DefaultCellStyle.BackColor(和/或ForeColor)以使其对用户显而易见。

I also came across the same problem,and its sol. 我也遇到了同样的问题,以及它的解决方案。 for me is: 对我来说是:

 int n = Convert.ToInt32(dataGridView3.Rows.Count.ToString());
         for (int i = 0; i < n; i++)
         {
             dataGridView3.Rows[i].Cells[0].ReadOnly = true;
         }

And it really worked for me.Works good when you are not going to declare columns name in datagridview and bringing it from any database. 它确实对我很有用。当你不打算在datagridview中声明列名并从任何数据库中引入它时,它很好。

Your GridView control exposes the Columns property. GridView控件公开Columns属性。 Through these objects you can set properties (including visibility) for the individual columns. 通过这些对象,您可以为各列设置属性(包括可见性)。 Example: 例:

GridView.Columns[6].visible=false;

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

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