Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I wan to add records in datagridview inside a loop for that my code is given below.... it insert the data but only in last row. what should i do o insert the data in all rows please tell me.....


C#
for (int i = 1; i < 7; i++)
          {
              this.dgTimeInfo.Rows.Add();
              int rowCount = this.dgTimeInfo.Rows.Count;
              this.dgTimeInfo.Rows[i].Cells["Day"].Value = Sunday;
              DataGridViewColumn column = this.dgTimeInfo.Columns["Day"];
              column.Width = 110;
              this.dgTimeInfo.Rows[i].Cells["Date"].Value = grid[i][1].ToString();
              DataGridViewColumn column1 = this.dgTimeInfo.Columns["Date"];
              column1.Width = 100;
              this.dgTimeInfo.Rows[i].Cells["TimesheetStatus"].Value = "Incomplete";
              DataGridViewColumn column3 = his.dgTimeInfo.Columns["TimesheetStatus"];
                  column3.Width = 100;
              DataGridViewColumn column3 = this.dgTimeInfo.Columns["TimesheetStatus"];
                  column3.Width = 100;
              }
              this.dgTimeInfo.Rows[i].Cells["Action"].Value = "Details...";
              this.dgTimeInfo.Rows[i].Cells["GoAction"].Value = "Go";
Posted
Updated 30-Mar-12 20:36pm
v3
Comments
Orcun Iyigun 31-Mar-12 2:13am    
Edited code block.

1 solution

If you are going to ask a question, then you really need to make sure that your code fragment will compile - that won't.
1) column3 is declared twice in the same scope.
2) this is always spelt with a 't' at the front. (Not that you need to use this much anyway, and certainly not in that code.)
3) You cannot use "i" after the loop - it is out of scope (and probably outside your array even if it was in scope)
Then, and only then can you solve your problem: You aren't putting data in the right row.
The Add method returns an index, which tells you where the new row was inserted. Use it.
C#
for (int i = 1; i < 7; i++)
    {
    int rowIndex = dgTimeInfo.Rows.Add();
    dgTimeInfo.Rows[rowIndex].Cells["Day"].Value = Sunday;
    DataGridViewColumn column = dgTimeInfo.Columns["Day"];
    column.Width = 110;
    dgTimeInfo.Rows[rowIndex].Cells["Date"].Value = grid[i][1].ToString();
    DataGridViewColumn column1 = dgTimeInfo.Columns["Date"];
    column1.Width = 100;
    dgTimeInfo.Rows[rowIndex].Cells["TimesheetStatus"].Value = "Incomplete";
    DataGridViewColumn column3 = dgTimeInfo.Columns["TimesheetStatus"];
    column3.Width = 100;
    dgTimeInfo.Rows[rowIndex].Cells["Action"].Value = "Details...";
    dgTimeInfo.Rows[rowIndex].Cells["GoAction"].Value = "Go";
    }
Better still, move the fixed stuff outside the loop, and tighten the code a little:
C#
dgTimeInfo.Columns["Day"].Width = 110;
dgTimeInfo.Columns["Date"].Width = 100;
dgTimeInfo.Columns["TimesheetStatus"].Width = 100;

for (int i = 1; i < 7; i++)
    {
    int rowIndex = dgTimeInfo.Rows.Add();
    DataGridViewRow row = dgTimeInfo.Rows[rowIndex];
    row.Cells["Day"].Value = Sunday;
    row.Cells["Date"].Value = grid[i][1].ToString();
    row.Cells["TimesheetStatus"].Value = "Incomplete";
    row.Cells["Action"].Value = "Details...";
    row.Cells["GoAction"].Value = "Go";
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900