English 中文(简体)
C# vertically oriented DataGrid: change column names
原标题:

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.

最佳回答

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.

问题回答

Another way to rename column in a data grid is

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签