Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 row headers in c#?

like image 451
NeedHelp Avatar asked May 06 '11 22:05

NeedHelp


2 Answers

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;
}

Result of Linqpad programm

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.

like image 180
surfmuggle Avatar answered Nov 01 '22 04:11

surfmuggle


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.

like image 39
nbushnell Avatar answered Nov 01 '22 04:11

nbushnell