2012-08-15 5 views
0

저는 C# 프로그래머입니다. 현재, 나는 datagrid - 뷰어의 SQL 데이터에 5 열 중 3 개 (fileId, filePath, authorName, fileContent, DateSend)를 추가하려고합니다. fileId 및 fileContent 열은 숨겨집니다. 고마워요!datagridview는 SQL 데이터베이스에서 선택된 행을 표시합니까?

SqlDataReader reader = cmd.ExecuteReader();  
gridView1.DataSource = reader; 
를 사용하여 그리드를 채우는 기억

OnRowCreated = "gridView1_RowCreated"

:

 con = new SqlConnection("Data Source=LEO-PC\\SQLEXPRESS;Initial Catalog = datashare;Integrated Security = True"); 
     cmd = new SqlCommand("select * from maintable", con); 
     con.Open(); 
     SqlDataReader sqlRead; 
     try 
     { 
      sqlRead = cmd.ExecuteReader(); 
      while (sqlRead.Read()) 
      { 
       //Adding to datagrid 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      con.Close(); 
     } 
+0

초보자는 SQLDataSource 컨트롤을 체크 아웃 할 수 있습니다. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx –

답변

0

는이 개 필드를 그리드에 OnRowCreated 이벤트를 만들 숨기려면

이어야하며 gridview 속성은이어야합니다.

protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
      if (e.Row.RowType == DataControlRowType.Header) 
      { 
       for (int i = 0; i < e.Row.Cells.Count; i++) 
       { 
        DataControlFieldCell cell = 
         (DataControlFieldCell) e.Row.Cells[i]; 


        if (cell.ContainingField.HeaderText == "fileId") 
        { 
         e.Row.Cells[i].Visible = false; 
         cell.Visible = false; 
        } 

        if (cell.ContainingField.HeaderText == "fileContent") 
        { 
         e.Row.Cells[i].Visible = false; 
         cell.Visible = false; 
        } 
       } 
      } 

      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       for (int i = 0; i < e.Row.Cells.Count; i++) 
       { 
        DataControlFieldCell cell = 
         (DataControlFieldCell) e.Row.Cells[i]; 

        if (cell.ContainingField.ToString() == "fileId") 
        { 
         cell.Visible = false; 
        } 

        if (cell.ContainingField.ToString() == "fileContent") 
        { 
         cell.Visible = false; 
        } 
       } 
      } 
} 

이 가장 effecient 방법이되지 않을 수도 있지만, 코드를 사용하여 목적에 부합 :3210

그런 다음 다음과 같이 이벤트를 만들 수 있습니다. 하나는 열이 바인딩되어있을 때 gridview 템플릿을 사용하여 열을 숨길 수 있어야합니다.

관련 문제