例如,dataGridView单元格中的一个单词是:one; two; three .....
我希望在text2中的form2中单独显示:
text in textBox1: one
text in textBox2: two
text in textBox3: three
我如何解析这个?
我用formOne这样填充datagrid:
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
string text = "";
for (int i = 0; i < emails.Length ;i++)
{
if (emails[i].ToString().Trim() != "")
{
text = text + emails[i] + ";" ;
dataGridView1.Rows[cell.RowIndex].Cells[col].Value = text;
}
}
}
答案 0 :(得分:2)
string cellValue = "one;two;three";
// should contain at least three values
var values = cellValue.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
textBox1.Text = values[0];
textBox2.Text = values[1];
textBox3.Text = values[2];
如果单元格中可能存在不同数量的值,请考虑动态创建texbox。另一种选择是使用网格。
另一个选项 - 获取texbox的列表:
var textBoxes = new List<TextBox> { textBox1, textBox2, textBox3 };
或者,如果您要按照正确的顺序添加texbox:
var textBoxes = Controls.OfType<TextBox>().ToList();
将它们全部填入循环
string cellValue = "one;two;three";
var values = cellValue.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < values.Length; i++)
if (textBoxes.Count < i) // also you can ensure you have textBox for value
textBoxes[i].Text = values[i];