Asp.net 根据API响应填充GridView列数据
我是ASP.NET新手。我想根据API的响应动态地向Asp.net 根据API响应填充GridView列数据,asp.net,gridview,Asp.net,Gridview,我是ASP.NET新手。我想根据API的响应动态地向GridView添加一列 id User secretcode 1 u1 {response from the API based on the Id value} 2 u1 {response from the API based on the Id value} 3 u1 {response from the API based on the Id v
GridView
添加一列
id User secretcode
1 u1 {response from the API based on the Id value}
2 u1 {response from the API based on the Id value}
3 u1 {response from the API based on the Id value}
4 u1 {response from the API based on the Id value}
5 u1 {response from the API based on the Id value}
id
和User
已经在我的数据库表(users)中,因此对于每个返回的行,我想调用API来填充我的第三列,即secretcode
。基本上,我不知道在哪里使用ForEach循环
这是我正在研究的粗略代码:
DataTable table = new DataTable();
DataColumn col3 = new DataColumn("Secretcode");
col3.DataType = System.Type.GetType("System.Int");
table.Columns.Add(col3);
row[col3] = {response data from API}
gvTest.DataSource = table;
gvTest.DataBind();
也许是这样的
DataTable table = new DataTable();
DataColumn col = new DataColumn("Secretcode");
table.Columns.Add(col);
for(int i = 0; i < table.Rows.Count; i++)
{
// Where 'SomeAPICall()' is calling the API and returning the
// correct data type. If it is returning an object you may want
// to convert it before you attach it to the table
table.Rows[i]["Secretcode"] = SomeAPICall(table.Rows[i]["id"]);
}
gvTest.DataSource = table;
gvTest.DataBind();
我通常更喜欢使用for循环,因为通常您希望在两个不同的集合上使用相同的索引号,而这在foreach循环中是无法实现的。在这种情况下,这并不重要
DataTable table = new DataTable();
DataColumn col = new DataColumn("Secretcode");
table.Columns.Add(col);
foreach(DataRow row in table.Rows)
{
// Where 'SomeAPICall()' is calling the API and returning the
// correct data type. If it is returning an object you may want
// to convert it before you attach it to the table
row["Secretcode"] = SomeAPICall(row["id"]);
}
gvTest.DataSource = table;
gvTest.DataBind();