我正在开发一个Windows窗体应用程序,我已经使用datagridview来显示我的数据,但我还想从Datagridview中选择一些特定的列并将它们显示给一个TextBox。
我已经尝试了,但它只是将最后一个名为“SEIKEN_NO”的列添加到文本框中,但我希望在一个TextBox中显示多个列值。
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// to set oem no, fic_no and seiken_no to textbox named particular1Txt.
dataGridView1.Refresh();
try
{
if (RB1.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
Particular1Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
Particular1Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
else if (RB2.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular2Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
Particular2Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
Particular2Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
答案 0 :(得分:2)
在为TextBox分配数据的每一行之前:
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
更改为:
Particular1Txt.Text += dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
请注意+=
否则您将覆盖TextBox的内容
对于值并置格式,您可以:
+= " " + dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); // very basic way to do it
String.Format()
的另一种方式示例Particular1Txt.Text=String.Format({0} {1} {2}), dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(),dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(),dataGridView1.Rows[i].Cells["Seiken_No"].Value.ToString();
您还可以使用:String.Join()和字符串数组,或StringBuilder类,
希望这能帮到你
答案 1 :(得分:1)
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// to set oem no, fic_no and seiken_no to textbox named particular1Txt.
dataGridView1.Refresh();
try
{
if (RB1.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
else if (RB2.Checked == true)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
}
}
}