简体   繁体   中英

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM