wpf 将新的 Datagrid Row 保存到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18434070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save a new Datagrid Row to database
提问by Joe K
Working in C#WPF. Ok so I have a datagridwhich is bound to an entity frameworkin my project. The datagrid functions awesome when updating values which are displayed. I push the changes to the databaseusing MyDataEntity.SaveChanges()and everything works perfectly.
在C#WPF. 好的,所以我有一个datagrid绑定到entity framework我的项目中的一个。更新显示的值时,数据网格功能很棒。我将更改推送到database使用, MyDataEntity.SaveChanges()并且一切正常。
The issue comes up when adding a new row. I've set CanUserAddRows="True"in my XAML, so the user is able to add new rows. The problem comes when i have to save the new rows to the database. For some reason, the SaveChanges()call doesn't update the database. What I tried to do was pull all the values of the current row into an IListand then access the individual values so I could call MyDataEntities.Add()' on theRowEditEnding` event, but that made some other problems. Mainly, I cant access the individual values in the IList. when I look at the data in the list, it is in there, i have seen it, i just cant assign it to anything.
添加新行时出现问题。我已经CanUserAddRows="True"在 XAML 中进行了设置,因此用户可以添加新行。当我必须将新行保存到数据库时,问题就出现了。出于某种原因,该SaveChanges()调用不会更新数据库。我试图做的是将当前行的所有值拉入一个IList,然后访问各个值,这样我就可以调用MyDataEntities.Add()' on theRowEditEnding` 事件,但这产生了一些其他问题。主要是,我无法访问 IList 中的各个值。当我查看列表中的数据时,它就在那里,我已经看到了,但我无法将其分配给任何东西。
So really, my problem is two-fold.
所以真的,我的问题有两个方面。
1: How can I save the newly entered row to my database
1:如何将新输入的行保存到我的数据库中
2: How can I access the values in the IList
2:如何访问 IList 中的值
2 would be a better solution, because this way i could use the following code to fill additional columns in the database as well as pass data to other methods to get values for specific columns in the database.
2 将是更好的解决方案,因为这样我可以使用以下代码填充数据库中的其他列,并将数据传递给其他方法以获取数据库中特定列的值。
private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
IList rows = DataGrid.SelectedItems;
TableName tbl = new TableName
{
Name = rows[1].ToString(),
Qty = decimal.Parse(rows[4].ToString()),
Type = rows[6].ToString(),
TQty = ConvertQtoT(rows[4].ToString(), rows[6].ToString(),
OnLIst = true
};
MyDataEntity.TableNames.Add(tbl);
MyDataEntity.TableNames.SaveChanges();
}
}
回答by Krish
You can save a newly entered row into database from datagrid as follows:
您可以将新输入的行从数据网格保存到数据库中,如下所示:
First assign datasource to datagrid:
首先将数据源分配给数据网格:
datagrid.ItemsSource = myview.GetList();
Next select the SelectionChanged Event of Datagrid:
接下来选择 Datagrid 的 SelectionChanged 事件:
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
objToAdd = datagrid.SelectedItem as clsName; //Entity Object
}
Next select CellEditEnding Event of Datagrid:
接下来选择 Datagrid 的 CellEditEnding 事件:
private void datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
try
{
FrameworkElement element1 = datagrid.Columns[0].GetCellContent(e.Row);
if (element1.GetType() == typeof(TextBox))
{
var colomn1 = ((TextBox)element1).Text;
objToAdd.Column1 = Convert.ToInt32(Column1);
}
FrameworkElement element2 = datagrid.Columns[1].GetCellContent(e.Row);
if (element2.GetType() == typeof(TextBox))
{
var colomn2 = ((TextBox)element2).Text;
objToAdd.Column2 = Convert.ToInt32(Column2);
}
FrameworkElement element3 = datagrid.Columns[2].GetCellContent(e.Row);
if (element3.GetType() == typeof(TextBox))
{
var colomn3 = ((TextBox)element3).Text;
objToAdd.Column3 = Convert.ToInt32(Column3);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Next select RowEditEnding Event of Datagrid:
接下来选择 Datagrid 的 RowEditEnding 事件:
private void datagrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
try
{
var Res = MessageBox.Show("Do you want to Create this new entry", "Confirm", MessageBoxButton.YesNo);
if (Res == MessageBoxResult.Yes)
{
EntityObject.InsertEmployee(objToAdd);
EntityObject.SaveChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Hope this will help you...
希望能帮到你...

