开发者

row headers on a datatable - to be displayed in a datagridview

开发者 https://www.devze.com 2023-03-03 18:47 出处:网络
Is there anyway to store row header information in a datatable so that when i bind it to a datagridview, it will automatically display both the column and 开发者_如何学Crow headers in c#?Linqpad Demo-

Is there anyway to store row header information in a datatable so that when i bind it to a datagridview, it will automatically display both the column and 开发者_如何学Crow headers in c#?


Linqpad Demo-Program

As far as i understood you would like to add the column name as values into the datatable / the datagridview. The following is a Linqpad-Program you can easily copy paste into Linqpad to play around. The code adds the column-names to the first row to the datatable. You can easily bind this datatable to a gridview - but beware that each column of the datatable must be of type string.

void Main()
{
    GetDataTable().Dump();
}

public DataTable GetDataTable()
{
    var dt = new DataTable();   
    dt.Columns.Add("Id", typeof(string)); // dt.Columns.Add("Id", typeof(int));
    dt.Columns["Id"].Caption ="my id";  
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Job", typeof(string));
    dt.Rows.Add(GetHeaders(dt));
    dt.Rows.Add(1, "Janeway", "Captain");
    dt.Rows.Add(2, "Seven Of Nine", "nobody knows");
    dt.Rows.Add(3, "Doctor", "Medical Officer");
    return dt;
}

public DataRow GetHeaders(DataTable dt)
{
    DataRow dataRow = dt.NewRow();      
    string[] columnNames = dt.Columns.Cast<DataColumn>()
                                 .Select(x => x.ColumnName)
                                 .ToArray();    
    columnNames.Dump();
    dataRow.ItemArray = columnNames;
    return dataRow;
}

row headers on a datatable - to be displayed in a datagridview

Update 2019-06 with additional explanation and alternative code

The method GetHeaders is not the simplest option to get the headers. Previoulsy the extension method Cast<TResult>(IEnumerable) was used on the DataColumnCollection-Class An alternative would be to just iterate over the collection - this what is done In GetHeadersNew T

public DataRow GetHeadersNew(DataTable dt)
{
    DataRow row = dt.NewRow();      
    DataColumnCollection columns = dt.Columns;
    for (int i = 0 ;i <columns.Count ;i++)
    {
         row[i] = columns[i].ColumnName;
    }       
    return row;
}

This is likely more efficient because less objects and methods are involved.


As long as you can create them with the code based on the data in the row I would just add them at run time using c#. Add a column to the datatable and run through it with a foreach loop. As long as there are not too many rows this code will execute very quickly:

DataTable dt = new DataTable();
// code here to get your datatable
dt.Columns.Add("rowheader");
foreach (DataRow r in dt.Rows)
{
    r["rowheader"] = "my nice row header";
}

Then output the new column rowheader as the first cell in the grid.

Another solution is to use the sql query to return an 'extra' column in the result set. for example:

Select *, 'my nice row header' as rowheader from myTable

In this way you make SQL do all the work.

0

精彩评论

暂无评论...
验证码 换一张
取 消