DataGridView多个选择到文本框用逗号 [英] DataGridView Multiple selection to textbox with comma
本文介绍了DataGridView多个选择到文本框用逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮我用逗号将datagridview多个选择添加到文本框,如果我选择多行,我仅将一行列添加到文本框,以下是我到目前为止已完成的代码..
<
Hi, please help me on getting datagridview multiple selection to textbox with comma, if i select multiple row i am getting only one rows column to textbox, below is code which i have done so far..
<
private void btnUpdate_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow drv in dataGridView1.SelectedRows)
{
textBox1.Text = "";
bool val = true;
if (dataGridView1.SelectedRows != null)
{
if (!firstvalue)
{
textBox1.Text = ",";
}
textBox1.Text = textBox1.Text + dataGridView1.SelectedCells[1].Value.ToString();
val = false;
}
}
}
推荐答案
您正在重置文本,而不是在if循环中添加逗号.您在if块中的代码应该是.
You are resetting the text instead of appending the comma in the if loop. Your code in the if block should be.
if (!firstvalue)
{
textBox1.Text += ",";
}
更新SM:
试试:
UPDATE SM:
Try:
if (!firstvalue)
{
textBox1.Text += "," + dataGridView1.SelectedCells[1].Value.ToString();
}
更新#2 Tarak:
另一个问题是您要为每一行重置textBox1.Text =".您必须将其移出foreach.另外,您还需要使用drv并从中获取值.
Update #2 Tarak:
Another problem is you are resetting the textBox1.Text = "" for each of the row. You have to move it out of the foreach. Also you need to use the drv and get the value from that.
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows != null)
{
textBox1.Text = "";
bool firstvalue = true;
foreach (DataGridViewRow drv in dataGridView1.SelectedRows)
{
if (!firstvalue)
{
textBox1.Text += ",";
}
textBox1.Text += drv.Cells[1].Value.ToString();
firstvalue = false;
}
}
}
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
//Column Num represents col names
MessageBox.Show(row.Cells[1].Value.ToString());
//////////////The 1 Represent the grid column you want the data from
}
By stylesmylez
这篇关于DataGridView多个选择到文本框用逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文