我该如何缩短本规范

时间:2015-06-13 23:08:59

标签: c#

private void button4_Click(object sender, EventArgs e)
{  
    textBox1.Text = dataGridView1.Rows[0].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[0].Cells[1].Value.ToString() +
                   "&" + dataGridView1.Rows[0].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[0].Cells[3].Value.ToString() + 
                   "%" +
                         dataGridView1.Rows[1].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[1].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[1].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[1].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[2].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[2].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[2].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[2].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[3].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[3].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[3].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[3].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[4].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[4].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[4].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[4].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[5].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[5].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[5].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[5].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[6].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[6].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[6].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[6].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[7].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[7].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[7].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[7].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[8].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[8].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[8].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[8].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[9].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[9].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[9].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[9].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[10].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[10].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[10].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[10].Cells[3].Value.ToString() + 
                   "%" + 
                         dataGridView1.Rows[11].Cells[0].Value.ToString() + 
                   "&" + dataGridView1.Rows[11].Cells[1].Value.ToString() + 
                   "&" + dataGridView1.Rows[11].Cells[2].Value.ToString() + 
                   "&" + dataGridView1.Rows[11].Cells[3].Value.ToString() + 
                   "%" ;
}

也许这行[11]到[250] ......或者...... 我可以缩短这段代码吗? 请帮我 .. 对不起我的英语很抱歉:( ..

3 个答案:

答案 0 :(得分:2)

这是一个StringBuilder的版本,适用于任意数量的行/列:

private void button4_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
            sb.Append(cell.Value).Append('&');

        // Replace the last '&' with a '%'
        if (sb.Length != 0)
            sb[sb.Length - 1] = '%';
    }

    textBox1.Text = sb.ToString();
}

StringBuilder将构建一个字符串,而不必分配大量string个对象(这些对象是不可变的)并在整个地方复制字符串数据。

旁注:您应该真正重命名您的变量:button4dataGridView1textBox1真的 可怕的 名称。使用有意义的东西,它可以避免以后的麻烦。

答案 1 :(得分:1)

使用for循环:

private void button4_Click(object sender, EventArgs e)
{  
     for (int i = 0; i <= 11; i++)
     {
         textBox1.Text += dataGridView1.Rows[i].Cells[0].Value.ToString() + "&" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "&" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "&" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "%"
     }
}

你可以使用另一个for循环播放另一个“&amp;”如果你想要的话。优化是使用StringBuilder创建字符串,然后设置文本框文本。

答案 2 :(得分:1)

使用LINQ:

textBox1.Text = string.Join("%",
                            from row in dataGridView1.Rows
                            select string.Format("{0}&{1}&{2}&{3}",
                                                 row.Cells[0].Value.ToString(),
                                                 row.Cells[1].Value.ToString(),
                                                 row.Cells[2].Value.ToString(),
                                                 row.Cells[3].Value.ToString()));