Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向DataGridView C添加新行#_C#_Datagridview_Row - Fatal编程技术网

C# 向DataGridView C添加新行#

C# 向DataGridView C添加新行#,c#,datagridview,row,C#,Datagridview,Row,我正在寻找一种向datagrid添加新行的简单方法。那么,我有: dataGridView2.ColumnCount = 4; int w = 0; foreach (var item in tInArr) {... dataGridView2.Rows.Add(); dataGridView2[0, w].Value = muTat3.ToString(); dataGridView2[1, w].Value = item.ToString(); .... w++;} 我的错在哪里?我在这里

我正在寻找一种向datagrid添加新行的简单方法。那么,我有:

dataGridView2.ColumnCount = 4;
int w = 0;
foreach (var item in tInArr)
{...
dataGridView2.Rows.Add();
dataGridView2[0, w].Value = muTat3.ToString();
dataGridView2[1, w].Value = item.ToString();
....
w++;}

我的错在哪里?我在这里读过一些文章,但没有答案。

首先创建datatable,然后可以将datagrid的数据源设置为长但易于维护的数据源

像这样

DataTable table = new DataTable();
table.Columns.Add("Check", typeof(bool));
table.Columns.Add("Path", typeof(string));
table.Columns.Add("Date", typeof(DateTime));


table.Rows.Add(false, "", DateTime.Now);
dataGridView2.DataSource = table;

首先创建datatable,然后可以将datagrid的数据源设置为长但易于维护的数据源

像这样

DataTable table = new DataTable();
table.Columns.Add("Check", typeof(bool));
table.Columns.Add("Path", typeof(string));
table.Columns.Add("Date", typeof(DateTime));


table.Rows.Add(false, "", DateTime.Now);
dataGridView2.DataSource = table;

首先创建datatable,然后可以将datagrid的数据源设置为长但易于维护的数据源

像这样

DataTable table = new DataTable();
table.Columns.Add("Check", typeof(bool));
table.Columns.Add("Path", typeof(string));
table.Columns.Add("Date", typeof(DateTime));


table.Rows.Add(false, "", DateTime.Now);
dataGridView2.DataSource = table;

首先创建datatable,然后可以将datagrid的数据源设置为长但易于维护的数据源

像这样

DataTable table = new DataTable();
table.Columns.Add("Check", typeof(bool));
table.Columns.Add("Path", typeof(string));
table.Columns.Add("Date", typeof(DateTime));


table.Rows.Add(false, "", DateTime.Now);
dataGridView2.DataSource = table;

如果没有数据源,则可以采用以下方法:

  int rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value1"; // 0 for first column
  dataGridView1[1, rowIndex].Value = "value2"; // 1 for second column
  rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value3";
  dataGridView1[1, rowIndex].Value = "value4";
在您的代码中,它应该是:

// dataGridView2.ColumnCount = 4;  
int w = 0;
foreach (var item in tInArr)
{...
  w = dataGridView2.Rows.Add();
  dataGridView2[0, w].Value = muTat3.ToString();
  dataGridView2[1, w].Value = item.ToString();
  ....
  //w++; this is not required
}

如果没有数据源,则可以采用以下方法:

  int rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value1"; // 0 for first column
  dataGridView1[1, rowIndex].Value = "value2"; // 1 for second column
  rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value3";
  dataGridView1[1, rowIndex].Value = "value4";
在您的代码中,它应该是:

// dataGridView2.ColumnCount = 4;  
int w = 0;
foreach (var item in tInArr)
{...
  w = dataGridView2.Rows.Add();
  dataGridView2[0, w].Value = muTat3.ToString();
  dataGridView2[1, w].Value = item.ToString();
  ....
  //w++; this is not required
}

如果没有数据源,则可以采用以下方法:

  int rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value1"; // 0 for first column
  dataGridView1[1, rowIndex].Value = "value2"; // 1 for second column
  rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value3";
  dataGridView1[1, rowIndex].Value = "value4";
在您的代码中,它应该是:

// dataGridView2.ColumnCount = 4;  
int w = 0;
foreach (var item in tInArr)
{...
  w = dataGridView2.Rows.Add();
  dataGridView2[0, w].Value = muTat3.ToString();
  dataGridView2[1, w].Value = item.ToString();
  ....
  //w++; this is not required
}

如果没有数据源,则可以采用以下方法:

  int rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value1"; // 0 for first column
  dataGridView1[1, rowIndex].Value = "value2"; // 1 for second column
  rowIndex = dataGridView1.Rows.Add();
  dataGridView1[0, rowIndex].Value = "value3";
  dataGridView1[1, rowIndex].Value = "value4";
在您的代码中,它应该是:

// dataGridView2.ColumnCount = 4;  
int w = 0;
foreach (var item in tInArr)
{...
  w = dataGridView2.Rows.Add();
  dataGridView2[0, w].Value = muTat3.ToString();
  dataGridView2[1, w].Value = item.ToString();
  ....
  //w++; this is not required
}

在这里,您可以使用datable,然后按如下方式迭代循环:

 foreach (DataRow rows in dt.Rows)
 {..

     int w =  dataGridView2.Rows.Add();

     dataGridView2[0, w].Value =  rows[0].ToString();
     dataGridView2[1, w].Value = rows[1].ToString();
  }

在这里,您可以使用datable,然后按如下方式迭代循环:

 foreach (DataRow rows in dt.Rows)
 {..

     int w =  dataGridView2.Rows.Add();

     dataGridView2[0, w].Value =  rows[0].ToString();
     dataGridView2[1, w].Value = rows[1].ToString();
  }

在这里,您可以使用datable,然后按如下方式迭代循环:

 foreach (DataRow rows in dt.Rows)
 {..

     int w =  dataGridView2.Rows.Add();

     dataGridView2[0, w].Value =  rows[0].ToString();
     dataGridView2[1, w].Value = rows[1].ToString();
  }

在这里,您可以使用datable,然后按如下方式迭代循环:

 foreach (DataRow rows in dt.Rows)
 {..

     int w =  dataGridView2.Rows.Add();

     dataGridView2[0, w].Value =  rows[0].ToString();
     dataGridView2[1, w].Value = rows[1].ToString();
  }

我猜你有索引错误吗?@AnuragJain它是一些数字的列表吗?我猜你有索引错误吗?@AnuragJain它是一些数字的列表吗?我猜你有索引错误吗?@AnuragJain它是一些数字的列表吗?AnuragJain它是一些数字的列表