C# 对于每个选定的datagridview行,发送到richTextBox
我需要foreach选定的datagridview1行将特定单元格发送到richTextBoxC# 对于每个选定的datagridview行,发送到richTextBox,c#,C#,我需要foreach选定的datagridview1行将特定单元格发送到richTextBox foreach (DataGridViewRow item in this.dataGridView1.SelectedRows) { //Each foreach iteration, item will contain a different row. richTextBox1.Text += item.Cells[1].Value.ToString(); //Append the
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
//Each foreach iteration, item will contain a different row.
richTextBox1.Text += item.Cells[1].Value.ToString(); //Append the text of the current row
}
以下是我正在尝试的:
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
richTextBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
但它只适用于第一个选中的项目。对于要重置richTextBox1内容的每个项目 在您的foreach中,您应该使用“item”。当前未使用项,但始终使用第一行 您可能希望将文本附加到RichTextBox的内容中
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
//Each foreach iteration, item will contain a different row.
richTextBox1.Text += item.Cells[1].Value.ToString(); //Append the text of the current row
}
注:something+=x是something=something+x的缩写 使用
项而不是dataGridView1.SelectedRows[0]
?另外,不要使用=
而是+=
来避免每次迭代都覆盖文本框的文本?