简体   繁体   中英

Adding column in a datatable with a dynamic column name in C#

DataTable dt = new DataTable();
var dr = dt1.Date; 
String rr = Convert.ToString(dr); 
DataColumn dc1=new DataColumn(); 
dc1.ColumnName = rr; dt.Columns.Add(dc1);

And if i add datarow after this like

dt.Rows.Add("hello","hello1","hello2");  
dataGrid1.ItemsSource = dt.DefaultView;

the data is not displayed in the grid . If i comment the line

dc1.ColumnName = rr;

the values are displayed properly But i want the colmn name to be the date that is "dt1" here

pleae note that the dt1 are the date values which is dynamic and it will be incremented in each loop. like

dt1 = dt1.AddDays(1);

Please help

Without seeing your Xaml for the data grid it's difficult to be sure but I imagine that you've specified a field name for the date column in your xaml.

To resolve this, you'll need to set AutoGenerateColumns=True and let the grid automatically find the field name.

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