如何使用C#在wpf中插入和更新datagrid行 [英] How I can Insert and Update datagrid row in wpf using C#

查看:191
本文介绍了如何使用C#在wpf中插入和更新datagrid行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发wpf应用程序而不使用任何framework.until现在我成功地在Datagrid上执行了以下任务。

1.通过绑定数据库表来显示数据网格中的数据

2.根据主键从数据网格中获取所选行数据



这是我的数据网格的代码

 <   DataGrid     x:名称  =  dtgsecutity      Horizo​​ntalAlignment   =    高度  =  168   保证金  =  614,513, 0,0    ItemsSource   =  {Binding Path = Security_details}    VerticalAlignment   =  Top   < span class =code-attribute>宽度  =  344 >  
< DataGrid.Columns < span class =code-keyword>>
< DataGridTextColumn 宽度 = 30 标题 = S \ No Binding = {Binding Path = id,Mode = TwoWay} / >
< DataGridTextColumn 宽度 = 45 标题 = 代码 绑定 = {Binding Path = Code,Mode = TwoWay} < span class =code-attribute> / >
< DataGridTextColumn 宽度 = 80 标题 = 描述 绑定 = {Binding Path = Description,Mode = TwoWay} / >
< DataGridTextColumn 宽度 = 40 标题 = 评价 < span class =code-attribute> Binding = {Binding Path = Rate,Mode = TwoWay} / >
< DataGridTextColumn < span class =code-attribute>宽度 = 30 标题 = 数量 绑定 = {Binding Path = Qty,Mode = TwoWay} / >
< DataGridTextColumn 宽度 = 60 标题 = 金额 < span class =code-attribute> Binding = {Binding Path = Amount,Mode = TwoWay} / >
< DataGridTextColumn 宽度 = 60 标题 = 备注 绑定 = {Binding Path =备注,Mode = TwoWay} / >
< / DataGrid.Columns >
< / DataGrid >



现在我对这个数据网格的担忧是:



当数据网格中没有加载db表时:



如果我的数据网格是用db表卸载的,那么它不是编辑。如何通过使其可编辑来创建行?以及如何实用地检索所有那些新创建的行以进行插入命令?



当表格加载到数据网格中时:



1.如果我的数据网格加载了db表,并且我尝试添加新行,则它不会以垂直方式显示新行,而是以水平方式显示新行。我如何设置数据网格以垂直方式添加新行?

2.如何我可以实际检索所有这些多个更新的行以获取更新命令?



请帮助我完成此任务而不使用任何框架。谢谢。

解决方案

< datagridtextcolumn width =30header =S \ Nobinding ={Binding Path = id,Mode = TwoWay}>



如果id是主键,模式应该是单向的;除了它应该在你的数据源Security_details上清楚地注明。



确保你有主键,系统知道来自数据源Security_details。

I am developing wpf application using C# without using any framework.until now i successfully performed following task on Datagrid.
1.Displaying data in Data-grid by binding with database table
2.Getting the selected row data from Data-grid based on primary key

Here is code for my Data-grid

<DataGrid x:Name="dtgsecutity"  HorizontalAlignment="Left" Height="168" Margin="614,513,0,0" ItemsSource="{Binding Path=Security_details}" VerticalAlignment="Top" Width="344">
   <DataGrid.Columns>
   <DataGridTextColumn Width="30" Header="S\No" Binding="{Binding Path=id, Mode=TwoWay}" />
   <DataGridTextColumn Width="45" Header="Code" Binding="{Binding Path=Code, Mode=TwoWay}" />
   <DataGridTextColumn Width="80" Header="Description" Binding="{Binding Path=Description, Mode=TwoWay}" />
   <DataGridTextColumn Width="40" Header="Rate" Binding="{Binding Path=Rate, Mode=TwoWay}" />
   <DataGridTextColumn Width="30" Header="Qty" Binding="{Binding Path=Qty, Mode=TwoWay}" />
   <DataGridTextColumn Width="60" Header="Amount" Binding="{Binding Path=Amount, Mode=TwoWay}" />
   <DataGridTextColumn Width="60" Header="Remarks" Binding="{Binding Path=Remarks, Mode=TwoWay}" />
   </DataGrid.Columns>
   </DataGrid>


Now my concerns with this data-grid are:

When No db table is loaded in data-grid :

If my data-grid is unloaded with db tables it is not editable. How i can create row by making it editable? And How i can pragmatically retrieve all those multiple newly created rows for insert command?

When table is loaded in data-grid :

1. If my data-grid is loaded with db tables and i try to add new rows it is not showing new rows in vertical manner rather it shows new rows in horizontal manner. How i can set data-grid to add new row to in vertical manner?
2. And How i can pragmatically retrieve all those multiple updated rows for update command?

Please help me for accomplishing this task without using any framework.Thanks .

解决方案

<datagridtextcolumn width="30" header="S\No" binding="{Binding Path=id, Mode=TwoWay}">

If id is the primary key, the mode should be one way; besides it should be clearly noted on your data source "Security_details".

Make sure you have primary key and the system knows that from the data source "Security_details" .


这篇关于如何使用C#在wpf中插入和更新datagrid行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆