?
Solved

DataGridView - Combo Box - On Change of Value Event

Posted on 2012-04-05
4
Medium Priority
?
362 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 1500 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 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