2013-04-10 38 views
0

特定的GridView列使用Visual Studio和VB.net我有一個gridview填充數據,並基於在下拉菜單文本值我想隱藏某些列並酌情取消隱藏。VB.Net隱藏基於下拉文本選擇

下拉通過SQL填充的對象列表(英語,數學科學等)

該網格包含列包括三列KS2英語,數學KS2和KS2平均。

當英語從下拉拿起我想隱藏的KS2數學和KS2平均列。

當數學是挑我想隱藏的KS2英語和KS2平均列。

最後,如果選擇了其他問題,我想隱藏的KS2英語和數學KS2列。

我已經填充了gridview確定的數據,根據下拉列表中的主題進行更新,但我不確定需要做什麼才能開始獲取有關基於選擇顯示的列的具體信息。

這裏是一個屏幕快照,應該讓我至今清楚什麼:

enter image description here

回答

1

試試這個代碼:

它用於加處理

使用
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 

     If TypeOf e.Control Is ComboBox Then 

      AddHandler CType(e.Control, ComboBox).SelectedIndexChanged, AddressOf LastColumnComboSelectionChanged 

     End If 

    End Sub 

其對於上可見的僞列

Private Sub LastColumnComboSelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) 

    DataGridView1.Columns(5).Visible = True 
    DataGridView1.Columns(4).Visible = True 
    DataGridView1.Columns(3).Visible = True 

    If sender.SelectedItem = "Maths" Then 
     DataGridView1.Columns(2).Visible = False 
     DataGridView1.Columns(4).Visible = False 
    ElseIf sender.SelectedItem = "English" Then 
     DataGridView1.Columns(3).Visible = False 
     DataGridView1.Columns(4).Visible = False 
    ElseIf sender.SelectedItem = "others" Then 
     DataGridView1.Columns(3).Visible = False 
     DataGridView1.Columns(4).Visible = False 
     DataGridView1.Columns(2).Visible = False 
    End If 

    End Sub 
+0

感謝這麼遠。我已經在我的項目中實現這一點,並在爲GridView,下拉菜單和列位置適當的控制所取代,但我得到的錯誤告知該類型'System.Windows.Forms.DataGridViewEditingControlShowingEventArgs'和'ComboBox'沒有定義。 – Matt 2013-04-10 13:11:52

+0

您是否在您的datagridview中添加了datagridviewcomboxcolumn。而你正在winforms或web – Sathish 2013-04-10 13:16:47

+0

謝謝@SATSON。我添加了一個截圖,希望能讓事情更清楚。 我在webform中有一個單獨的下拉列表和gridview控件。 – Matt 2013-04-10 13:44:28

1

我整理了它。寫在頁面加載以下步驟:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    GvStudentDetails.Columns(17).Visible = False 
    GvStudentDetails.Columns(18).Visible = False 
End Sub 

我的下拉列表中選擇過程中的以下選擇case語句:

Protected Sub DdlSubject_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DdlSubject.SelectedIndexChanged 
    Select Case strSubject 
     Case "English" 
      GvStudentDetails.Columns(16).Visible = True 
      GvStudentDetails.Columns(17).Visible = False 
      GvStudentDetails.Columns(18).Visible = False 
     Case "Mathematics" 
      GvStudentDetails.Columns(16).Visible = False 
      GvStudentDetails.Columns(17).Visible = True 
      GvStudentDetails.Columns(18).Visible = False 
     Case Else 
      GvStudentDetails.Columns(16).Visible = False 
      GvStudentDetails.Columns(17).Visible = False 
      GvStudentDetails.Columns(18).Visible = True 
    End Select 
End Sub