我有一个关于从dataGridView转换为文本框的问题。我希望当看到x用Headername替换它时
我在这里有dataGridview:
Name A B C D ...
kiki x x ...
lola x ...
maja x x x ...
我想要在我的文本框中:
kiki A,C ...
lola B ...
maja A,C,D ...
我认为我的代码看起来像这样;
string abc = "";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 1; i < dataGridView1.Columns.Count - 1; i++)
{
if(dataGridView1.Rows[i].Cells[j].Value.ToString() != "x")
abc = dataGridView1.Columns[j].HeaderText.ToString();
richTextBox1.Text += dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " + abc;
//or something like that, can some1 give me a key?
}
}
有人可以帮助我吗?不知道怎么做,请给我任何想法
答案 0 :(得分:1)
这对你有用。此代码将所有内容放入一行多行richTextBox
,每行添加一行:
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string text = dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " ;
bool added = false;
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString() == "x")
{
text += dataGridView1.Columns[j].HeaderText.ToString() + ",";
if (!added)
added = true;
}
}
if(added)
{
text = text.Remove(text.Length - 1); //to remove ',' at the end
}
richTextBox1.Text += text + Environment.NewLine; //add to richTextbox1 with newline
}