1

私は現在、SharePoint Web パーツの C# で DataGrid の向きを反転するためにインターウェブを調べているときに見つけた手法を使用しています。これは正しく機能しており、SQL Server 2005 データベースからデータを取得して 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;
}

また、これがデータグリッドの方向の反転を処理する「正しい」方法であるかどうか、または少なくとも一般的にそれを行うためのより良い方法があるかどうかも疑問に思っています。

4

2 に答える 2

0

SQL SELECTステートメントを変更して列の名前を変更し、各フィールドに名前が付けられるようにすることにしました。例えば:

SELECT fldCustomerNameAS[顧客名]

これは問題なく機能します。

于 2010-02-23T16:33:57.540 に答える
0

データグリッドの列の名前を変更する別の方法は、

mygrid.Columns["EmpID"].HeaderText = "Employee ID";
于 2011-04-20T22:03:49.957 に答える