我目前正在使用我在浏览互联网时发现的一种技术,用于在 C# 中为 SharePoint Web 部件翻转 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;
}
我还想知道这是否是处理翻转数据网格方向的“正确”方法,或者至少是否有更好的方法来做这件事。