简体   繁体   中英

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

有没有将行标题信息存储在数据表中,这样当我将它绑定到datagridview时,它会自动显示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;
}

Linqpad程序的结果

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM