English 中文(简体)
Gridviews and DropdownLists
原标题:

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

问题回答

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.





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签