C# 将 DatagridView 与 Linq to SQL 一起使用的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1000801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:14:26  来源:igfitidea点击:

Simplest way to use a DatagridView with Linq to SQL

c#.netlinq-to-sqldatagridview

提问by Martin Marconcini

I have never used datagrids and such, but today I came across a simple problem and decided to "databind" stuff to finish this faster, however I've found that it doesn't work as I was expecting.

我从未使用过数据网格等,但今天我遇到了一个简单的问题,并决定“数据绑定”东西以更快地完成此操作,但是我发现它并没有像我预期的那样工作。

I though that by doing something as simple as:

我认为通过做一些简单的事情:

 var q = from cust in dc.Customers
         where cust.FirstName == someString
         select cust;

 var list = new BindingList<Customer>(q.ToList());
 return list;

Then using that list in a DataGridView1.DataSource was all that I needed, however, no matter how much I google, I can't find a decent example on how to populate (for add/edit/modify) the results of a single table query into a DataGridView1. Most samples talk about ASP.NET which I lack, this is WinForms.

然后在 DataGridView1.DataSource 中使用该列表就是我所需要的,但是,无论我谷歌多少,我都找不到关于如何填充(用于添加/编辑/修改)单个表结果的体面示例查询到 DataGridView1。大多数示例都讨论了我缺少的 ASP.NET,这是 WinForms。

Any ideas?

有任何想法吗?

I've came across other posts and the GetNewBindingList, but that doesn't seem to change much.

我遇到过其他帖子和 GetNewBindingList,但这似乎没有太大变化。

What am I missing (must be obvious)?

我错过了什么(必须很明显)?

回答by Lazarus

You can just bind the IQueryable result to the DataGridView, not sure why you converting it to a BindingList, is there a specific reason for that?

您可以将 IQueryable 结果绑定到 DataGridView,不确定为什么将其转换为 BindingList,是否有特定原因?

回答by Marc Gravell

You might have a look at the TableList<T>from this post- based on BindingList<T>, but with hooks into the data-context.

您可能会TableList<T>这篇文章中查看 - 基于BindingList<T>,但与数据上下文挂钩。

回答by Waheed

Just bind it.

绑定就行了

    var q = from cust in dc.Customers
            where cust.FirstName == someString
            select cust;
    DataGridView1.DataSource = q

No need to convert it to list.

无需将其转换为列表。

回答by Kay Zed

Try this, see the answer of thedugas: unable-to-edit-datagridview-populated-with-results-of-linq-query

试试这个,看 thedugas 的答案:无法编辑数据网格视图填充的结果与 linq 查询

Next to a BindingList, it uses a BindingSource. And instead of the MergeEntryclass you use your Customerclass.

在 BindingList 旁边,它使用 BindingSource。而不是MergeEntry你使用你的Customer班级。

回答by user3576855

string = "12/11/2014"

字符串 = "12/11/2014"

Try this:

尝试这个:

try
{
    con_a_refacionesDataContextDataContext con = new con_a_refacionesDataContextDataContext();
    BindingSource b = new BindingSource();
    b.DataSource = from eq in con.Venta 
                   where eq.fecha_dia == st 
                   select eq;
    dataGridView1.DataSource = b;
}
catch
{
}