【问题标题】:Simplest way to use a DatagridView with Linq to SQL将 DatagridView 与 Linq to SQL 一起使用的最简单方法
【发布时间】:2009-06-16 11:06:46
【问题描述】:

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

我虽然通过做一些简单的事情来做到这一点:

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

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

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

有什么想法吗?

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

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

【问题讨论】:

    标签: c# .net linq-to-sql datagridview


    【解决方案1】:

    你可以只将IQueryable结果绑定到DataGridView,不知道为什么要转换成BindingList,有什么具体原因吗?

    【讨论】:

    • 提供两种方式的绑定以便DataGridView的变化可以被通知?
    • 好吧,没有理由。正如我所提到的,我对 DataGrids 是 100% 的新手,这是我在谷歌土地上找到的一个例子。 :S(但它不起作用)
    【解决方案2】:

    您可能会查看来自this postTableList&lt;T&gt; - 基于BindingList&lt;T&gt;,但与数据上下文挂钩。

    【讨论】:

    • 哦,我以前看过那个帖子(在其中一个标签中打开过),我才意识到这是你的帖子! :) 将重新阅读并询问我是否有问题。谢谢。
    【解决方案3】:

    试试这个,看看thedugas的回答:unable-to-edit-datagridview-populated-with-results-of-linq-query

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

    【讨论】:

      【解决方案4】:

      只需绑定它。

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

      无需将其转换为列表。

      【讨论】:

      • 这将不允许在 DataGridView 中进行编辑。他提到了“添加/编辑/修改”。
      【解决方案5】:

      字符串 = “2014 年 12 月 11 日”

      试试这个:

      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
      {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多