【问题标题】:How to make the last row in a ReadOnly DataGrid editable如何使 ReadOnly DataGrid 中的最后一行可编辑
【发布时间】:2013-12-02 17:28:24
【问题描述】:

我正在使用 ReadOnly DataGrid 在我的 WPF 应用程序中显示一些数据。

那么我的问题是。

如何使最后一行可编辑?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:
    <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Students}" LoadingRow="dataGrid_LoadingRow_1">
    
    private void dataGrid_LoadingRow_1(object sender, DataGridRowEventArgs e)
        {
            if (e.Row.IsNewItem)
                e.Row.IsEnabled = true;
            else
                e.Row.IsEnabled =false;
        }
    

    【讨论】:

    • 嗨,我尝试了该代码,但出现了错误:错误 CS1061:“System.Windows.Controls.DataGridRow”不包含“IsNewItem”的定义,并且没有扩展方法“IsNewItem”接受可以找到“System.Windows.Controls.DataGridRow”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
    • @moez DataGridRow.IsNewItem 属性在 .NET 版本 Properties中将Target Framework设置为.NET Framework 4.5
    【解决方案2】:

    我一直在使用不是 wpf 的 windows 窗体工作,并且对 C# 相当陌生,所以可能有更好的方法来做到这一点。

    我遇到了类似的问题,为了解决这个问题,我将表格设置为非只读,然后遍历所有行并将它们分别设置为只读,最后一行除外。不是最漂亮的,但它确实有效。

    foreach (DataRow dataRow in dataGridView.Rows)
    {
        dataRow.ReadOnly = true;
    }
    
    dataGridView.Rows[dataGridView.Rows.Count - 1].ReadOnly = false;
    

    【讨论】:

    • 您能告诉我您是如何将它们设置为 ReadOnly 您使用了哪个属性吗?
    • 我为它添加了代码。就像我说的,虽然我没有使用 wpf 的经验,所以我不确定是否有任何差异。
    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2014-07-23
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多