Hi C Pierce,
Before we go any further, we need to define a table to store the information about our program jobs. The structure of the table can be like below:
create table jobsInfo
(
id int, --job id.
name varchar(250), --name of the job
weekNum int, --week number of the job, such as 0(Sunday)
isAm bit --am or pm(1:am, 0:pm)
)
When the form loads, we need to create and fill a DataTable of the type above. Then we need to create a BindingSource and set its DataSource property to the DataTable we filled just now. With all this done, we can bind this DataSource to the DataGridViewComboBoxColumn
private DataGridViewComboBoxColumn _comboboxCol = null;
private BindingSource _comboBoxSrc = null;
private void Form6_Load(object sender, EventArgs e)
{
//Initialize the DataGridView and the DataGridViewComboBoxColumn here. I ignore the code details.
//Get and fill the table.
DataTable jobsTable = GetJobsInfoTable();
//Create and bind the BindingSource to the ComboBox column.
_comboBoxSrc = new BindingSource();
_comboBoxSrc.DataSource = jobsTable;
_comboboxCol.DataSource = _comboBoxSrc;
//Set these properties to indicate which field is used to store the value and
//which field is used to show on the DataGridView.
_comboboxCol.ValueMember = "id";
_comboboxCol.DisplayMember = "name";
}
When the week or am/pm fields on the form is changed, we need to reset the binding source of the ComboBox column to reset its item list. The method below show how to do that:
//Reset the binding source of the ComboBox column.
private void ResetDataSource(DayOfWeek week, bool isAm)
{
//Get week and isAm numbers.
int weekNum = (int)week;
int isAmBit = isAm ? 1 : 0;
//Create the filter to filter the binding source.
string filter = string.Format("weekNum={0} and isAm={1}", weekNum, isAmBit);
//Set the filter, the list in ComboBox column would also be changed.
_comboBoxSrc.Filter = filter;
}
You can get more about DataGridViewComboBoxColumn from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx
Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.