C# 在datagridview中,我使用for循环将值存储在右边的行中。是否显示索引超出范围错误? foreach(table.Rows中的数据行) { if(第[“ProductId”].ToString()行包含(searchValue)) { 对于(inti=1;i

C# 在datagridview中,我使用for循环将值存储在右边的行中。是否显示索引超出范围错误? foreach(table.Rows中的数据行) { if(第[“ProductId”].ToString()行包含(searchValue)) { 对于(inti=1;i,c#,datagridview,C#,Datagridview,我认为 foreach (DataRow row in table.Rows) { if (row["ProductId"].ToString().Contains(searchValue)) { for (int i = 1; i <= table.Rows.Count; i++) { MessageBox.Show("Produ

我认为

foreach (DataRow row in table.Rows)
{                    
   if (row["ProductId"].ToString().Contains(searchValue))
   {
       for (int i = 1; i <= table.Rows.Count; i++)
       {                            
            MessageBox.Show("Product Id: " + row["ProductId"].ToString());
            MessageBox.Show(dataGridView1.Rows.Count.ToString());

            dataGridView1.Rows[i].Cells[0].Value = row["ProductId"].ToString();
            dataGridView1.Rows[i].Cells[1].Value = row["ProductName"].ToString();
            dataGridView1.Rows[i].Cells[2].Value = row["ProductType"].ToString();
            dataGridView1.Rows[i].Cells[3].Value = row["CreateDate"].ToString();
            dataGridView1.Rows[i].Cells[4].Value = row["UpdateDate"].ToString();
            dataGridView1.Rows[i].Cells[5].Value = row["IsActive"].ToString();
       }
   }
} 
inti=1;i我认为

foreach (DataRow row in table.Rows)
{                    
   if (row["ProductId"].ToString().Contains(searchValue))
   {
       for (int i = 1; i <= table.Rows.Count; i++)
       {                            
            MessageBox.Show("Product Id: " + row["ProductId"].ToString());
            MessageBox.Show(dataGridView1.Rows.Count.ToString());

            dataGridView1.Rows[i].Cells[0].Value = row["ProductId"].ToString();
            dataGridView1.Rows[i].Cells[1].Value = row["ProductName"].ToString();
            dataGridView1.Rows[i].Cells[2].Value = row["ProductType"].ToString();
            dataGridView1.Rows[i].Cells[3].Value = row["CreateDate"].ToString();
            dataGridView1.Rows[i].Cells[4].Value = row["UpdateDate"].ToString();
            dataGridView1.Rows[i].Cells[5].Value = row["IsActive"].ToString();
       }
   }
} 

int i=1;i@mani您确定您有
6
列吗?在哪一行出现此错误?当您出现此错误时,
i
的值是多少?我认为将单元格值“row[“xxx”].ToString()指定为一个错误。这样,您就可以将“int”或“DateTime”值类型更改为“string”。因此删除“.ToString()”值.除了我前面的评论之外,令人惊讶的是,您在“for each”中循环了DataTable,然后又在“for int i”中循环了。不是吗loop refer to DataGridView rowsCount?@mani您确定您有
6
列吗?在哪一行出现此错误?当您出现此错误时,
i
的值是多少?我认为将单元格值“row[“xxx”].ToString()”指定为一个错误。执行此操作时,您将“int”或“DateTime”值类型更改为“string”。因此删除“.ToString()”。除了我前面的评论之外,令人惊讶的是,您在“for each”中循环了DataTable,然后又在“for int i”中循环。“for i”循环不是指DataGridView rowsCount吗?
int i = 0; i < table.Rows.Count