2009-11-16 54 views
1

是否有可能更改gridview中的下拉列表的數據源從另一個下拉列表中選擇索引更改方法在同一個gridview?Gridviews和DropdownLists

例如我有一個下拉菜單,需要改變其內容,這取決於在gridview的前一個單元格中選擇的內容,這也是一個下拉列表。

任何幫助,將不勝感激

感謝

回答

1

代替改變DataSource當第一DropDownList.SelectedIndex變化,你可以設置第二DropDownListDataSource時被編輯的。

可以通過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 
0

這裏是另一種方式,我會怎麼做,舉例:兩列(類型,天),如果用戶下降向下,並選擇「星期」 ,第二個組合填充週日,否則週末。

對於本示例的目的,添加一個帶有兩個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 

該函數將返回我們的數據源的基礎上,重點「周」或「週末」可以是:

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 

而且最後,當類型組合值發生變化時,此事件將觸發,並將適當的數據源分配給我們的天數組合:

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 

請注意,此事件在單元格驗證後觸發,即一旦您切斷單元格。

0

這是完全可能的。如何填充你的下拉列表?如果所有數據都是動態的,那麼每次更改下拉列表選定項時,都必須重新構建整個網格。

如果我沒有錯,您嘗試應用Filter機制。你是 ?過去我做的另一種方法是從GridView的行爲DropDownList構建我的數據源。想想你已經在屏幕上顯示的數據。一旦進入PreRender功能,您可以在您的下拉列表中綁定所需的數據,這樣您將切斷負載。