禁用DataGrid中的特定单元格编辑 [英] Disabling specific cell edit in DataGrid

查看:145
本文介绍了禁用DataGrid中的特定单元格编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的是,可以禁用DataGrid中的特定单元格编辑,不使用禁用Silverlight 4中整个列的编辑。我可以将特定单元格对象作为FrameworkElement获取,但是不包含属性IsReadOnly或IsEnabled。
你可能会问:为什么我需要这个?那么我的应用程序需要根据其他单元格内容禁用行中的特定单元格。每一行都以这种方式分开检查。
如果你有一个想法,我可以如何实现这样一个不寻常的行为请写;)

解决方案

如果你有您希望禁用的单元格/单元格的行,列索引:

  int r = 2,c = 4; 

然后您可以收听CellEnter和CellLeave的事件,并执行以下操作:

  private void dataGridView1_CellEnter(object sender,DataGridViewCellEventArgs e)
{
if(e.RowIndex == r)
{
if(e.ColumnIndex == c)
{
dataGridView1.Columns [e.ColumnIndex] .ReadOnly = true;
}
}
}

private void dataGridView1_CellLeave(object sender,DataGridViewCellEventArgs e)
{
if(e.RowIndex == r )
{
if(e.ColumnIndex == c)
{
dataGridView1.Columns [e.ColumnIndex] .ReadOnly = false;
}
}
}

你还在设置整列到Readonly,但是由于您在离开单元格后将其重新设置,因此它具有仅在单元格工作的效果。


I need to know is it possible to disable a specific cell edit in a DataGrid, without disabling edit for the entire column in Silverlight 4. I can get the specific cell object as a FrameworkElement but it does not contain property IsReadOnly or IsEnabled. You will probably ask: Why do I need that? Well my application requires disabling particular cells in row depending on the other cell content. Each row is being checked this way separately. If you have an idea how I can achieve such a unusual behaviour please write ;)

解决方案

If you have the row,column index of the cell/cells that you wish to have disabled:

int r = 2, c = 4;

Then you can listen to the events CellEnter and CellLeave and do the following:

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == r)
        {
            if (e.ColumnIndex == c)
            {
                dataGridView1.Columns[e.ColumnIndex].ReadOnly = true;
            }
        }
    }

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == r)
        {
            if (e.ColumnIndex == c)
            {
                dataGridView1.Columns[e.ColumnIndex].ReadOnly = false;
            }
        }
    }

You're still setting the entire column to Readonly, but since you are resetting it back after you leave the cell it has the effect of appearing to only work for the cell.

这篇关于禁用DataGrid中的特定单元格编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆