Solved

DataGridView - Combo Box - On Change of Value Event

Posted on 2012-04-05
4
353 Views
Last Modified: 2012-04-09
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
Comment
Question by:alfardan
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
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
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
what do you want to do when the value changes?
0
 

Author Comment

by:alfardan
Comment Utility
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
 

Author Closing Comment

by:alfardan
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now