C# DataGridView ComboBox Binding Issues
I've got a C# program that I'm working on but 开发者_JS百科I can't get the ComboBoxColumn to display as I want it to.
I have two tables. One Contains a list of price. (Columns: PriceID, PartID, Price, MinimumQuantity, CustomerID, ExpiryDate) The other table, a list of parts contains (Columns: PartID, PartNumber). What
I want to do is display the PartNumber in a ComboBoxColumn with the PartID as the value. But I want to corresponding part number selected automatically for each price row.
Does this make sense!?
What I am currently getting in each row is the prices, quantities and expiry dates and ComboBoxColumn with the parts list populating the combobox but no value selected.
Please can someone help me with this.
Please see the code below.
DbConnection dbConn = new DbConnection(getConnectionStr());
dbConn.execQuery("EXEC RBS_CustomerSpecificPriceListSelect " + customerID + ";", "CustomerPriceList");
dbConn.execQuery("EXEC RBS_PartsSelect;", "Parts");
DataTable dtCustomerPrices = dbConn.getDataTable("CustomerPriceList");
DataTable dtParts = dbConn.getDataTable("Parts");
dgvCustomerPrices.AutoGenerateColumns = false;
dgvCustomerPrices.DataSource = dtCustomerPrices;
dgvCustomerPrices.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
SetupDGVColumns(dtCustomerPrices, dtParts);
Then to generate the columns.
private void SetupDGVColumns(DataTable dtCustomerPrices, DataTable dtParts)
{
// Parts Dropdown
DataGridViewComboBoxColumn cboPartsColumn = new DataGridViewComboBoxColumn();
cboPartsColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cboPartsColumn.DataSource = dtParts;
cboPartsColumn.HeaderText = "PartNumber";
cboPartsColumn.DisplayMember = "PartNumber";
cboPartsColumn.ValueMember = "PartID";
cboPartsColumn.DataPropertyName = "PartID";
cboPartsColumn.Width = 110;
cboPartsColumn.AutoComplete = true;
dgvCustomerPrices.Columns.Add(cboPartsColumn);
// MinimumQuantity Textbox
DataGridViewTextBoxColumn tbcMinimumQuantityColumn = new DataGridViewTextBoxColumn();
tbcMinimumQuantityColumn.DataPropertyName = "MinimumQuantity";
tbcMinimumQuantityColumn.HeaderText = "MinimumQuantity";
tbcMinimumQuantityColumn.ValueType = typeof(double);
tbcMinimumQuantityColumn.Width = 140;
tbcMinimumQuantityColumn.DefaultCellStyle.Format = "#####0.00";
dgvCustomerPrices.Columns.Add(tbcMinimumQuantityColumn);
// Price Textbox
DataGridViewTextBoxColumn tbcPriceColumn = new DataGridViewTextBoxColumn();
tbcPriceColumn.DataPropertyName = "Price";
tbcPriceColumn.HeaderText = "Price";
tbcPriceColumn.ValueType = typeof(double);
tbcPriceColumn.Width = 100;
tbcPriceColumn.DefaultCellStyle.Format = "#####0.00";
dgvCustomerPrices.Columns.Add(tbcPriceColumn);
// ExpiryDate Textbox
DataGridViewTextBoxColumn tbcExpiryDateColumn = new DataGridViewTextBoxColumn();
tbcExpiryDateColumn.DataPropertyName = "ExpiryDate";
tbcExpiryDateColumn.HeaderText = "ExpiryDate";
tbcExpiryDateColumn.ValueType = typeof(string);
tbcExpiryDateColumn.DefaultCellStyle.Format = "d";
dgvCustomerPrices.Columns.Add(tbcExpiryDateColumn);
}
Ok so this was complete idiocy on my part. The SQL query that was selecting the combo columns data did not have the corresponding row ID in the SELECT clause.
Thank you all for your help though! I feel silly!
精彩评论