2

我有一个关于从 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?  
        } 
    }

有人可以帮我吗?不知道怎么做,请给我任何想法

4

1 回答 1

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
        }
于 2012-11-05T13:28:48.873 回答