Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 21, 2017 5:52 AM
Hi Everybody
I want get value of two columns in DataGridView with multi select in vb.net.
how to do it?
Thanks all.
Name of Allah, Most Gracious, Most Merciful and He created the human
All replies (5)
Thursday, September 21, 2017 8:38 AM ✅Answered | 2 votes
Hi,
How about this?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.DataGridView1.MultiSelect = True
For myRow As Integer = 0 To 9
Me.DataGridView1.Rows.Add()
Me.DataGridView1(0, myRow).Value = myRow
Me.DataGridView1(1, myRow).Value = myRow
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim val1 As String = ""
Dim val2 As String = ""
Dim cnt As Integer = 0
'
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
Dim selectedCol = cell.ColumnIndex
Dim selectedRow = cell.RowIndex
If (cnt = 0) Then
val1 = DataGridView1(selectedCol, selectedRow).Value
Else
val2 = DataGridView1(selectedCol, selectedRow).Value
End If
cnt += 1
Next
'
If (val1 = val2) Then
MessageBox.Show("Same" & Chr(13) & "val1=" & val1 & Chr(13) & "val2=" & val2)
Else
MessageBox.Show("Not same" & Chr(13) & "val1=" & val1 & Chr(13) & "val2=" & val2)
End If
End Sub
End Class
Regards,
Ashidacchi
Thursday, September 21, 2017 9:02 AM ✅Answered | 2 votes
Hi sh4015,
According to your description, you want to get two column value in datagridview multi select, you can refer to the following code to do something that you like:
Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Column2", GetType(Int32))
dt.Columns.Add("Column3", GetType(String))
dt.Columns.Add("Column4", GetType(String))
dt.Columns.Add("Column5", GetType(String))
dt.Rows.Add(1, "AA", "AAA", "AAAA")
dt.Rows.Add(2, "BB", "BBB", "BBBB")
dt.Rows.Add(3, "CC", "CCC", "CCCC")
dt.Rows.Add(4, "DD", "DDD", "DDDD")
dt.Rows.Add(5, "EE", "EEE", "EEEE")
dt.Rows.Add(6, "FF", "FFF", "FFFF")
dt.Rows.Add(7, "GG", "GGG", "GGGG")
dt.Rows.Add(8, "HH", "HHH", "HHHH")
dt.Rows.Add(9, "II", "III", "IIII")
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells("Column1").Value = 1
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells("Column1").Value = 0
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Cells("Column1").Value = True Then
'do something that you want to do
MessageBox.Show(String.Format("The first column is {0} and the second column is {1} :", DataGridView1.Rows(i).Cells("Column2").Value.ToString(), DataGridView1.Rows(i).Cells("Column3").Value.ToString()))
End If
Next
End Sub
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Friday, September 22, 2017 8:12 AM ✅Answered | 2 votes
Hi,
Your code is almost perfect.
You should use ROW, instead of CELL, in For Each loop.
I've changed it a little bit.
' [Calc Sum]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dblTotalDebit As Double = 0
Dim dblTotalCredit As Double = 0
'
For Each row As DataGridViewRow In DataGridView1.SelectedRows
Dim selectedRow = row.Index
dblTotalDebit += DataGridView1(2, selectedRow).Value
dblTotalCredit += DataGridView1(3, selectedRow).Value
Next
'
Me.tbTotalDebit.Text = dblTotalDebit
Me.tbTotalCredit.Text = dblTotalCredit
'
MessageBox.Show("Calc completed")
End Sub
Regards,
___________________
Ashidacchi
Friday, September 22, 2017 7:29 AM
thanks all
see this my code..
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Id", GetType(Int32))
dt.Columns.Add("Descereb", GetType(String))
dt.Columns.Add("Debit", GetType(Int64))
dt.Columns.Add("Credit", GetType(Int64))
dt.Rows.Add(1, "AA", 10252600, 32980401)
dt.Rows.Add(2, "BB", 3258404, 0)
dt.Rows.Add(3, "CC", 9802703, 65890000)
dt.Rows.Add(4, "DD", 0, 1258)
dt.Rows.Add(5, "EE", 9851045, 0)
dt.Rows.Add(6, "FF", 0, 0)
dt.Rows.Add(7, "GG", 3256000, 459700)
dt.Rows.Add(8, "HH", 358800, 657410)
dt.Rows.Add(9, "II", 77400258, 325800)
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dblTotalDebit As Double
Dim dblTotalCredit As Double
dblTotalDebit = 0
dblTotalCredit = 0
'
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
Dim selectedRow = cell.RowIndex
dblTotalDebit &= DataGridView1(2, selectedRow).Value
dblTotalCredit &= DataGridView1(3, selectedRow).Value
Next
'
tbTotalDebit.Text = dblTotalDebit
tbTotalCredit.Text = dblTotalCredit
End Sub
and my form...
but i see this error
how to solve it ?
Name of Allah, Most Gracious, Most Merciful and He created the human
Friday, September 22, 2017 12:20 PM
THANKS ALL
Name of Allah, Most Gracious, Most Merciful and He created the human