Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Using a form field to control the datasource of a combobox on a datagridview
 

Using a form field to control the datasource of a combobox on a datagridview

I am being tasked with creating a tracking system for tracking the status of various program jobs (JCL/COBOL) that run on a daily basis.  I am trying to set up a vb.net application that can be exported out and set up on the client's machine (which in this case would be other co-workers).  I am very new to .NET and I have tried to word many searches on what I want to do vs. what actually needs to be done.

I currently have a Win form with DataViewGrid which displays the table I am updating with the job completions.  One of the fields is a combobox and linked to a different datasource(tables).  What I am trying to do is be able to display a certain query for the "job name" combobox that is part of this datagridview based on the day of the week and whether it is AM or PM.  The form would allow me to select whether its AM or PM and the day would be captured when the form is loaded.  The reason for this is we have certain jobs that run in the am or pm and also based on the day of the week.  This would limit the jobs displayed just to that particular time of day and day of week.  My problem is how does one go about that?  I believe this to be an easy problem, just lacking the skills and knowledge on where to start looking.  I can update the datagrid which I created by using the toolbox(drag and drop)and update the query directly.  Does anyone have any suggestions or perhaps better wording on what I am trying to do here?  I have Googled a number of different combonations but none seem to give me a clear example of what I am trying to do.

Appreciate any assistance or guidance anyone can give me regarding this or if more information is needed will be happy to provide.
C Pierce  Monday, October 05, 2009 7:50 PM

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.
Aland Li  Thursday, October 08, 2009 3:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Combo Box won't update when its Data Source is changed
• DataGrid row insertion
• How to add functionality to "Data Contract" objects?
• question on negative identity keys created as placeholders when inserting into datatable
• How to execut an SQL statement using BindingNavigator
• Problem with database connectivity
• DataBinding Enums and Images
• custom myToolStripTextbox
• how to bind datareader to datagridview in vb.net2005
• how do i send a mail from vb.net?