2010-02-22 13 views
1

我目前正在使用我發現的技術,同時在CWeb中翻閱DataWeb的方向,用於SharePoint Web部件。它正常工作,從SQL Server 2005數據庫中提取數據並將其顯示在DataGrid中。我想知道是否有一種簡單的方法來更改列名稱,無論是使用數據庫中的擴展屬性,或者,如果我可以手動設置它們(儘管我有很多列,所以我更喜歡將擴展屬性添加到數據庫並顯示這些字段名稱)。C#垂直導向的DataGrid:更改列名

// 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方法:,或至少是

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; 
} 

我也想知道,如果這是「正確」的方式來處理翻轉DataGrid的方向,如果有更好的一般做法。

回答

0

我決定通過更改我的SQL SELECT語句來重命名列,以便每個字段都在那裏命名。例如:

SELECT fldCustomerName AS [客戶 名稱]

這一切正常。

0

的另一種方式在數據網格中重命名列是

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