在 WPF DataGrid 中手动添加行
我有以下 XAML 代码:
<sdk:DataGrid Margin="58,8,52,18" Name="dataGridTickets">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="ticketNoColumn" Header="Ticket No." IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatRowColumn" Header="Seat Row" IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatNumberColumn" Header="Seat Number" IsReadOnly="True" Width="SizeToHeader"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
我想以编程方式将手动数据输入到网格中,如何才能做到这一点?
谢谢
工作解决方案
I have the following XAML Code:
<sdk:DataGrid Margin="58,8,52,18" Name="dataGridTickets">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="ticketNoColumn" Header="Ticket No." IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatRowColumn" Header="Seat Row" IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatNumberColumn" Header="Seat Number" IsReadOnly="True" Width="SizeToHeader"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
I would like to enter manual data into the grid programatically, how can I manage to do this?
Thanks
Working Solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不向网格添加行。
结果:新行显示在网格中。
You don't add rows to a grid.
Result: new rows show up in the grid.
如果您不想对数据网格进行数据绑定(即使在运行时),您可以按照这篇文章中的建议进行操作:
以编程方式添加列和rows to WPF Datagrid
基本上,您创建一个新行(在代码中)并用项目填充它,然后将其分配给您的网格。
但正如亨克指出的那样,这并不是一个很好的做法。如果这是一次性情况,可能有其合理性,但一般来说,您应该通过更新底层数据源来解决它。以下是 Microsoft 的示例:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/9b96a798-e185-4d90-ba73-afc35eb91643
If you don't want to databind the datagrid (even at runtime), you can follow the advice in this SO article:
programmatically add column & rows to WPF Datagrid
Basically you create a new row (in code) and populate it with items and then assign it to your grid.
Like Henk pointed out though, it isn't a great practice. If this is a one-off situation, there may be justification for it but in general you should approach it by updating the underlying data source. Here is an example from Microsoft:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/9b96a798-e185-4d90-ba73-afc35eb91643