簡體   English   中英

C#垂直方向的DataGrid:更改列名

[英]C# vertically oriented DataGrid: change column names

我目前正在使用一種技術,該技術是在互聯網之間閑逛時發現的,用於翻轉C#中SharePoint Web部件的DataGrid方向。 它工作正常,可以從SQL Server 2005數據庫中提取數據並將其顯示在DataGrid中。 我想知道是否有一種簡單的方法來更改列名,或者使用db中的擴展屬性,或者可以手動設置它們(盡管我有很多列,所以我更喜歡將擴展屬性添加到數據庫並顯示這些屬性以代替字段名稱)。

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

我也想知道這是否是處理翻轉數據網格方向的“正確”方法,或者至少在一般情況下是否存在更好的方法。

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

SELECT fldCustomerName AS [客戶名稱]

這樣很好。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM