C#Visual Studio:如何使用LINQ从DataGridView编辑更新数据库? [英] C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ?

查看:462
本文介绍了C#Visual Studio:如何使用LINQ从DataGridView编辑更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DataGridView的窗体WinForms窗体窗体。它使用Linq从SQL Server Express数据库中提取数据,并将其正确显示在DataGridView列中。我在属性中设置允许编辑。当我在运行期间编辑和更改一个字段时,如果我指定一个列名称,它将只更新数据库。这不行,因为我需要它更新我编辑的列,而不是一个硬编码。它需要动态。查看网站旁边的评论。我可以从site.watever手动选择列名称,但这不是我想要的。我想要能够从我设置的字符串中执行Site.columnname。



我看到其他示例加载和/或编辑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屋!

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