Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the content of a cell datagridview Winform C#

Hello so I managed to get the row where my mouse highlighted it. However I don't know how to access its content.

for example: I highlighted row 25 I can get that it highlighted row 25 howver I don't know how to access its content and put it all in a textbox. anyone?

like image 545
rj tubera Avatar asked Jan 15 '12 16:01

rj tubera


1 Answers

you can use the value property to access the content of the cell as follows

// the content of the cell
var x = dataGridView1.Rows[0].Cells[0].Value;

// this gets the content of the first selected cell 
dataGridView1.SelectedCells[0].Value;

// you can set the cells the user is able to select using this 
dataGridView1.SelectionMode= SelectionMode. //intellisense gives you some options here

To do exactly what you are asking something like this should suffice

System.Text.StringBuilder t = new System.Text.StringBuilder();
String delimiter = ",";

foreach(DataGridViewCell c in dataGridView1.SelectedRows[0].Cells)
{
    t.Append(c.Value);
    t.Append(delimiter);
}

textBox1.Text = t.ToString();
like image 169
SkeetJon Avatar answered Nov 04 '22 10:11

SkeetJon