Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView只读单元格_C#_Datagridview - Fatal编程技术网

C# DataGridView只读单元格

C# DataGridView只读单元格,c#,datagridview,C#,Datagridview,我有一个绑定的DataGridView,其中包含大量数据。问题是某些单元格必须是只读的,而且当用户在单元格之间使用TAB或ENTER进行导航时,只读单元格应该被忽略。加载后立即使某些特定单元格只读的最佳方法是什么 考虑到网格中有大量数据,在设置DataSource之后循环单元格不是一个好主意。此外,在CellEnter上使单元格只读也不起作用,因为使用TAB键导航时,我必须知道下一个单元格是否为只读。在绑定数据之前,请尝试使列而不是单个单元格为只读: this.dgrid.Columns["co

我有一个绑定的DataGridView,其中包含大量数据。问题是某些单元格必须是只读的,而且当用户在单元格之间使用TAB或ENTER进行导航时,只读单元格应该被忽略。加载后立即使某些特定单元格只读的最佳方法是什么


考虑到网格中有大量数据,在设置DataSource之后循环单元格不是一个好主意。此外,在CellEnter上使单元格只读也不起作用,因为使用TAB键导航时,我必须知道下一个单元格是否为只读。

在绑定数据之前,请尝试使列而不是单个单元格为只读:

this.dgrid.Columns["colName"].ReadOnly = true;
如果需要对列中的单个单元格执行此操作,则必须循环并按如下方式设置它们:

this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;

一旦列为只读(见Rashmi的响应),您就可以处理此事件

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Tab)
    {
        Boolean readOnly = (sender as DataGridView).SelectedCells[0].ReadOnly;

        return;
    }

}
将获取下一个单元格的只读属性

谢谢,我还没试过这个

但是,在RowEnter事件中,您可以将单元格的readonly属性设置为true(根据Rashmi)

我猜当您从一行移动到另一行时(或者当您从单元格A1更改为单元格B3时),应该触发RowEnter事件


这有帮助吗?

这里有一个很好的示例:


您只需要重写
Paint()
,我在compact framework上使用了它来根据单元格内容更改背景色,因此在同样的注释中,将其设置为只读应该不会有任何问题。

是否可以使用模板列而不是绑定列,然后为字段的只读性设置一个条件

然后,您可以为只读显示标签,为可编辑显示文本框。标签不会干扰选项卡索引

<asp:TemplateColumn>
  <ItemTemplate>
<%
    if ( <%# Eval( "ReadOnlyFlag" ) %> )
    { 
%>
    <asp:Label Text="<%# Eval( "BoundColumn" ) %>" />
<%
    }
    else
    {
 %>
    <asp:Textbox Text="<%# Eval( "BoundColumn" ) %>" />
<%
    }
%>
    </ItemTemplate>
</asp:TemplateColumn>

当需要禁用单元格时,可以使用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

您可以使用
beginingedit
事件检查单元格是否满足条件,如果不满足条件则取消操作:

在下面的示例中,如果单元格已经包含一个值,它将取消该操作,将其视为只读

xaml

<DataGrid BeginningEdit="DataGrid_BeginningEdit" ItemsSource="{Binding Example, Mode=TwoWay}"/>

我使用以下代码迭代了datagrid:

dataGridViewTest.DataSource=个人;
foreach(dataGridViewTest.Rows中的DataGridViewRow行)
{
foreach(row.Cells中的DataGridViewCell单元格)
{
if(cell.Value.ToString()=“John”)
{
cell.ReadOnly=true;
}
}
}

我不能,列中的某些单元格可以是只读的,而其他单元格不能。这取决于一些标志。对于单个列,您需要循环并将其设置为true。这将很耗时,但我认为没有其他选择..ReadOnly=true-是的,但这必须在数据绑定完成后完成。例如,在DataGridView.DataBindingComplete事件处理程序中。我无法设置只读列,因为同一列中的某些单元格可能是只读的,而其他单元格可能不是只读的。这取决于一些标志。最重要的是,我不能100%确定该列是否为只读,甚至会影响我编写的代码。也许值得一试…ASP.NET没有“DataGridView”的。。。OP请求WinForms回复。仅链接回复违反发布指南。一个完整的答案是用户不必导航到另一个页面来解决问题。
<DataGrid BeginningEdit="DataGrid_BeginningEdit" ItemsSource="{Binding Example, Mode=TwoWay}"/>
private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    string content = (e.EditingEventArgs.Source as TextBlock).Text;

    if (!(String.IsNullOrEmpty(content)))
        e.Cancel = true;
}