首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Winform DataBinding

Winform DataBinding
EN

Stack Overflow用户
提问于 2011-10-20 18:53:55
回答 2查看 499关注 0票数 1

我需要启用/禁用datagridview的行/列,这可以通过在绑定后循环所有行来轻松完成。但是我想在数据绑定的时候这样做...有没有办法做到这一点?另外,如何启用/禁用行单元格?

代码语言:javascript
复制
dgvLayout.AutoGenerateColumns = false;
dgvLayout.DataSource = list;

在单元格中单击,但不起作用

代码语言:javascript
复制
if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-")
{
    if (e.ColumnIndex == 2 || e.ColumnIndex == 5)
    {
        return;
    }
    else if (e.ColumnIndex == 1)
    {
        return;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-21 04:01:55

您可以使用此解决方案启用和禁用单元格

要“禁用”一个单元格,它必须是只读的,并且以某种方式灰显。此函数用于启用/禁用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;
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2011-10-20 20:24:51

你可以在datagrid的RowsAdded事件上编写你的代码

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7834862

复制
相关文章

相似问题

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