DataGridview单元格未填充

时间:2017-04-05 08:36:43

标签: c# for-loop datagridview windows-applications cells

for (int i = 0; i <= x; i++)
            {
                for (int k = 0; k <= TimeSub; k++)
                {

                    dataGridView1.Rows.Add(FromDate); // hour
                    dataGridView1.Rows[k].Cells[1].Value = FromTime;
                    FromTime = FromTime + 1;
                } 
                FromDate=FromDate.AddDays(1);

            }

当我执行此语句时,所有行都被正确填充但单元格却没有。请帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

您总是从第二个循环的第一行循环 Rows.Add重新生成新行索引。

请改为尝试:

var rowIndex = dataGridView1.Rows.Add(FromDate); // hour
dataGridView1.Rows[rowIndex].Cells[1].Value = FromTime;