2014-02-15 3 views
1

테이블에서 gridview 데이터를 가져오고 있습니다. 내 gridview 3 텍스트 상자와 하나의 드롭 다운 control.I 데이터를 하나씩 데이터베이스에서 가져오고 싶습니다. 각 행을 가져 오는 후 내가 그리드보기의 행을 증가 시키려면 다시 가져 오십시오. 여기 Gridview의 행을 늘리는 방법은 무엇입니까?

는 루프 때의 원하는 이유는 확실하지 않다 내가 저를 도와주세요 '범위를 벗어난 인덱스'오류가있어 첫 번째 행을 인출 한 후 내 코드

SqlCommand sqlCommand2 = new SqlCommand("proc_bcdet", sqlConnection); 
sqlCommand2.CommandType = CommandType.StoredProcedure; 
sqlConnection.Open(); 
sqlCommand2.Parameters.Add("@btcode", SqlDbType.Int).Value = Session["btcode"]; 
SqlDataAdapter adp = new SqlDataAdapter(); 
adp.SelectCommand = sqlCommand2; 
DataSet ds = new DataSet(); 
adp.Fill(ds); 
DataTable dt = ds.Tables[0]; 

int rowIndex = 0; 
if (dt.Rows.Count > 0) 
{ 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 

     TextBox box1 = (TextBox)gvCashReceipt.Rows[i].Cells[1].FindControl("txtlgroup"); 
     TextBox box2 = (TextBox)gvCashReceipt.Rows[i].Cells[2].FindControl("txtamt"); 
     TextBox box3 = (TextBox)gvCashReceipt.Rows[i].Cells[3].FindControl("txtnarr"); 
     Label lb1 = (Label)gvCashReceipt.Rows[i].Cells[4].FindControl("lbllcode"); 
     DropDownList ddl1 = (DropDownList)gvCashReceipt.Rows[rowIndex].Cells[0].FindControl("drprledger"); 


     //Fill the DropDownList with Data 
     ddl1.DataSource = module.Query("Select Name from AccMast where LGcode in (2,3,4) order by Lcode"); 
     ddl1.DataBind(); 


     if (i < dt.Rows.Count) 
     { 

      //Assign the value from DataTable to the TextBox 
      box1.Text = dt.Rows[i]["Lgname"].ToString(); 
      box2.Text = dt.Rows[i]["Amt"].ToString(); 
      box3.Text = dt.Rows[i]["Narr"].ToString(); 
      lb1.Text = dt.Rows[i]["Lcode"].ToString(); 
      //Set the Previous Selected Items on Each DropDownList on Postbacks 
      ddl1.ClearSelection(); 
      ddl1.Items.FindByText(dt.Rows[i]["Name"].ToString()).Selected = true; 
     } 

     rowIndex++; 
    } 
} 

...

+1

"데이터베이스에서 한 행씩 데이터를 가져 오려고합니다."이는 대개 좋은 생각이 아닙니다. 한 번의 호출로 필요한 모든 데이터를 가져옵니다. 그렇지 않으면 페이지로드 당 수백 개의 쿼리를 호출 할 수 있습니다 ... 수백 명이 동일한 페이지를 클릭하면 결과가 최적이 아닙니다. – MikeSmithDev

답변

0

입니다 데이터를 직접 Gridview에 바인딩 할 수 있습니다. 그러나 증분 열을 가져 오는 것이 필요한 경우 증분 열이있는 새 데이터 테이블을 만든 다음 결과 데이터 테이블과 병합하는 것이 가장 좋은 방법입니다.

이 링크는이를 수행하는 방법을 보여줍니다. http://tinyurl.com/kv5v2zy

매우 유용한 데이터 그리드를 사용해보십시오.

희망이 도움이됩니다.

관련 문제