使用Ling to SQL的DatagridView的最简单方法

时间:2021-05-10 16:31:09

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.

我从来没有使用过datagrids等,但是今天我遇到了一个简单的问题并且决定“数据绑定”的东西来更快地完成它,但是我发现它不能像我期望的那样工作。

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)?

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

5 个解决方案

#1


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,是否有特定的原因?

#2


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

你可以看一下这篇文章中的TableList - 基于BindingList ,但是挂钩到数据上下文中。

#3


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 MergeEntry class you use your Customer class.

在BindingList旁边,它使用BindingSource。而不是MergeEntry类,您使用Customer类。

#4


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.

无需将其转换为列表。

#5


string = "12/11/2014"

string =“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
{
}

#1


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,是否有特定的原因?

#2


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

你可以看一下这篇文章中的TableList - 基于BindingList ,但是挂钩到数据上下文中。

#3


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 MergeEntry class you use your Customer class.

在BindingList旁边,它使用BindingSource。而不是MergeEntry类,您使用Customer类。

#4


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.

无需将其转换为列表。

#5


string = "12/11/2014"

string =“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
{
}