菜单中的datagridview中的数据插入 [英] data insertion in datagridview menually

查看:54
本文介绍了菜单中的datagridview中的数据插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在datagridview的循环内添加记录,以下是我的代码....它插入数据但仅在最后一行.我该怎么办o将数据插入所有行中,请告诉我.....


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.....


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";

推荐答案

如果您要提出问题,那么您确实需要确保您的代码片段可以编译-不会.
1)在相同的范围内两次声明了column3.
2)this始终在前面拼写为"t". (不是您仍然需要经常使用this,并且肯定不需要在该代码中使用.)
3)您不能在循环后使用"i"-它超出范围(即使在范围内,也可能超出数组)
然后,只有这样才能解决您的问题:您不会将数据放在正确的行中.
Add方法返回一个索引,该索引告诉您新行的插入位置.使用它.
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.
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:

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";
    }


这篇关于菜单中的datagridview中的数据插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆