【问题标题】:Winform DataBindingWinform 数据绑定
【发布时间】:2011-10-20 10:53:55
【问题描述】:

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

dgvLayout.AutoGenerateColumns = false;
dgvLayout.DataSource = list;

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

if ((dgvLayout.Rows[e.RowIndex].Cells["colControlText"].Value.ToString()) == "-Invalid-")
{
    if (e.ColumnIndex == 2 || e.ColumnIndex == 5)
    {
        return;
    }
    else if (e.ColumnIndex == 1)
    {
        return;
    }
}

【问题讨论】:

    标签: c# winforms data-binding datagridview


    【解决方案1】:

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

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

        /// <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】:

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

      【讨论】:

      • 谢谢!这就是我要找的
      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2013-06-21
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多