2010-02-22 1 views
1

현재 SharePoint 웹 파트에 대해 C#에서 DataGrid의 방향을 바꾸기 위해 interwebs를 파고들 때 발견 된 기술을 사용하고 있습니다. SQL Server 2005 데이터베이스에서 데이터를 가져 와서 DataGrid에 표시하면 올바르게 작동합니다. 컬럼의 이름을 변경하는 간단한 방법이 있는지 알고 싶습니다. 확장 된 속성을 사용하거나 또는 수작업으로 설정할 수 있다면 (컬럼이 많기 때문에 선호합니다. 확장 속성을 db에 추가하고 필드 이름 대신 표시합니다. 더 나은가있는 경우, 또는 적어도C#의 세로 방향 DataGrid : 열 이름 변경

public DataSet FlipDataSet(DataSet my_DataSet) 
{ 
    DataSet ds = new DataSet(); 
    foreach (DataTable dt in my_DataSet.Tables) 
    { 
     DataTable table = new DataTable(); 
     for (int i = 0; i <= dt.Rows.Count; i++) 
     { 
      table.Columns.Add(Convert.ToString(i)); 
     } 
     DataRow r = null; 
     for (int k = 0; k < dt.Columns.Count; k++) 
     { 
      r = table.NewRow(); 
      r[0] = dt.Columns[k].ToString(); 
      for (int j = 1; j <= dt.Rows.Count; j++)  
       r[j] = dt.Rows[j - 1][k]; 
      table.Rows.Add(r); 
     } 
     ds.Tables.Add(table); 
    } 
    return ds; 
} 

이이 데이터 그리드의 방향을 틀지 처리 할 수있는 "권리"방법 인 경우 나 또한 궁금하네요 : 여기

// Create a new data adapter and fill a dataset with the above SQL data. 
SqlDataAdapter da = new SqlDataAdapter(); 
da.SelectCommand = cmd; 
DataSet ds = new DataSet(); 
da.Fill(ds, "Bobst Specs"); 
// Flip the dataset to vertical orientation. 
DataSet flipped_ds = FlipDataSet(ds); 
DataView dv = flipped_ds.Tables[0].DefaultView; 
// Bind the dataset to a datagrid and display. 
DataGrid outputGrid = new DataGrid(); 
outputGrid.DataSource = dv; 
outputGrid.DataBind(); 
outputGrid.ShowHeader = false; // Remove the integer headings. 
outputGrid.AutoGenerateColumns = false; 
Controls.Add(outputGrid); 

는 FlipDataSet 방법입니다 일반적으로 그것을하는 방법.

답변

0

필자는 각 SQL SELECT 문을 변경하여 열의 이름을 바꾸어 각 필드의 이름을 지정하기로 결정했습니다. 예를 들어 :

SELECT fldCustomerName AS [고객 이름]

이 잘 작동합니다.

0

데이터 그리드의 칼럼의 이름을 변경하는 또 다른 방법은

mygrid.Columns["EmpID"].HeaderText = "Employee ID"; 
이다