2016-10-07 103 views
1

我真的做節目,我已經建立了一些代碼,這樣如何使用DataGridView的組合框不使用Windows窗體改變數據源

  1. 寫文本框中輸入網站網址,然後點擊開始按鈕來篩選,匹配數據顯示在DataGridViews中。

  2. 我有一個6 DataGridViews。在首先的DataGridView,匹配的數據顯示(步驟1) ,然後,其他5個DataGridviews將顯示像級聯指首先的DataGridView row`s值(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 DB]

  2. 如果我選擇[查看超過100GB的數據庫]選項,則DataGridView只顯示匹配的行。

  3. 這意味着,我想用組合框選擇過濾的DataGridView和數據源已經

    綁定,所以我想不改變數據源....

我試圖找到組合框和DataGridView的,但通常與對組合框在DataGridView中....

和我如何能得到獲得第一DataGridViews`s值(webApplicationName)

請有人幫助我。我不有任何想法...

感謝

+0

可能重複o f [過濾DataGrid動態名稱與TextBox](http://stackoverflow.com/questions/34221657/filter-datagrid-for-name-with-textbox-dynamically) –

回答

2

您可以使用BindingSource的數據源的綁定到你的DataGridView。 這樣,您只需更改bindingSource的Filter屬性,並且篩選器將自動應用於網格中顯示的數據。 無需更改數據源。

+0

謝謝你的回覆Hassan Eskandari。 –

+0

但我用Dataview RowFilter lkie這個 –

+0

dvLists.RowFilter = string.Format(「ListLastModifyDate <= listModifiedDate」,mtCbListsSearchConditions.SelectedItem。的ToString()); a –

0

我建了一個代碼,並且效果很好

  1. 使用組合框SelectedItemChanged事件

  2. 使用數據視圖和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; 
      } 
     } 
相關問題