2011-03-01 4 views
0

데이터베이스에서 데이터를 검색하는 DataGridview 컨트롤이 있습니다. 그 일을 할 수있는 코드가 있었지만 gridview에서 데이터를 찾을 수 없었습니다. 나는 루프에서 브레이크 포인트를 만들었지 만 DataGridViewTextBoxCell Number = (DataGridViewTextBoxCell)DGCategory.Rows[i].Cells[l]; 데이터 그리드 뷰에 데이터가없는 apeared 때DataGridview 컨트롤에서 루프 문을 만들 때 데이터가 표시되지 않습니다.

private void getcategory() 
     { 
      for (int i = 0; i < DGCategory.Rows.Count; i++) 
      { 
       for (int l = 0; l < DGCategory.Rows[i].Cells.Count; l++) 
       { 
        DataGridViewTextBoxCell Number = (DataGridViewTextBoxCell)DGCategory.Rows[i].Cells[l]; 
        DataGridViewTextBoxCell Category = (DataGridViewTextBoxCell)DGCategory.Rows[i].Cells[l]; 
        DataGridViewTextBoxCell Parent = (DataGridViewTextBoxCell)DGCategory.Rows[i].Cells[l]; 
        DataGridViewCheckBoxCell Active = (DataGridViewCheckBoxCell)DGCategory.Rows[i].Cells[l]; 
        DataGridViewTextBoxCell AddDate = (DataGridViewTextBoxCell)DGCategory.Rows[i].Cells[l]; 

       using (SqlConnection Con = GetConnection()) 
       { 
        SqlCommand cmd = new SqlCommand("SP_GetAllCategories", Con); 
        cmd.CommandType = CommandType.StoredProcedure; 
        SqlDataReader dr; 
        dr = cmd.ExecuteReader(); 
        if (dr.Read()) 
        { 
         Number.Value = dr["Number"].ToString(); 
         Category.Value = dr["Category"].ToString(); 
         Parent.Value = dr["Parent"].ToString(); 
         Active.Value = dr["Active"].ToString(); 
         AddDate.Value = dr["AddDate"].ToString(); 

        } 

       } 
      } 
     } 
    } 
+0

나는 당신이 그 루프로 무엇을하려하는지 전혀 모른다. –

답변

1

u는 그리드보기로 데이터베이스에서 값을 얻고 싶다면 .. 다음을 사용 :
는 쿼리를 작성 예를 들어, "SelectCommand에"라는 문자열에 다음 결과를 얻을 수있는 데이터 세트를 사용

데이터의 gridview 코드에서 다음
public DataSet GetSearchEmpResults(string emp_fn) 
{ 
string selectCommand = @"select 
           emp.first_name as 'First Name', 
           emp.last_name as 'Last Name' 
           from Employees emp 
           where emp.first_name = '" + emp_fn + "';"; 
SqlCommand command = new SqlCommand(selectCommand, this.Connection); 
DataSet ds = new DataSet("Results"); 
SqlDataAdapter da = new SqlDataAdapter(command); 
da.Fill(ds, "Results"); 
return ds; 
} 

:

DataSet results = db.GetSearchEmpResults(fristName, lastName); //this depends on your results 
dataGridViewResults.DataSource = results; 
dataGridViewResults.DataMember = "Results"; 

dataGridViewResults은 검색 결과가 표시 될 데이터 격자의 이름입니다.
결과를 받으려면 데이터베이스에 연결해야합니다.

관련 문제