DataGridViewComboBoxColumn的事件

小舖文 http://www.blueshop.com.tw/board/FUM20050124191756KKC/BRD20120620111736XS0.html
--------------------------------------------------------------------------------------

這個在 MSDN 上有參考資料,排版一下供參考 ^^

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditingcontrolshowingeventargs.control.aspx


Public Class Form1
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim comboBoxColumn As New DataGridViewComboBoxColumn()
        comboBoxColumn.Items.AddRange(Color.Red, Color.Yellow, Color.Green, Color.Blue)
        comboBoxColumn.ValueType = GetType(Color)
        DataGridView1.Columns.Add(comboBoxColumn)
    End Sub

    Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim combo As ComboBox = CType(e.Control, ComboBox)
        If (combo IsNot Nothing) Then

            ' Remove an existing event-handler, if present, to avoid
            ' adding multiple handlers when the editing control is reused.
            RemoveHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

            ' Add the event handler.
            AddHandler combo.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
        End If
    End Sub

    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim comboBox1 As ComboBox = CType(sender, ComboBox)
        Me.BackColor = CType(CType(sender, ComboBox).SelectedItem, Color)
    End Sub
End Class
程式檔案下載

3 則留言:

  1. 謝謝分享,請問如果我是 checkbox 點下去,但卻沒辦法用程式取到勾選的資料,要怎麼做?

    回覆刪除
  2. 有點趕,能請你幫我嗎?還是我去小舖發帖問你?

    回覆刪除
  3. 感謝你的幫忙!!

    回覆刪除