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