禁用DataGrid中的特定单元格编辑 [英] Disabling specific cell edit in DataGrid
问题描述
你可能会问:为什么我需要这个?那么我的应用程序需要根据其他单元格内容禁用行中的特定单元格。每一行都以这种方式分开检查。
如果你有一个想法,我可以如何实现这样一个不寻常的行为请写;)
如果你有您希望禁用的单元格/单元格的行,列索引:
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屋!