我目前正在使用一种技术,我在寻找互联网时,在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;
}
我也想知道这是否是处理翻转数据网格方向的“正确”方式,或者至少是否有更好的方法来执行此操作。
答案 0 :(得分:0)
我决定通过更改SQL SELECT语句来重命名列,以便在那里命名每个字段。例如:
SELECT fldCustomerName AS [Customer 名]
这很好用。
答案 1 :(得分:0)
在数据网格中重命名列的另一种方法是
mygrid.Columns["EmpID"].HeaderText = "Employee ID";