C#Visual Studio:如何使用LINQ从DataGridView编辑更新数据库? [英] C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ?
问题描述
我看到其他示例加载和/或编辑DataGridView的数据,但这些示例不是使用LINQ。
我的表单加载代码:
public partial class EditImage:Form
{
SQL sqlLink = new SQL();
CustomMessageBox msg = new CustomMessageBox();
public EditImage()
{
InitializeComponent();
}
private void EditImage_Load(object sender,EventArgs e)
{
dgImageView.DataSource = sqlLink.getImageList();
dgImageView.AutoResizeColumns();
dgImageView.AutoResizeRows();
}
private void dgImageView_CellValueChanged(object sender,DataGridViewCellEventArgs e)
{
int rid = e.RowIndex;
int dbrid = rid + 1;
int cid = e.ColumnIndex;
string change = dgImageView [cid,rid] .Value.ToString();
string columname = dgImageView [cid,rid] .OwningColumn.Name;
使用(var dc = new DbLink())
{
var site =(from img in dc.images其中img.id == dbrid select img).SingleOrDefault( );
if(site!= null)
{
try
{
site。 =变化
dc.SubmitChanges();
dgImageView.EndEdit();
}
catch(Exception ex)
{
msg.Text(Error,ex.ToString(),MessageBoxButtons.OK);
}
}
}
}
代码从SQL类获取DataSource:
public IQueryable getImageList()
{
return selectImageList();
}
和
private IQueryable selectImageList()
{
DbLink dbl = new DbLink();
图片tl = new image();
var results =从dbl.images中的imgs
选择imgs;
返回结果;
}
dbml文件的屏幕截图:
你可以尝试反思: / p>
var changedColumn = typeof(image).GetProperty(columnName);
changedColumn.SetValue(site,change);
I have a form WinForms form window with a DataGridView. It pulls data from a SQL Server Express database using Linq and displays it in the DataGridView columns properly. I have it set in the properties to allow editing. When I edit and change a field during runtime it will only update the database if I specify one column name. This doesn't work well because I need it to update the column I edit, not the one hard coded. It needs to be dynamic. See the comments next to site.??? I can choose a column name manually from site.whatever but that's not what I want. I want to be able to do Site.columnname from the string I set.
I see other examples loading and/or editing a DataGridView with data, but these examples are not using LINQ.
My form load code:
public partial class EditImage : Form
{
SQL sqlLink = new SQL();
CustomMessageBox msg = new CustomMessageBox();
public EditImage()
{
InitializeComponent();
}
private void EditImage_Load(object sender, EventArgs e)
{
dgImageView.DataSource = sqlLink.getImageList();
dgImageView.AutoResizeColumns();
dgImageView.AutoResizeRows();
}
private void dgImageView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int rid = e.RowIndex;
int dbrid = rid + 1;
int cid = e.ColumnIndex;
string change = dgImageView[cid, rid].Value.ToString();
string columname = dgImageView[cid, rid].OwningColumn.Name;
using (var dc = new DbLink())
{
var site = (from img in dc.images where img.id == dbrid select img).SingleOrDefault();
if (site != null)
{
try
{
site.??? = change;
dc.SubmitChanges();
dgImageView.EndEdit();
}
catch (Exception ex)
{
msg.Text("Error", ex.ToString(), MessageBoxButtons.OK);
}
}
}
}
Code from SQL class to retrieve DataSource:
public IQueryable getImageList()
{
return selectImageList();
}
and
private IQueryable selectImageList()
{
DbLink dbl = new DbLink();
image tl = new image();
var results = from imgs in dbl.images
select imgs;
return results;
}
A screenshot of the dbml file:
You could try with reflection:
var changedColumn = typeof(image).GetProperty(columnName);
changedColumn.SetValue(site,change);
这篇关于C#Visual Studio:如何使用LINQ从DataGridView编辑更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!