[英]How Can I Shorten This Code
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] .. 或更多.. 我可以缩短这段代码。 请帮助我..我很抱歉我的英语不好:(..
这是一个带有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
对象(它们是不可变的)和到处复制字符串数据。
旁注:你真的应该重命名你的变量: button4
、 dataGridView1
、 textBox1
等是非常糟糕的名字。 改用有意义的东西,它会让你免于以后的头痛。
使用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 循环。 一种优化是使用StringBuilder
创建字符串,然后设置文本框文本。
使用 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()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.