C# DataGridView 只读单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/943823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView read only cells
提问by
I have a binded DataGridView that contains a large amount of data. The problem is that some cells has to be ReadOnly and also when the user navigates with TAB or ENTER between cells, the ReadOnly cells should be bypassed. What's the best way of making some specific cells ReadOnly imediatly after loadging?
我有一个包含大量数据的绑定 DataGridView。问题是某些单元格必须是只读的,而且当用户在单元格之间使用 TAB 或 ENTER 导航时,应该绕过只读单元格。加载后立即使某些特定单元格只读的最佳方法是什么?
Looping through cells after I set DataSource is not a good idea taking in consideration that the grid has a large amount of data. Also, making the cell ReadOnly on CellEnter does not work because when navigating with TAB key I have to already know if the next cell is ReadOnly or not.
考虑到网格有大量数据,在设置 DataSource 后循环遍历单元格并不是一个好主意。此外,在 CellEnter 上使单元格只读不起作用,因为在使用 TAB 键导航时,我必须已经知道下一个单元格是否为只读。
回答by SO User
Try to make the column rather than individual cells readonly before binding the data:
在绑定数据之前,尝试将列而不是单个单元格设为只读:
this.dgrid.Columns["colName"].ReadOnly = true;
If you need to do for individual cells within the column, then you will have to loop and set them like this:
如果您需要对列中的单个单元格进行处理,则必须循环并像这样设置它们:
this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;
回答by SO User
Once the column is read only (see Rashmi's response) you can handle this event...
一旦该列是只读的(请参阅 Rashmi 的响应),您就可以处理此事件...
private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Tab)
{
Boolean readOnly = (sender as DataGridView).SelectedCells[0].ReadOnly;
return;
}
}
Which will get the next cell's read only property.
这将获得下一个单元格的只读属性。
Thanks
谢谢
回答by shahkalpesh
I haven't tried this.
我没试过这个。
But, you could set the cell's readonly property to true (as per Rashmi), on RowEnter event?
但是,您可以在 RowEnter 事件中将单元格的 readonly 属性设置为 true(根据 Rashmi)?
I guess RowEnter event should fire when you move from one row to the other (or it should when you change from cell A1 to B3).
我想 RowEnter 事件应该在您从一行移动到另一行时触发(或者当您从单元格 A1 更改为 B3 时应该触发)。
Does that help at all?
这些帮助有用?
回答by Fiur
There's a very nice sample here:
http://blogs.msdn.com/netcfteam/archive/2006/04/25/583542.aspx
这里有一个非常好的示例:http:
//blogs.msdn.com/netcfteam/archive/2006/04/25/583542.aspx
You just need to override Paint()
, I've used this on compact framework to change the backcolor depending on the cell contents so on the same note you shouldn't have any problem to set them read only.
您只需要覆盖Paint()
,我已经在紧凑框架上使用它来根据单元格内容更改背景颜色,因此在同一个注释中,将它们设置为只读应该没有任何问题。
回答by Adam Fox
Could you not use a template column instead of a bound column then have a condition for the readonlyness of the field?
你能不能不使用模板列而不是绑定列然后有一个字段的只读条件?
Then you could present a label for readonly and a textbox for editable. Labels would not interfere with your tab index.
然后,您可以显示一个只读标签和一个可编辑文本框。标签不会干扰您的标签索引。
<asp:TemplateColumn>
<ItemTemplate>
<%
if ( <%# Eval( "ReadOnlyFlag" ) %> )
{
%>
<asp:Label Text="<%# Eval( "BoundColumn" ) %>" />
<%
}
else
{
%>
<asp:Textbox Text="<%# Eval( "BoundColumn" ) %>" />
<%
}
%>
</ItemTemplate>
</asp:TemplateColumn>
回答by Ismael
You can use CellBeginEdit event and set e.Cancel = True when you need disable the cell.
当您需要禁用单元格时,您可以使用 CellBeginEdit 事件并设置 e.Cancel = True。
Private Sub DataGridView_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridViewMsg.CellBeginEdit
If DataGridViewMsg.Rows(e.RowIndex).Cells("disable").Value = "Y" Then
e.Cancel = True
End If
End Sub
回答by Anindya
this.dataGridViewEmpList.EditMode = DataGridViewEditMode.EditProgrammatically;
回答by Alfie
You can do this using the BeginningEdit
event to check if the check if the cell meets a condition and cancel the operation if not:
您可以使用BeginningEdit
事件来检查单元格是否满足条件,如果不满足则取消操作:
In the example below, if the cell already contains a value it will cancel the operation, deeming it as read only.
在下面的示例中,如果单元格已经包含一个值,它将取消操作,将其视为只读。
xaml
:
xaml
:
<DataGrid BeginningEdit="DataGrid_BeginningEdit" ItemsSource="{Binding Example, Mode=TwoWay}"/>
c#
:
c#
:
private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
string content = (e.EditingEventArgs.Source as TextBlock).Text;
if (!(String.IsNullOrEmpty(content)))
e.Cancel = true;
}