如何在不更改数据源的情况下使用组合框过滤数据视图

本文关键字:组合 过滤 视图 数据 情况下 数据源 | 更新日期: 2023-09-27 18:12:04

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

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

  2. 我有一个6 DataGridViews。在First DataGridView中,显示匹配的数据(步骤1)然后,其他5个DataGridView将显示像级联引用第一个DataGridView行值(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. 添加组合框并添加一些条件,例如[Show all]和[See Over 100GB DB]

  2. 如果我选择[查看超过100GB DB]选项,在DataGridView中只显示匹配的行。

  3. 这意味着,我想使用组合框选择和数据源过滤数据视图已经

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

我试着找到组合框和DataGridView,但通常在DataGridView的组合框相关....

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

谁来帮帮我…我不知道……

谢谢

如何在不更改数据源的情况下使用组合框过滤数据视图

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

我构建了一个代码,并且运行良好

  1. 使用ComboBox SelectedItemChanged事件

  2. 使用DataView和DataView。RowFilter

下面代码

        private void mtcbContentDBSearchCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataView dvContentDatabase = new DataView(dtContent);
            if (mtcbContentDBSearchCondition.SelectedItem.ToString() == "All")
            {
                mgrdContentDatabase.DataSource = dtContent;
            }
            else
            {
                dvContentDatabase.RowFilter = string.Format("ContentDBSize >= 300000000", mtcbContentDBSearchCondition.SelectedItem.ToString());
                mgrdContentDatabase.DataSource = dvContentDatabase;
            }
        }