I have a datagrid that displays info from 2 tables - the chemicals table contains chemicalID (PK), description, etc. and dailyInventory contains inventoryID (PK), date, chemicalID (FK), etc. In my first pass at displaying this data I used a JOIN statement. When I finally got it to display the way I wanted, I ran into issues trying to update with the OLEDBAdapter. After some investigation I learned that Join statements should be avoided and instead use a DataRelation.
I made all the adjustments needed, but can't get the datagrid to bind to the relation so I can display the "description" column from the chemicals table with my data from the dailyInventory table. You'll note that I have commented out quite a few lines trying various options. While it isn't throwing any errors, I can only get the info from dailyInventory to display with no control over column headings, and no description from the chemicals table.
In addition to getting the "description" field to display with dailyInventory, I need to do a RowFilter based upon date. AND if there are no entries for a particular date then I to list only the chemical descriptions to simplify data entry as well. I'm fairly new to .NET so...
I made all the adjustments needed, but can't get the datagrid to bind to the relation so I can display the "description" column from the chemicals table with my data from the dailyInventory table. You'll note that I have commented out quite a few lines trying various options. While it isn't throwing any errors, I can only get the info from dailyInventory to display with no control over column headings, and no description from the chemicals table.
In addition to getting the "description" field to display with dailyInventory, I need to do a RowFilter based upon date. AND if there are no entries for a particular date then I to list only the chemical descriptions to simplify data entry as well. I'm fairly new to .NET so...
Code:
// chemicals table
this.oleAdpChemicals.SelectCommand = this.oleCmdChemicals;
oleAdpChemicals.Fill(dsChemicalTracking, "chemicals");
dvChemicals = dsChemicalTracking.Tables["chemicals"].DefaultView;
// dailyInventory table and dataview
this.oleAdpDailyInv.SelectCommand = this.oleCmdDailyInv;
oleAdpDailyInv.Fill(dsChemicalTracking, "dailyInventory");
dvDailyInv = dsChemicalTracking.Tables["dailyInventory"].DefaultView;
strFilter = "date='" + dtpDate.Value.ToShortDateString() + "'";
dvDailyInv.RowFilter = strFilter;
// add relations
DataColumn chemicalCol = new DataColumn();
DataColumn dailyInvCol = new DataColumn();
chemicalCol = dsChemicalTracking.Tables["chemicals"].Columns["chemicalID"];
dailyInvCol = dsChemicalTracking.Tables["dailyInventory"].Columns["chemicalID"];
DataRelation chemicalsToDailyInv = new DataRelation("chemicalsToDailyInv", chemicalCol, dailyInvCol);
dsChemicalTracking.Relations.Add(chemicalsToDailyInv);
dgDailyInv.SetDataBinding(dsChemicalTracking, "chemicals.chemicalsToDailyInv");
//dgDailyInv.DataSource = dvDailyInv;
//dgDailyInv.DataMember = "dailyInventory";
// Declare DataGridTableStyle
//tsDailyInv.MappingName = dsChemicalTracking.Relations["chemicalsToDailyInv"].RelationName;
//tsDailyInv.MappingName = dsChemicalTracking.Tables["dailyInventory"].TableName;
tsDailyInv.AlternatingBackColor = Color.FromArgb(255, 153, 102);
tcDate.MappingName = "date";
tcDate.HeaderText = "Date";
tcDate.Width = 100;
tcDescription.MappingName = "chemicals.description";
tcDescription.HeaderText = "Chemical";
tcDescription.Width = 125;
tcAmtInStock.MappingName = "amtInStock";
tcAmtInStock.HeaderText = "Amt In Stock";
tcAmtInStock.Width = 100;
tsDailyInv.GridColumnStyles.Add(tcDate);
tsDailyInv.GridColumnStyles.Add(tcDescription);
tsDailyInv.GridColumnStyles.Add(tcAmtInStock);
// Add the DataGridTableStyle instance to the GridTableStylesCollection
dgDailyInv.TableStyles.Add(tsDailyInv);
dgDailyInv.Expand(-1);
// get chemical names for dailyInventory entry
//if (dvDailyInv.Count.Equals(0))
//{
// strFilter = "discontinued = 0";
// dvChemicals.RowFilter = strFilter;
// foreach (DataRowView drChemicals in dvChemicals)
// {
//drDailyInv = dvDailyInv.AddNew();
//drDailyInv["date"] = dtpDate.Value.ToShortDateString();
//drDailyInv["description"] = drChemicals["description"];
//drDailyInv["amtInStock"] = 0;
//drDailyInv.EndEdit();
// }
//}