C# 仅将一个DataGrid行设置为可编程编辑
我正在用数据库中的信息填充我的数据网格。信息似乎很好。我想把所有内容都保持为只读,少读一列。我不希望该列标题可编辑。只有下面的几行。例如,如下图所示,我希望能够编辑列参与者行内容5、5、5和3 我看不到任何选项可以单独选择要设置为只读的行或列。我只看到将整个DataGrid设置为只读的选项。我试图循环并尝试将只读设置为所需的列,但无法。请给我一些建议。谢谢 看到了一些示例,其中您可以通过XAML单独声明哪一列是只读的,但这些都是预先确定的数据的手动输入。我正在尝试使用自动生成的列填充DataGrid,数据来自数据库,我希望以编程方式完成C# 仅将一个DataGrid行设置为可编程编辑,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我正在用数据库中的信息填充我的数据网格。信息似乎很好。我想把所有内容都保持为只读,少读一列。我不希望该列标题可编辑。只有下面的几行。例如,如下图所示,我希望能够编辑列参与者行内容5、5、5和3 我看不到任何选项可以单独选择要设置为只读的行或列。我只看到将整个DataGrid设置为只读的选项。我试图循环并尝试将只读设置为所需的列,但无法。请给我一些建议。谢谢 看到了一些示例,其中您可以通过XAML单独声明哪一列是只读的,但这些都是预先确定的数据的手动输入。我正在尝试使用自动生成的列填充DataG
Connection connection = new Connection();
public static DataTable dt = new DataTable();
private void FillTable(DataGrid dataGrid)
{
//Connecting to database
connection.ConnectToDB(table, dt);
dataGrid.ItemsSource = dt.DefaultView;
foreach (DataGridColumn column in dataGrid1.Columns)
{
if (column.DisplayIndex == 1)
{
dataGrid1.IsReadOnly = true;
}
}
//dataGrid1.IsReadOnly = true;
}
XAML:
将
AutoGenerateColumns
-属性设置为false,这样就可以在视觉设计中提供所需的属性
蒙皮的小示例:
<GroupBox Grid.Row="2" Header="Items" Padding="5">
<GroupBox.Resources>
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#C2E58F" />
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#C2E58F" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="BorderThickness" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Setter Property="BorderThickness" Value="0" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="AlternationCount" Value="2" />
<Setter Property="AlternatingRowBackground" Value="LightGray" />
<Setter Property="CanUserAddRows" Value="False" />
<Setter Property="CanUserDeleteRows" Value="False" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="SelectionUnit" Value="FullRow" />
<Setter Property="FocusManager.IsFocusScope" Value="True" />
</Style>
</GroupBox.Resources>
<DataGrid ItemsSource="{Binding NameNotes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Firstname" Width="Auto" SortMemberPath="Firstname">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Firstname}" VerticalAlignment="Center" Margin="3,1"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Firstname}" VerticalAlignment="Center" Margin="3,1"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Lastname" Width="Auto" SortMemberPath="Lastname">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Lastname}" VerticalAlignment="Center" Margin="3,1"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Note" Width="*" SortMemberPath="Note">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Note}" VerticalAlignment="Center" Margin="3,1" TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
要编辑一列,必须为此列提供
CellEditingTemplate
。在我的示例中,我对Firstname列执行了此操作。如果列是自动生成的,则处理DataGrid.AutoGeneratingColumn
private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName != "Participants")
{ e.Column.IsReadOnly = true; }
}
并更改FillTable
方法:
private void FillTable(DataGrid dataGrid)
{
connection.ConnectToDB(table, dt);
dataGrid.ItemsSource = dt.DefaultView;
}
列是自动生成的吗?谢谢,但我尝试从我的类文件中以编程方式生成,最好不使用XAML。尝试了你的方法。每一列最后都是可编辑的,当我尝试编辑参与者行内容并按enter键时,屏幕冻结。编辑并添加了我的数据网格XAML行(如果有帮助)。删除
dataGrid1.IsReadOnly=true代码>来自您的代码。是的,我正在使用您的代码和已编辑的FillTable方法,如您所示。轻微更正。屏幕冻结是我的错。我输入了错误的值。错误是-我可以编辑所有内容,而不仅仅是参与者列。
private void FillTable(DataGrid dataGrid)
{
connection.ConnectToDB(table, dt);
dataGrid.ItemsSource = dt.DefaultView;
}