【问题标题】:DataGridView read only cellsDataGridView 只读单元格
【发布时间】:2010-10-30 22:31:45
【问题描述】:

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

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

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

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

    this.dgrid.Columns["colName"].ReadOnly = true;
    

    如果您需要对列中的单个单元格执行此操作,则必须像这样循环并设置它们:

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

    【讨论】:

    • 我不能,列中的某些单元格可能是只读的,而另一些则不是。这取决于一些标志。
    • 对于单个列,您需要循环并将其设置为 true。这会很耗时,但我认为没有其他选择。
    • .ReadOnly = true - 是的,但这必须在数据绑定完成后完成。例如。在 DataGridView.DataBindingComplete 事件处理程序中。
    【解决方案2】:

    您可以使用 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
    

    【讨论】:

      【解决方案3】:

      我没试过。

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

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

      这有帮助吗?

      【讨论】:

        【解决方案4】:
        this.dataGridViewEmpList.EditMode = DataGridViewEditMode.EditProgrammatically;
        

        【讨论】:

          【解决方案5】:

          我使用以下代码遍历数据网格:

          dataGridViewTest.DataSource = persons;
          
          foreach (DataGridViewRow row in dataGridViewTest.Rows)
          {
              foreach (DataGridViewCell cell in row.Cells)
              {
                  if (cell.Value.ToString() == "John")
                  {
                      cell.ReadOnly = true;
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:

            一旦该列是只读的(请参阅 Rashmi 的回复),您就可以处理此事件...

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

            这将获得下一个单元格的只读属性。

            谢谢

            【讨论】:

            • 我无法在 ReadOnly 列上放置,因为同一列中的某些单元格可能是 ReadOnly 而其他则不是。这取决于一些标志。
            • 老实说,我不能 100% 确定该列是否为 ReadOnly 甚至会影响我编写的代码。可能值得一试......
            【解决方案7】:

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

            在下面的例子中,如果单元格已经包含一个值,它将取消操作,认为它是只读的。

            xaml:

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

            c#:

            private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
            {
                string content = (e.EditingEventArgs.Source as TextBlock).Text;
            
                if (!(String.IsNullOrEmpty(content)))
                    e.Cancel = true;
            }
            

            【讨论】:

              【解决方案8】:

              您不能使用模板列而不是绑定列然后对字段的只读性设置条件吗?

              然后您可以显示一个只读标签和一个可编辑文本框。标签不会干扰您的标签索引。

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

              【讨论】:

              • ASP.NET 没有“DataGridView”... OP 要求 WinForms 答案。
              【解决方案9】:

              这里有一个很好的示例:
              http://blogs.msdn.com/netcfteam/archive/2006/04/25/583542.aspx

              您只需要覆盖 Paint() ,我已经在紧凑框架上使用它来根据单元格内容更改背景色,因此在同一张纸条上,将它们设置为只读应该没有任何问题。

              【讨论】:

              • 仅链接回复违反了发布指南。一个完整的答案是用户不必导航到另一个页面来解决问题。
              猜你喜欢
              • 2012-01-06
              • 1970-01-01
              • 1970-01-01
              • 2013-11-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多