如何在不更改数据源的情况下使用组合框过滤 Datagridview [英] How to filter Datagridview using combobox without changing datasource

查看:22
本文介绍了如何在不更改数据源的情况下使用组合框过滤 Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows 窗体制作一个程序,并且我已经构建了一些代码,就像这样

I`m making a program using Windows forms and I already built some codes, like this

  1. 在文本框中写入站点 url,然后单击开始按钮,匹配的数据显示在 DataGridViews 中.

  1. Write a site url in Text Box then click Start Button, matched data showed in DataGridViews.

我有 6 个 DataGridView.在第一个 DataGridView 中,显示匹配的数据(步骤 1)然后,其他 5 个 DataGridviews 将显示为层叠引用第一个 DataGridView 行的值(webApplicationName)

I have a 6 DataGridViews. In First DataGridView, matched data showed(step 1) and then, other 5 DataGridviews will showed like cascade refer First DataGridView row`s value(webApplicationName)

代码如下

private void mtbtnStart_Click(object sender, EventArgs e)
    {
        string webApplicationName = string.Empty;
        string siteID = string.Empty;
        mtlblError.Text = "No record returned.";

        Migration_Status_DAC dac = new Migration_Status_DAC();
        DataSet ds = new DataSet();

        //Web Application           
        ds = dac.SelectWebApplicationStatus(mtextUrl.Text);
        DataTable dt = ds.Tables[0];
        

        if (ds != null && dt != null && dt.Rows.Count > 0)
        {
            webApplicationName = dt.Rows[0]["AppName"].ToString();
            mgrdWebApplication.DataSource = dt;

            //Content Database
            ds = dac.SelectContentDatabaseStatus(webApplicationName);
            DataTable dtContent = ds.Tables[0];
            if (ds != null && dtContent != null && dtContent.Rows.Count > 0)
            {
                mtlblError.Visible = false;
                mgrdContentDatabase.DataSource = dtContent;

                //SiteCollection
                ds = dac.SelectSiteCollectionStatus(webApplicationName);
                DataTable dtSiteCol = ds.Tables[0];
                if (ds != null && dtSiteCol != null && dtSiteCol.Rows.Count > 0)
                {
                    mgrdSiteCollections.DataSource = dtSiteCol;

                    //Sites
                    ds = dac.SelectSitesStatus(webApplicationName);
                    DataTable dtSites = ds.Tables[0];
                    if (ds != null && dtSites != null && dtSites.Rows.Count > 0)
                    {
                        siteID = dtSites.Rows[0]["SiteID"].ToString();
                        mgrdSites.DataSource = dtSites;

                        //Lists
                        ds = dac.SelectListsStatus(siteID);
                        DataTable dtLists = ds.Tables[0];
                        if (ds != null && dtLists != null && dtLists.Rows.Count > 0) 
                        {
                            mgrdLists.DataSource = dtLists;
                        }
                        //Document Library
                        ds = dac.SelectDocumentLibraryStatus(siteID);
                        DataTable dtDocLib = ds.Tables[0];
                        if (ds != null && dtDocLib != null && dtDocLib.Rows.Count > 0)
                        {
                            mgridDocumentLibrary.DataSource = dtDocLib;
                        }
                    }
                    else
                    {
                        mtlblError.Visible = true;
                    }
                }
                else
                {
                    mtlblError.Visible = true;
                }
            }
            else
            {
                mtlblError.Visible = true;
            }

        }
        else
        {
            mgrdWebApplication.DataSource = null;
            mgrdContentDatabase.DataSource = null;
            mgrdSiteCollections.DataSource = null;
            mgrdSites.DataSource = null;
            mgrdLists.DataSource = null;
            mgridDocumentLibrary.DataSource = null;
        }

    }

现在我想添加这个

  1. 添加组合框并添加一些条件,例如[显示全部]和[查看超过100GB的数据库]

  1. Add Combobox and Add some conditions, for example, [Show all] and [See Over 100GB DB]

如果我选择 [See Over 100GB DB] 选项,在 DataGridView 中只显示匹配的行.

If I choose [See Over 100GB DB] option, in that DataGridView showed only matched rows.

这意味着,我想使用组合框选择来过滤 Datagridview 并且数据源已经是

It means, I want to filter Datagridview using Combobox Selection and Datasource is already

绑定,所以我不想改变数据源....

binded, so I want to do not change DataSource....

我试图找到组合框和 DataGridView,但通常与 DataGridView 中的组合框相关....

I try to find combobox and DataGridView, but usually related on Combobox in DataGridView....

我怎样才能得到第一个 DataGridViews 的值(webApplicationName)

And how can I get get first DataGridViews`s value(webApplicationName)

请有人帮助我..我不知道...

Please somebody help me.. I don`t have any idea...

谢谢

推荐答案

您可以使用 BindingSource 将数据源绑定到 DataGridView.这样您只需更改 bindingSource 的 Filter 属性,过滤器将自动应用于网格中显示的数据.无需更改数据源.

You can use a BindingSource to bind your datasource to your DataGridView. This way you only change the Filter property of your bindingSource and the filter automatically will apply to data shown in the grid. No need to change datasource.

这篇关于如何在不更改数据源的情况下使用组合框过滤 Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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