[英]Add a datagridview to a dataset to visually control master-view?
我有兩個嵌套的表(主視圖)來表示我的數據。 父表在下面稱為dtm,子表在dtd中。 我想給一些單元格上色,在網上閱讀后,我似乎需要使用一種叫做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;
}
這是我嘗試過的方法,但它告訴我Columns [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;
數據集和數據表僅僅是數據。 它們代表內存中數據庫中數據的子集。 數據表示為行和列。
要向用戶顯示數據,您需要將數據集/表中的數據綁定到UI控件。 用戶控件的一種類型是DataGridView。 網上有很多介紹示例,介紹如何將數據集綁定到gridview。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.