Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I use this to show some data from a datagridview with checkboxes to a textbox.

VB
TextBox1.Text = DataGridView1.Item(3, DataGridView1.CurrentRow.Index).Value


Oke , but how do i select more then one row and show the items seperated with a - between the items ?

For example when select 3 checkboxes

VB
Ben - Eric - John


What I have tried:

<pre lang="vb">  TextBox1.Text = DataGridView1.Item(3, DataGridView1.CurrentRow.Index).Value


This is only displaying one item , when i select 2 checkboxes it is only showing the last one
Posted
Updated 22-Jan-19 4:28am

not clear what the structure of your current code, but let assume there is a checkbox on each row with "Chk" name, the name is in the "name" column. You can write a loop to loop through the rows, if checked, capture the name, here is an example.

You can also try dataGridView1.SelectedRows if checking the checkbox will select the row.

C#
List<string> names = new List<string>();
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                bool isSelected = Convert.ToBoolean(r.Cells["Chk"].Value);

                if (isSelected)
                {
                    names.Add(r.Cells["name"].Value.ToString());
                }
            }

            MessageBox.Show(string.Join(" - ", names));
 
Share this answer
 
Comments
BASSIES 20-Jan-19 7:04am    
Thanks for your answer Bryian , but i can't transform the code to vb.net.

There is an error


Error converting code
Error message:
CONVERSION ERROR: Conversion for IncompleteMember not implemented, please report this issue in '(DataGridViewRow r ' at character 88
BASSIES 20-Jan-19 9:23am    
Found another converter

Dim names As List(Of String) = New List(Of String)
For Each r As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(r.Cells("Chk").Value)
If isSelected Then
names.Add(r.Cells("Product").Value.ToString)
End If

Next
MessageBox.Show(String.Join(" - ", names))

Get an error on names
List(Of String)' cannot be converted to 'String()
Bryian Tan 20-Jan-19 15:52pm    
The code seem fine. Did you check what in the "names"?
BASSIES 21-Jan-19 7:47am    
I can't start the debug because of the error on names

the error is

List(Of String)' cannot be converted to 'String()
BASSIES 22-Jan-19 8:38am    
Bryian ,

Was searching on your hint to look for dataGridView1.SelectedRows

Found this , it works but the text is not seperated with -
Tryed to ajust but no luck

The outcome is BenEricJohn no - between the name's


Dim message As String = String.Empty
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("chk").Value)
If isSelected Then
message &= Environment.NewLine
message &= row.Cells("name").Value.ToString()
End If
Next

TextBox1.Text = ("Selected Values" & message)

what do i change here with textBox1.text= (..........
Dim message As String = String.Empty
      For Each row As DataGridViewRow In DataGridView1.Rows
          Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("chk").Value)
          If isSelected Then

              message &= row.Cells("Name").Value.ToString()
              message &= " / "
          End If
      Next


      TextBox1.Text = (message)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900