Solved

DataGridView - Combo Box - On Change of Value Event

Posted on 2012-04-05
4
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37810305
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
ID: 37810318
what do you want to do when the value changes?
0
 

Author Comment

by:alfardan
ID: 37820487
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
ID: 37826288
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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