[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

DataGridView - Combo Box - On Change of Value Event

Hi

Reference to my previous question (http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27663282.html), now what I want is that whenever go to any of the displayed rows then go to its Combo Box column then change the value then I want to do something, how do I add this event to each Combo Box of each row?
0
alfardan
Asked:
alfardan
  • 2
1 Solution
 
CodeCruiserCommented:
Handle the EditingControlShowing event of the grid and if the type of control being shown is combobox then subscribe to its change event

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx
0
 
nepaluzCommented:
what do you want to do when the value changes?
0
 
alfardanAuthor Commented:
CodeCruiser

The ComboBox control is created at run time, how do I add this event your mentioning?

Here is the full code of my grid:

Dim ApplicationList As New DataSet
ApplicationList = MC.DB(AppListSQLStr(MC.UserSysID()))

Dim img_down As New DataGridViewImageColumn()
Dim inImg1 As Image = Image.FromFile(IO.Directory.GetCurrentDirectory() + "\AppImages\down_arrow.jpg")
img_down.Name = "MoveDown"
img_down.HeaderText = ""
img_down.Image = inImg1

Dim img_up As New DataGridViewImageColumn()
Dim inImg2 As Image = Image.FromFile(IO.Directory.GetCurrentDirectory() + "\AppImages\up_arrow.jpg")
img_up.Name = "MoveUp"
img_up.HeaderText = ""
img_up.Image = inImg2

Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = "Priority"
cmb.Name = "cmb"
cmb.MaxDropDownItems = 3
cmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
Dim PriorityValues As New DataSet
PriorityValues = MC.DB("select PriorityDesc from App_Priority order by ID")
For b = 0 To PriorityValues.Tables(0).Rows.Count - 1
    cmb.Items.Add(PriorityValues.Tables(0).Rows(b).Item("PriorityDesc"))
Next


AppListOrderGridView.Columns.Add(img_down)
AppListOrderGridView.Columns.Add(img_up)
AppListOrderGridView.Columns.Add("ApplNum", "No.")
AppListOrderGridView.Columns.Add(cmb)
AppListOrderGridView.Columns.Add("ApplName", "Application Name")
AppListOrderGridView.Columns.Add("OrderID", "Order ID")
AppListOrderGridView.Columns.Add("ApplOrder", "Order Value")


For i = 0 To ApplicationList.Tables(0).Rows.Count - 1
    AppListOrderGridView.Rows.Add(inImg1, inImg2, (i + 1), ApplicationList.Tables(0).Rows(i).Item("AppPriority"), ApplicationList.Tables(0).Rows(i).Item("Appname"), ApplicationList.Tables(0).Rows(i).Item("ID"), ApplicationList.Tables(0).Rows(i).Item("AppOrder"))
Next


For i = 0 To AppListOrderGridView.ColumnCount - 1
    AppListOrderGridView.Columns(i).AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells
    AppListOrderGridView.Columns(i).SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
    If AppListOrderGridView.Columns(i).Name <> "cmb" Then
        AppListOrderGridView.Columns(i).ReadOnly = True
    End If
Next

AppListOrderGridView.Columns("OrderID").Visible = False
AppListOrderGridView.Columns("ApplOrder").Visible = False

AppListOrderGridView.Columns("ApplName").AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill

Open in new window


nepaluz

I want - whenever the value of the ComboBox changed- to save the new selected data to database.
0
 
alfardanAuthor Commented:
I got it, and it was through the (EditingControlShowing) as CodeCruiser mentioned, but the code there in the link he provided was not including the event handler part of the needed code I waslooking for.

Anyway, I got the event handler part and here is what I added to my code above:

Private Sub AppListOrderGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles AppListOrderGridView.EditingControlShowing
    Dim comboBox As ComboBox = CType(e.Control, ComboBox)
    If (Not (comboBox) Is Nothing) Then
        RemoveHandler comboBox.SelectedIndexChanged, AddressOf ComboBoxIndexChanged
        AddHandler comboBox.SelectedIndexChanged, AddressOf ComboBoxIndexChanged
    End If
End Sub

Sub ComboBoxIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    If AppListOrderGridView.CurrentCellAddress.X = 3 Then
        'Do Some Action
    End If
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now