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

发布于 2024-08-22 21:55:14 字数 1605 浏览 8 评论 0原文

我目前正在使用一种在互联网上探索时发现的技术,用于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-08-29 21:55:14

我决定通过更改 SQL SELECT 语句来重命名列,以便每个字段都在那里命名。例如:

选择 fldCustomerName AS [客户
名称]

这很好用。

I decided to rename the columns by altering my SQL SELECT statement so that each field is named there. For instance:

SELECT fldCustomerName AS [Customer
Name]

This works just fine.

想挽留 2024-08-29 21:55:14

重命名数据网格中的列的另一种方法是

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

Another way to rename column in a data grid is

mygrid.Columns["EmpID"].HeaderText = "Employee ID";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文