在datagridview中仅为某些列启用编辑模式c#

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

Enable edit mode in datagridview only for certain column c#

c#ms-accessdatagridview

提问by Kaoru

How do i enable edit mode only for the certain column?

如何仅为特定列启用编辑模式?

Let's say i have this columns:

假设我有这个列:

Product Code || Quantity || Description || Price

Product Code || Quantity || Description || Price

I wanted to enable edit mode only for Quantitycolumn and Pricecolumn. The rest cannot be edited.

我只想为Quantity列和Price列启用编辑模式。其余部分无法编辑。

I already did this below code, but it seems not working:

我已经在下面的代码中做了这个,但它似乎不起作用:

dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystroke;
dataGridView1.Columns["Quantity"].ReadOnly = false;
dataGridView1.Columns["Price"].ReadOnly = false;

All i can do is disable ReadOnlyto false, but it is enabled all of the columns that i don't want to enable them.

我所能做的就是关闭ReadOnlyfalse,但它使所有列的,我不想让他们。

Note: By the time program runs for the first time, i already set the ReadOnlyto true

注:在第一次时间程序运行时,我已经设置ReadOnlytrue

采纳答案by user2156093

this will help you

这会帮助你

MSDN DataGridViewColumn

MSDN DataGridViewColumn

回答by Jymaiso

You should set ReadOnly = true to all non-editable column. By default, column are set to ReadOnly = false

您应该将 ReadOnly = true 设置为所有不可编辑的列。默认情况下,列设置为 ReadOnly = false

回答by Radhakrishnan V

foreach (DataGridViewColumn dc in dgvTestParameter.Columns)
        {
            if (dc.Index.Equals(0) || dc.Index.Equals(4))
            {
                dc.ReadOnly = false;
            }
            else
            {
                dc.ReadOnly = true;
            }
        }

回答by user3521369

int EditableCol_index = datagridview["colname"].index;

// if this is true , you wont be able to edit even you set the column readonly=false;
datagridview1.readonly = false;
foreach (datagridviewcolumns c in datagridview1 )
{
   c.readonly = true;
   if( c.index == EditableCol_index )
   { 
     c.readonly = false;  // editable
   }
}

回答by M.Usman

From design part you just check "Enable Editing" mode in your Datagridview. this example may help you,

从设计部分,您只需在 Datagridview 中选中“启用编辑”模式。这个例子可能对你有帮助,

enter image description here

在此处输入图片说明

I have added two columns on as readonly purpose(column name is "ReadOnly Column") and another as editable column(column name is "Normal Column").

我添加了两列作为只读目的(列名是“只读列”),另一列作为可编辑列(列名是“普通列”)。

Code:

代码:

dataGridView1.Columns["readOnlyColumn"].ReadOnly = true;

Thank you....

谢谢....

回答by mathtec

The out-of-box solution is using attributes on your data model:

开箱即用的解决方案是在您的数据模型上使用属性:

public class MyDataModel
{
    // Do not show in data grid view
    [System.ComponentModel.Browsable(false)]
    public virtual int ID { get; protected set; }

    // Set to read-only in data grid view
    [System.ComponentModel.ReadOnly(true)]
    public virtual string Person { get; set; }
}