如何使用多个表适配器查询中的行正确填充 DataGridView?

发布于 2024-10-10 23:39:42 字数 983 浏览 6 评论 0原文

我正在执行一个临时查询来获取一组 ID,每个 ID 都进入表适配器的查询,填充行。

但是,除了使用表适配器上的 ClearBeforeFill 属性之外,我想不出任何其他方法来清除。

所有这些都相当于代码可以工作,但我觉得有点像黑客。

有谁知道正确/更好的方法来做到这一点? (注意:我知道内联 SQL 并不理想)

    var db = new Data.PRDataDataContext();
    verifiedLineItemsTableAdapter.ClearBeforeFill = true;
    bool flag = true;
    foreach (var affid in db.ExecuteQuery<int>(
        @"
        SELECT Item.affid           
        FROM   dbo.Item INNER JOIN
               dbo.Affiliate ON dbo.Item.affid = dbo.Affiliate.affid
        WHERE  Affiliate.name={0}
        UNION
        SELECT affid
        FROM   dbo.Publisher
        WHERE  name={0}"
        , name))
    {
        Console.WriteLine("got " + affid);
        verifiedLineItemsTableAdapter.FillByAffId(
            publisherReportDataSet1.VerifiedLineItems, affid);
        if (flag)
        {
            verifiedLineItemsTableAdapter.ClearBeforeFill = false;
            flag = false;
        }
    }

I'm doin an ad-hoc query to get a set of ID's, each of which goes to a table adapter's query, populating rows.

However, I can't figure out any other way to clear other than using the ClearBeforeFill property on the table adapter.

All of this amounts to code that works but I feel is kind of a hack.

Does anyone know the right/better way to do this? (note: i'm aware inline SQL isn't ideal)

    var db = new Data.PRDataDataContext();
    verifiedLineItemsTableAdapter.ClearBeforeFill = true;
    bool flag = true;
    foreach (var affid in db.ExecuteQuery<int>(
        @"
        SELECT Item.affid           
        FROM   dbo.Item INNER JOIN
               dbo.Affiliate ON dbo.Item.affid = dbo.Affiliate.affid
        WHERE  Affiliate.name={0}
        UNION
        SELECT affid
        FROM   dbo.Publisher
        WHERE  name={0}"
        , name))
    {
        Console.WriteLine("got " + affid);
        verifiedLineItemsTableAdapter.FillByAffId(
            publisherReportDataSet1.VerifiedLineItems, affid);
        if (flag)
        {
            verifiedLineItemsTableAdapter.ClearBeforeFill = false;
            flag = false;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2024-10-17 23:39:42

我会将其分为 2 个独立的函数,然后将其连接起来

与此类似

List<int> getItemAffid(string Name) {....//your code here to return a list}

List<int> getPublisherAffid(string Name) {....//your code here to return a list} 

,然后将它们连接起来

var SearchItemAffid = from a in getItemAffid(<your var>) select a;
var SearchedPublisherAffid = from a in getPublisherAffid(<your var>) select a;

var Combined = SearchedPublisherAffid.Union(SearchItemAffid);

编辑: 然后您可以将其绑定到网格

DataGrid.Datasource = Combined

I would divide it into 2 separate functions and join the up afterward

Similar to this

List<int> getItemAffid(string Name) {....//your code here to return a list}

List<int> getPublisherAffid(string Name) {....//your code here to return a list} 

and then join them up

var SearchItemAffid = from a in getItemAffid(<your var>) select a;
var SearchedPublisherAffid = from a in getPublisherAffid(<your var>) select a;

var Combined = SearchedPublisherAffid.Union(SearchItemAffid);

EDIT : you can then bind it to a grid

DataGrid.Datasource = Combined
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文