网格视图和下拉列表

发布于 2024-08-10 23:05:45 字数 157 浏览 10 评论 0原文

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

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

任何帮助将不胜感激

谢谢

Is it possible to change the data source of a dropdown list in a gridview from another dropdown list selected index changed method in the same gridview?

for example I have a dropdown that needs to change its contents depending on what is chosen in the previous cell of the gridview, which is also a dropdown list.

Any Help would be much appreciated

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2024-08-17 23:05:45

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

有关如何实现此目的的示例,请参见此处

在本文中,作者挂钩 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

Instead of changing the DataSource when the 1st DropDownList.SelectedIndex changes, you could set the DataSource of the 2nd DropDownList when it is being edited.

An example of how this can be achieved can be found here.

In this article, the author hooks to the EditingControlShowing event in order to change the type of the ComboBox. This can be easily modified to change the DataSource instead:

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
毁虫ゝ 2024-08-17 23:05:45

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

出于本示例的目的,添加一个具有两个 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 组合值更改时将触发此事件,并分配适当的数据我们的天组合的来源:

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

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

Here is another way how I would do this, by example: Two columns (Types, Days), if the user drops-down and chooses 'week', a second combo populates with week days, otherwise, weekends.

For the purpose of this example, add a grid (DataGridView1) with two ComboBoxCell columns and let the first column have these items: week, weekend.

This class will be our data source:

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

This function will return our data source, based on the key which can be 'week' or '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

And lastly, this event will fire when the Type combo value changes, and assign the appropriate data source to our days combo:

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

Note that this event fires after the cell is validated, ie once you tab off the cell.

家住魔仙堡 2024-08-17 23:05:45

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

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

It is totally possible. How are populating your dropdownlists ? If all the data is dynamic then you will have to rebuild the entire grid everytime you change the dropdownlist selected item.

If I am not wrong , you are trying to apply Filter mechanism. Are you ? Another way I have done in the past is to build my data source for DropDownList from the rows of GridView. Think about the data that you already have on the screen. Once you are in PreRender Function you can bind needed data in your dropdownlist, this way you will cut out load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文