【问题标题】:Gridviews and DropdownLists网格视图和下拉列表
【发布时间】:2009-11-16 13:54:34
【问题描述】:

是否可以从同一网格视图中的另一个下拉列表选择索引更改方法更改网格视图中下拉列表的数据源?

例如,我有一个下拉列表,需要根据在 gridview 的前一个单元格中选择的内容来更改其内容,这也是一个下拉列表。

任何帮助将不胜感激

谢谢

【问题讨论】:

    标签: vb.net gridview drop-down-menu selectedindex


    【解决方案1】:

    您可以在编辑第二个DropDownList 时设置第二个DropDownListDataSource,而不是在第一个DropDownList.SelectedIndex 更改时更改DataSource

    可以在here 找到如何实现这一点的示例。

    在本文中,作者挂钩EditingControlShowing 事件以更改ComboBox 的类型。这可以很容易地修改为更改DataSource

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
      ' make sure we are editing the 2nd ComboBox:'
      Dim comboBoxColumn2 As DataGridViewComboBoxColumn = DataGridView1.Columns(2)
      If (DataGridView1.CurrentCellAddress.X = comboBoxColumn2.DisplayIndex) Then
        'here you retrieve the value of the 1st ComboBox:'
        Dim comboBox1Value As object = DataGridView1.SelectedRow... 'fill with whatever is needed'
        Dim cb As ComboBox = e.Control
        If (cb IsNot Nothing) Then
          cb.DataSource = Nothing 'maybe not needed, I'm not sure
          cb.DataSource = 'here, set the data source based on the value of ComboBox1'
        End If
      End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      这是我执行此操作的另一种方式,例如:两列(类型、天),如果用户下拉并选择“星期”,则第二个组合填充工作日,否则填充周末。

      就本示例而言,添加一个包含两个 ComboBoxCell 列的网格 (DataGridView1),并让第一列包含以下项目:星期、周末。

      这个类将是我们的数据源:

      Class WeekDataItem
      
          Sub New(ByVal id As Integer, ByVal name As String)
              Me.ID = id
              Me.Name = name
          End Sub
      
          Public Property ID() As Integer
              Get
                  Return _ID
              End Get
              Set(ByVal value As Integer)
                  _ID = value
              End Set
          End Property
          Private _ID As Integer
      
          Public Property Name() As String
              Get
                  Return _Name
              End Get
              Set(ByVal value As String)
                  _Name = value
              End Set
          End Property
          Private _Name As String
      
      End Class
      

      此函数将根据键返回我们的数据源,键可以是 'week' 或 'weekend':

      Function getWeekDataSource(ByVal key As String) As List(Of WeekDataItem)
          getWeekDataSource = New List(Of WeekDataItem)
          If (key = "week") Then
              getWeekDataSource.Add(New WeekDataItem(1, "monday"))
              getWeekDataSource.Add(New WeekDataItem(2, "tuesday"))
              getWeekDataSource.Add(New WeekDataItem(3, "wednesday"))
              getWeekDataSource.Add(New WeekDataItem(4, "thrusday"))
              getWeekDataSource.Add(New WeekDataItem(5, "friday"))
          ElseIf (key = "weekend") Then
              getWeekDataSource.Add(New WeekDataItem(6, "caturday"))
              getWeekDataSource.Add(New WeekDataItem(7, "sunday"))
          End If
      End Function
      

      最后,当 Type 组合值发生变化时,该事件将触发,并将适当的数据源分配给我们的 days 组合:

      Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
              ' if the type dropdown value changed
              If (e.ColumnIndex = clmTypes.Index) Then
                  ' set the week dropdown data source for the current row
                  If Not IsNothing(DataGridView1.CurrentRow) Then
                      ' get the combobox cell we want to change
                      Dim comboCell As DataGridViewComboBoxCell
                      comboCell = CType(DataGridView1.CurrentRow.Cells(clmDays.Index), DataGridViewComboBoxCell)
                      ' assign it's new data source
                      comboCell.DataSource = getWeekDataSource(CStr(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value))
                      ' update the data source members so it displays info properly
                      comboCell.DisplayMember = "Name"
                      comboCell.ValueMember = "ID"
      
                  End If
              End If
          End Sub
      

      请注意,此事件在单元格被验证后触发,即一旦您关闭单元格。

      【讨论】:

        【解决方案3】:

        完全有可能。如何填充您的下拉列表?如果所有数据都是动态的,那么每次更改下拉列表选定项时都必须重建整个网格。

        如果我没记错,您正在尝试应用过滤器机制。你是 ?我过去做过的另一种方法是从 GridView 的行中为 DropDownList 构建我的数据源。想想你已经在屏幕上拥有的数据。进入 PreRender 函数后,您可以在下拉列表中绑定所需的数据,这样您就可以减少负载。

        【讨论】:

          猜你喜欢
          • 2016-05-06
          • 1970-01-01
          • 1970-01-01
          • 2014-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多