[英]save datagrid view changes to database entity framework
I am trying to bind datagridview with entity Framework and want to save datagridview changes back to database. 我正在尝试将datagridview与实体Framework绑定,并想将datagridview的更改保存回数据库。 but no success. 但没有成功。 i did some research found below code as sol. 我做了一些研究,发现下面的代码为sol。 but in my case it is not going to work. 但就我而言,它是行不通的。 the post i viewed almost 3 years old. 我查看了将近3岁的帖子。 may be the below logic is outdated. 可能是以下逻辑已过时。 any suggestion please. 任何建议。 i am using EF5. 我正在使用EF5。
AmzEntities amz;
public SellerSettings()
{
InitializeComponent();
}
private void SellerSettings_Load(object sender, EventArgs e)
{
amz = new AmzEntities();
AmzEntities context = new AmzEntities();
context.Sellers.Load();
dataGridView1.DataSource = context.Sellers.Local.ToBindingList(); ;
}
private void buttonSave_Click(object sender, EventArgs e)
{
amz.SaveChanges();
}
I don't know If you want to make an insertion or an update, 我不知道如果您要插入或更新,
for example if you want to save (insert) a sellers name, you would need to call the sellers class and point to the name in the class sellers and finally tell the DbContext to save it 例如,如果您想保存(插入)卖方名称,则需要调用卖方类并指向卖方类别中的名称,最后告诉DbContext保存它
AmzEntities context = new AmzEntities();
Sellers _sellers = new Sellers();
_sellers.name= "Jhon Abdullah";
context.Sellers.Add(_sellers);
context.SaveChanges();
update would be a little different, 更新会有所不同,
var updateQuery = (from sellers1 in context.Sellers
where Sellers.name== "Jhon Abdullah"
select sellers1).FirstOrDefault();
updateQuery.name = "Jack Abdullah";
ExEnt.SaveChanges();
to save de data from the datagridview you would need to find the data in the datagridview, can be done with the index and a GridViewRow, depend on your datagridview structure and where is it being accessed _rowCommand, etc. 要从datagridview中保存数据,您需要在datagridview中找到数据,可以使用索引和GridViewRow来完成,具体取决于您的datagridview结构以及在何处访问_rowCommand等。
Example in rowCommand rowCommand中的示例
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = dataGridView1.Rows[index];
Label lblName = (gvRow.FindControl("lbl_name") as Label);
AmzEntities context = new AmzEntities();
Sellers _sellers = new Sellers();
_sellers.name= lblName.Text;
context.Sellers.Add(_sellers);
context.SaveChanges();
good luck! 祝好运!
DataContext db = new DataContext();
public SupliersForm()
{
InitializeComponent();
supliersDG.DataSource = db.Supliers.Local.ToBindingList();
db.Supliers.Load();
}
private void SaveBtn_Click(object sender, EventArgs e)
{
supliersDG.EndEdit();
db.SaveChanges();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.