当我在转置之前将我的数据表发送到dataGrid时,一切正常。但在转置数据表后,我的列名称获取dataTime值,其余的datagrid表为空。
转置方法:
private DataTable Transpose(DataTable dt)
{
DataTable dtNew = new DataTable();
//adding columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
dtNew.Columns.Add(i.ToString());
}
//Changing Column Captions:
//dtNew.Columns[0].ColumnName = " ";
for (int i = 0; i < dt.Rows.Count; i++)
{
//For dateTime columns use like below
dtNew.Columns[i + 1].ColumnName = Convert.ToDateTime(dt.Rows[i].ItemArray[0].ToString()).ToString("MM/dd/yyyy");
//Else just assign the ItermArry[0] to the columnName prooperty
}
//Adding Row Data
for (int k = 1; k < dt.Columns.Count; k++)
{
DataRow r = dtNew.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
r[j] = dt.Rows[j - 1][k];
dtNew.Rows.Add(r);
}
return dtNew;
IMG: my dataGrid before transpose
IMG: my datagrid after transpose
我尝试了多种方法来解决这个问题,但我不知道为什么会发生这种情况。看起来如果我在其工作日期旁边更改另一个字符串的列名。
我的数据网格代码是:
dataGrid_x.ItemsSource = x.Tables[listBox_sites.SelectedItem.ToString()].AsDataView();
dataGrid_y.DataContext = y.Tables[listBox_sites.SelectedItem.ToString()].DefaultView;
dataGrid_t.DataContext = t.Tables[listBox_sites.SelectedItem.ToString()].DefaultView;
dataGrid_h.DataContext = h.Tables[listBox_sites.SelectedItem.ToString()].DefaultView;