how get average of two DGV ComboBoxColumn to be in DGV textboxColumn

ahmed707

New member
Joined
Feb 2, 2015
Messages
3
Programming Experience
3-5
i have datagridview with 3 columns the first column is Assessment1 the second column is Assessment 2
now i want to get the average of two columns to be as new column in data grid view

this code

PHP:
01
Dim dictionary As New Dictionary(Of String, Integer)
02
            dictionary.Add("A", 100)
03
            dictionary.Add("B", 85)
04
            dictionary.Add("C", 70)
05
            dictionary.Add("D", 60)
06
            Dim combo1 = New DataGridViewComboBoxColumn
07
            combo1.FlatStyle = FlatStyle.Flat
08
            combo1.DisplayMember = "Key"
09
            combo1.ValueMember = "Value"
10
            combo1.HeaderText = "1st Assessment"
11
            combo1.DataSource = New BindingSource(dictionary, Nothing)
12
            DataGridView1.Columns.Insert(4, combo1)
13
 
14
            Dim combo2 = New DataGridViewComboBoxColumn
15
            combo2.FlatStyle = FlatStyle.Flat
16
            combo2.DisplayMember = "Key"
17
            combo2.ValueMember = "Value"
18
            combo2.HeaderText = "2nd Assessment"
19
            combo2.DataSource = New BindingSource(dictionary, Nothing)
20
            DataGridView1.Columns.Insert(5, combo2)
21
 
22
            Dim txtbox1 = New DataGridViewTextBoxColumn
23
 
24
 
25
           '/ what should i write here to get the average for the values of two columns

any input is greatly appreciated


thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,545
Location
Norway
Programming Experience
10+
For example do something like this in CellEndEdit event handler:
        If TypeOf Me.DataGridView1.CurrentCell Is DataGridViewComboBoxCell Then
            With Me.DataGridView1.Rows(e.RowIndex)
                .Cells(6).Value = (CInt(.Cells(4).Value) + CInt(.Cells(5).Value)) / 2
            End With
        End If

4 and 5 is index of those two combobox columns, 6 is index of average column - you could give the columns names instead.
 

ahmed707

New member
Joined
Feb 2, 2015
Messages
3
Programming Experience
3-5
thanks brother JohnH

it works fine but the result does not appear directly till change cell location i mean that the result does not come during select item from combo box

i am really grateful to you again
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,545
Location
Norway
Programming Experience
10+
When user edits the value is not committed until editing ends, this happens when user leaves the cell or EndEdit is called. You could do this in CurrentCellDirtyStateChanged event handler:
        If TypeOf Me.DataGridView1.CurrentCell Is DataGridViewComboBoxCell Then
            Me.DataGridView1.EndEdit()
        End If
 
Top Bottom