datagridview行按指定数量循环

ad0128 2010-12-08 09:27:16
如图现在有2个datagridview,


其中datagridview1可以编辑C列单元格是要循环的数量

点击print 将datagridview1的每行按C单元格的数量循环后添加到datagridview2中。
注:C列数量在datagridview2中可以不显示。

正确的结果应该是
1 1
1 1
2 2
2 2
2 2

请问如何实现?

        private void AddRow_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add("", "", "");
}

private void Print_Click(object sender, EventArgs e)
{

int count = 0;
int total = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
count = int.Parse(dataGridView1.Rows[i].Cells["c"].Value.ToString());
total += int.Parse(dataGridView1.Rows[i].Cells["c"].Value.ToString());

dataGridView2.RowCount = total;
for (int j = 0; j < total; j++)
{
for (int n = 0; n <= count; n++)
{
dataGridView2.Rows[j].Cells[0].Value = dataGridView1.Rows[i].Cells[0].Value;
dataGridView2.Rows[j].Cells[1].Value = dataGridView1.Rows[i].Cells[1].Value;
}
}
}
}




...全文
112 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhabb 2010-12-08
  • 打赏
  • 举报
回复
jhabb 2010-12-08
  • 打赏
  • 举报
回复
先获取c的数量 再根据这个数量 循环不就可以啦
ad0128 2010-12-08
  • 打赏
  • 举报
回复
刚开始用winform。大家帮帮解决一下。
ruanwei1987 2010-12-08
  • 打赏
  • 举报
回复
cxx1997 2010-12-08
  • 打赏
  • 举报
回复
到底是不会操作dataGridView,还是不会遍历?
2个问题都很简单,自己去GOOGLE吧
cxx1997 2010-12-08
  • 打赏
  • 举报
回复
无语。。。。
TimZhuFaith 2010-12-08
  • 打赏
  • 举报
回复
生成datatable就是了
ad0128 2010-12-08
  • 打赏
  • 举报
回复
帮帮忙,很急

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧