Click here to Skip to main content
14,665,550 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,

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

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

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

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));
   
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= (..........
Bryian Tan 22-Jan-19 9:17am
   
Well, not clear why the list of string wouldn’t work. Anyway, with this route, you can append the “-“ by doing something like &= “-“. The problem with this approach, the output will end up with redundant “-“ at the end. You might need to write another line of code to remove the last trailing “-“
BASSIES 22-Jan-19 10:26am
   
Thanks Bryian ,

I can remove it if wanted.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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)
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100