C# 垂直方向的 DataGrid:更改列名称
我目前正在使用一种在互联网上探索时发现的技术,用于在 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;
}
我还想知道这是否是处理翻转数据网格方向的“正确”方法,或者至少是否有更好的方法来做到这一点。
I'm currently using a technique I found while poking around the interwebs for flipping a DataGrid's orientation in C# for a SharePoint web part. It's working correctly, pulling data from a SQL Server 2005 database and displaying it in the DataGrid. I'd like to know if there is a simple way to change the column names, either using an extended property in the db, or, alternately, if I can manually set them (although I have lots of columns, so I'd prefer to add an extended property to the db and display those in place of the field names).
// 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);
Here is the FlipDataSet method:
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;
}
I'm also wondering if this is the "right" way to handle flipping the datagrid's orientation, or at the very least if there is a better way to do it in general.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我决定通过更改 SQL SELECT 语句来重命名列,以便每个字段都在那里命名。例如:
这很好用。
I decided to rename the columns by altering my SQL SELECT statement so that each field is named there. For instance:
This works just fine.
重命名数据网格中的列的另一种方法是
Another way to rename column in a data grid is