I have two tables nested (master view) for representing my data. The parent table is called dtm below and the child table is dtd. I wanted to color some of the cells and after reading online it seemed that I needed to use something called a DataGridView. However, I am a little unsure how to modify what I currently have to utilise this DataGridView.
Could someone please help? I have omitted the tedious column and data insertion parts:
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;
}
This is what I have tried, but it was telling me Columns[0] was null:
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;
datasets and datatables are just data. they represent a subset of data from the database in memory. the data is represented as rows and columns.
to display the data to the user you need to bind the data in the dataset/table to UI controls. One type of user control is a DataGridView. there are plenty of introduction examples on the web on how to bind a dataset to a gridview.