开发者

Gridviews and DropdownLists

Is it possible to change the data source of a dropdown list in a gridview from another dropdow开发者_如何学Pythonn 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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜