使用Visual Studio和VB.net我有一个填充了数据的gridview,并根据下拉列表中的文本值,我想隐藏某些列并取消隐藏。
下拉列表通过sql填充主题列表(英语,数学科学等...)
网格包含三列KS2英语,KS2数学和KS2平均值的列。
从下拉列表中选择英语时,我想隐藏KS2数学和KS2平均列。
选择数学时,我想隐藏KS2英语和KS2平均列。
最后,如果选择任何其他主题,我想隐藏KS2英语和KS2数学专栏。
我已经根据下拉菜单中的主题更新了gridview ok,但是我不知道我需要做什么才能开始根据选择开始具体了解哪些列。< / p>
这是一个屏幕截图,应该清楚我到目前为止的内容:
答案 0 :(得分: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
答案 1 :(得分: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
我的下拉选择程序中的以下select 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