简体   繁体   English

数据源为只读-C#

[英]datasource as readonly - C#

I have a DataGridView with properties DataSource = datatable() and readonly = false . 我有一个具有属性DataSource = datatable()readonly = false的DataGridView。 readonly must be false since there are other columns that can be editable. readonly必须为false,因为还有其他可编辑的列。 How do I make all the columns in DataSource read only (not editable)? 如何使DataSource中的所有列均为只读(不可编辑)?

The code is as follows: 代码如下:

type = new DataGridViewComboBoxColumn();
        table= new DataGridView
                         {
                             DataSource = datatable(), // this returns a DataTable object
                             AllowUserToAddRows = false,
                             AllowUserToDeleteRows = false,
                             RowHeadersVisible = false,
                             MultiSelect = false,
                             Name = "AgentTable",
                             AutoSize = true,
                             ReadOnly = false,
                         };
        table.Columns.Add(CreateStartButton());        
        type.Items.Add(" some table");
        type.ReadOnly = false;
        table.Columns.Add(type);

EDIT: the datagridview will contain 4 columns. 编辑:datagridview将包含4列。

  • First column, each cell is a button (readonly doesnt matter) 第一列,每个单元格都是一个按钮(只读无关紧要)
  • second column, each cell is a drop down box (readonly is false) 第二列,每个单元格都是一个下拉框(只读为false)
  • third and fourth columns are created as DataTable object so (readonly must be true) 第三和第四列创建为DataTable对象,因此(只读必须为true)

so my question is how to make the third and forth column read-only? 所以我的问题是如何使第三列和第四列为只读?

It should be easy to iterate the columns after you create them all. 创建所有列后,应该很容易对它们进行迭代。 I'd use something like this, but I'm calling it pseudocode because I don't know if DataColumn is the correct data type: 我会使用类似这样的东西,但是我称它为伪代码,因为我不知道DataColumn是否是正确的数据类型:

foreach(DataColumn col in table.Columns) {
    col.ReadOnly = true;
}

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

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