1

グリッドビュー内のドロップダウン リストのデータ ソースを、同じグリッドビュー内の別のドロップダウン リスト選択インデックス変更メソッドから変更することは可能ですか?

たとえば、グリッドビューの前のセルで選択された内容に応じて内容を変更する必要があるドロップダウンがあります。これはドロップダウン リストでもあります。

どんな助けでも大歓迎です

ありがとう

4

3 に答える 3

1

DataSource1 番目の変更時に を変更する代わりに、編集中の 2 番目DropDownList.SelectedIndexの を設定できます。DataSourceDropDownList

これを実現する方法の例は、ここにあります。

この記事では、作成者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
于 2009-11-16T20:10:12.820 に答える
0

これを行う別の方法を例に示します。2つの列(タイプ、日)。ユーザーがドロップダウンして「週」を選択すると、2番目のコンボに平日が表示されます。それ以外の場合は週末が表示されます。

この例では、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

このイベントは、セルが検証された後、つまりセルをタブオフした後に発生することに注意してください。

于 2009-11-25T14:15:46.187 に答える
0

それは完全に可能です。ドロップダウンリストはどのように作成されていますか? すべてのデータが動的である場合は、ドロップダウン リストの選択項目を変更するたびに、グリッド全体を再構築する必要があります。

私が間違っていなければ、フィルターメカニズムを適用しようとしています。あなたは ?過去に行った別の方法は、GridView の行から DropDownList のデータ ソースを作成することです。すでに画面に表示されているデータについて考えてみてください。PreRender Function に入ると、必要なデータをドロップダウン リストにバインドできます。この方法で負荷を削減できます。

于 2009-12-15T20:38:23.443 に答える