0
我有两个表嵌套(主视图)来表示我的数据。父表在下面称为dtm,子表为dtd。我想为一些单元格着色,并在网上阅读后,似乎我需要使用称为DataGridView的东西。但是,我有点不确定如何修改我目前必须使用此DataGridView。将datagridview添加到数据集以可视化地控制主视图?
有人可以帮忙吗?我省略了繁琐的列和数据的插入部分:
private DataTable CreateData()
{
DataTable dtm = new DataTable();
//Add the primary key first
DataColumn dcmpk = dtm.Columns.Add();
//Add the remaining columns
dtm.Columns.Add();
dtm.PrimaryKey = new DataColumn[] { dcmpk };
//Add the data rows
dtm.Rows.Add();
//The table to be nested
DataTable dtd = new DataTable();
//Add foreign key first
DataColumn dcdfk = dtd.Columns.Add();
//Add the remaining columns for the child elements
dtd.Columns.Add();
//Add all the data for the child elements
dtd.Rows.Add();
DataSet ds = new DataSet();
ds.Tables.AddRange(new DataTable[] { dtm, dtd });
ds.Relations.Add("Relationship", dcmpk, dcdfk);
return dtm;
}
这是我已经试过了,但它告诉我列[0]为空:
DataSet ds = new DataSet();
ds.Tables.AddRange(new DataTable[] { dtm, dtd });
ds.Relations.Add("Relationship", dcmpk, dcdfk);
DataGridView dgv1 = new DataGridView();
dgv1.DataSource = ds.Tables[0];
dgv1.Columns[0].DefaultCellStyle.BackColor = Color.Red;
请参阅上面的编辑^ – mezamorphic 2012-01-05 15:30:37
设置数据源后调用'dgv1.DataBind();'。这应该解决它。 – 2012-01-05 15:40:06
那里似乎没有名为DataBind()的方法? – mezamorphic 2012-01-05 15:44:54