【问题标题】:Set Specific DataRows to read-only based on their value根据其值将特定 DataRows 设置为只读
【发布时间】:2026-01-20 21:15:01
【问题描述】:

我在向导窗口中显示数据表,方法是将 DataRows 添加到 DataTable(已经有 DataColumns),然后使用从该 DataTable 提供的 .DefaultView (DataView) 作为 DataGrid 的 .ItemsSource,它显示在页面。

我的问题是,我只希望特定行是只读的,基于行的列之一中提供的值。例如,如果在某一行中有一个值为 Yes 或 No 的列,则该行将基于此为只读。据我所知,当您将此 DataRow 添加到 DataTable 时,无法将其设为只读。

有没有办法循环遍历 DataTable、DataGrid 或 DataView 并根据其中一列中的值将特定行设置为只读?

【问题讨论】:

标签: c# sql wpf datagrid datatable


【解决方案1】:

您不能将数据对象设为只读,但可以将控件的行设为只读。

For Each row as DataGridViewRow In DataGridView1.Rows
    If row.Value(3) = True ' 3 is just a random number as I cannot see your example
        row.ReadOnly = True
    Else
        row.ReadOnly = False
    EndIf
Next 

或者,如果您想要 WPF 答案,我怀疑,请查看 this

【讨论】:

  • 我没有使用 DataGridView,而是使用 DataGrids 和 DataViews,这似乎是 WPF。所以据我所知,这并不适用。