首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Datagridview中启用和禁用单元格

在Datagridview中启用和禁用单元格
EN

Stack Overflow用户
提问于 2009-03-10 06:25:50
回答 3查看 73.9K关注 0票数 24

我使用了一个DataGridView控件来显示一些数据。我需要根据网格中的一些值动态地启用和禁用一些数据。

有人能告诉我怎么做吗?

EN

回答 3

Stack Overflow用户

发布于 2011-03-14 02:41:50

要“禁用”一个单元格,它必须是只读的,并且以某种方式灰显。此函数用于启用/禁用DataGridViewCell:

代码语言:javascript
复制
    /// <summary>
    /// Toggles the "enabled" status of a cell in a DataGridView. There is no native
    /// support for disabling a cell, hence the need for this method. The disabled state
    /// means that the cell is read-only and grayed out.
    /// </summary>
    /// <param name="dc">Cell to enable/disable</param>
    /// <param name="enabled">Whether the cell is enabled or disabled</param>
    private void enableCell(DataGridViewCell dc, bool enabled) {
        //toggle read-only state
        dc.ReadOnly = !enabled;
        if (enabled)
        {
            //restore cell style to the default value
            dc.Style.BackColor = dc.OwningColumn.DefaultCellStyle.BackColor;
            dc.Style.ForeColor = dc.OwningColumn.DefaultCellStyle.ForeColor;
        }
        else { 
            //gray out the cell
            dc.Style.BackColor = Color.LightGray;
            dc.Style.ForeColor = Color.DarkGray;
        }
    }
票数 45
EN

Stack Overflow用户

发布于 2009-03-10 07:14:23

您可以将特定行或单元格设置为只读,这样用户就不能更改该值。你是这个意思吗?

代码语言:javascript
复制
dataGridView1.Rows[0].ReadOnly = true;
dataGridView1.Rows[1].Cells[2].ReadOnly = true;
票数 18
EN

Stack Overflow用户

发布于 2017-05-24 21:45:53

步骤1:

代码语言:javascript
复制
type form load : -
For i = 0 to Datagridview.columns.count -1
   if i <> 1 then //restricted columns, 'i' is Your column index
    Datagridview.Columns(i).ReadOnly = True
   end if
Next

第二步

代码语言:javascript
复制
type Cellbeginedit
Datagridview.BeginEdit(True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/629107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档