Solved

DataGridView - Combo Box - On Change of Value Event

Posted on 2012-04-05
4
360 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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