2012-02-23 3 views
0

ASP.NET을 처음 사용합니다. 나는 각 행을 반환에 대한, 그래서 내가 채우기 위해 API를 호출 할 동적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 value} 
4  u1  {response from the API based on the Id value} 
5  u1  {response from the API based on the Id value} 

idUser 내 데이터베이스 테이블 (사용자)에 이미있는 API의 응답에 따라 GridView에 열을 추가 할 내 세 번째 열, 즉 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(); 

답변

1

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(); 

같은

아마도 뭔가 아니면 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(); 

자주 for 루프를 사용하는 것이 좋습니다. 일반적으로 두 개의 다른 컬렉션에서 동일한 인덱스 번호를 사용하기를 원하기 때문입니다. wh foreach 루프로는 실제로 할 수 없습니다. 이 경우에는 문제가되지 않지만

+0

thnx 님이 도움을 주셨습니다. –