Link to home
Start Free TrialLog in
Avatar of LenTompkins
LenTompkinsFlag for United States of America

asked on

How to populate drop down list where number is not previously listed in datagrid?

I have a list of bill numbers (column name BillNO) that I want to populate a combo box and restrict the list of items to all bill numbers not listed in the datagrid. How do I do this? See attached screen print of example. When we put logic in the query for the combo box to exclude the items already in the table, when the data shows on the form the BillNO field is blank.

Thank you for your assistance.
ExpertsExchangeComboBox.docx
Avatar of BasiKobrA
BasiKobrA

Depending on what exactly you need, you might use this to restrict the elements of newly created DataGridViewComboBox items. Maybe this code could fit in an event-fired method like RowsAdded to know which combobox list to restrict.
// The collection with bill numbers
List<string> bill_no = new List<string>() { "nb1", "nb2", "nb3" }; 

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
    foreach (DataGridViewCell dgvc in dgvr.Cells)
        if (dgvc.ColumnIndex == 1)
            bill_no.Remove(dgvc.Value);

// Naive implementation
yourCurrentDGVComboBox.Items.Clear;
foreach (string nb in bill_no)
    yourCurrentDGVComboBox.Items.Add(nb);

Open in new window

Avatar of LenTompkins

ASKER

I added the code like you suggested for looping through the datatable rows adding them to the combo box of the datagridview; however, all existing rows in the datagridview show a blank in the BillNO.
Avatar of Fernando Soto
What version of Visual Studio .Net and the .Net Framework are you using?
Fernando,
We are using Visual Studio 2008 and .NET Framework 3.5
Hi LenTompkins;

I will assume that the source of the DataGridView is a DataTable and the List of bill numbers called lstBillNo and the combo box to be populated is comboBox1 then the code in the code snippet should do what you need.

Fernando
List<String> ddList = lstBillNo.Except(dt.AsEnumerable().Select(b => b.Field<String>("BillNo"))).ToList();

comboBox1.DataSource = ddList;

Open in new window

Fernando,
For me, the above code is still not working. When I run the code, I get the following error:
"The following exception occurred in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event."
Please let me know what you suggest.
Thanks.
Can you upload the code file/s so I can try to see what is going on.
1. Perhaps you should check that the code to populate the list does not return NULL before setting it to the DataSource property of the comboBox.
2. Where in the code you supplied does the exception occur: line 1 or line 3?
FernandoSoto:
Thanks for your assistance here is some of the code. Does this help? Let me know if you need more information. Sorry I did not respond sooner. I tried to fix myself and other project took higher priority. Thanks.

        private void LoadDataSet()
        {
            //Instantiate Dataset & Table Adapters
            int recordsReturned = 0;

            this.m_ds_RevClaimOpenARDataset = new RevClaimOpenARDataSet();
            this.m_adpRevClaimComments = new RevClaimCommentsTableAdapter();
            this.m_adpRevClaimOpenAR = new RevClaimOpenARTableAdapter();

            try
            {
                //fill the table adapter (columns)
                recordsReturned =
                    this.m_adpRevClaimComments.FillBySiteNO
                       (this.m_ds_RevClaimOpenARDataset.RevClaimComments, mySiteNo);
                updateBillNODropDownList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void InitializeDataGrid()
        {
            this.m_srcRevClaimComments = new BindingSource();
            this.gridRevClaimOpenAR.AutoGenerateColumns = false;

            //DataSource for Binding Sources are the datasets
            this.m_srcRevClaimComments.DataSource = this.m_ds_RevClaimOpenARDataset.RevClaimComments;
            this.gridRevClaimOpenAR.DataSource = this.m_srcRevClaimComments;
            //Give each column a data source
            this.navReversedClaimOpenAR.BindingSource = this.m_srcRevClaimComments;

            ////Set Binding for "BillNO" Drop-down column
            //this.BillNOCol.DataPropertyName = "BillNO";
            //this.BillNOCol.DisplayMember = "BillNO";
            //this.BillNOCol.ValueMember = "BillNO";
        }

        private void updateBillNODropDownList()
        {
            int recordsReturned = this.m_adpRevClaimOpenAR.FillBySiteNO
                      (this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR, mySiteNo);
            if (recordsReturned == 0)
                this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR.Rows.Add(99999);
            //this.BillNOCol.DataSource = this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR.Rows;
            IEnumerable<int> lstBillNo = this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR
                .Select(b => b.Field<int>("BillNO")).ToList();
            List<int> ddList = lstBillNo.Except(this.m_ds_RevClaimOpenARDataset.RevClaimComments.AsEnumerable()
                .Select(b => b.Field<int>("BillNo"))).ToList();
            this.BillNOCol.DataSource = ddList;
        }

Orcbighter:
1. If there is no data, one record will be added with all 9's, so should never return NULLs.
2. Not sure which line is causing the problem. I see the error when the data is trying to be displayed to the screen.
Thanks.
Hi LenTompkins;

The code I posted was executing against the List collection and the DataTable and not against the DataGridview so I am not understanding the error the system returned. Can you post the implementation you used from the sample I posted above.

Fernando
See above for more details of the code, but this is what I tried to create from your code:
IEnumerable<int> lstBillNo = this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR
                .Select(b => b.Field<int>("BillNO")).ToList();
            List<int> ddList = lstBillNo.Except(this.m_ds_RevClaimOpenARDataset.RevClaimComments.AsEnumerable()
                .Select(b => b.Field<int>("BillNo"))).ToList();
            this.BillNOCol.DataSource = ddList;

And then the gridview's datasource is set as follows:
this.gridRevClaimOpenAR.DataSource = this.m_srcRevClaimComments;

The error I am receiving is still the same:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
Thanks for you assistance.
Hi LenTompkins;

Is you DataSet/DataTable create as a Strongly type object using the "Add new Data Source" in VS? if so I would need to test to see if any changes need to be made to the syntax.

Is BillNOCol your listbox? If so does it list the values you expected from the Except list?

It seems that this.m_srcRevClaimComments is what is causing the error which does not come into play in getting the List of Except.

Fernando
Fernando,
I will answer your questions as best as I can. Unfortunately, I am new to Visual Studio.

The dataset (RevClaimOpenARDataSet) was created by dragging both tables ( RevClaimComments, RevClaimOpenAR ) from the Server Connection within Visual Studio. Would this be a strongly typed dataset? Both of the tables have a field for BillNo.

Yes, BillNOCol is the name assigned to the combo drop down box which is a field in the datagrid.

I am trying to set combo drop down list box ( BillNOCol) available values to ddList which is the code you provided containing the list of BillNO's found in RevClaimOpenAR removing the BillNO's that are listed in RevClaimComments. The code you provide does this with no problem. Next, the data source to the datagrid is set to RevClaimOpenAR (has columns BillNo, SiteNo, Comments). You will see the details of this in the code provided before. Is this correct?

Thanks for your assistance.
Hi LenTompkins;

In answer to your question, "Would this be a strongly typed dataset? ", Yes that is a strongly typed dataset and tables.

Glad to know that the code I posted does work for you.

Now to the real issue. The problem seems to be that now you want to update the combo boxes in the grid with the list you just built. Would it be possible to upload your project and a copy of a test database to this site so that I can look at what you need done and how I can help to achieve that. If you can do that I would suggest to Zip up the project and database and upload it to the EE support site at http://www.ee-stuff.com/ . You may need to login, the password and login ID is the same as you use here at the main site. Then once logged in click on the Expert Area tab at the top of the page. Then click on the link "Upload a new file". Then follow the instructions on the page. Once you upload a file the server will return a link to you, please post the links here. If you can not do this we will have to see how we can proceed from here.

Thanks;
Fernando
Hi Fernando,
I will have to create another application that will only have this functionality and get you a small amount of data. I will have it for you by end of day tomorrow and let you know.
Thanks for your assistance.
Not a problem.
Fernando,
The C# application and database have been uploaded. Please see the link attached. Let me know if you need more information. Thank you for your assistance.

https://filedb.experts-exchange.com/incoming/ee-stuff/7997-ComboBoxProblem.rar
Hi LenTompkins;

I have downloaded your project and database and saw the issue you are having. This is what I found thats happening. In your data grid if you open the Columns collection you will find 7 columns defined. The BillNo column is getting loaded twice. First to load the list I help you to create. Second from the RevClaimComments table and what is causing the error because it is not a collection of string. To prove this I added a new column of type DataGridViewComboBoxColumn and called it BillNum to load the List. I then deleted BillNo DataGridViewComboBoxColumn column and re-added it as a TextBox column which will get used by BillNo in the RevClaimComments table. I then changed the code in the InitializeDataGrid and updateBillNODropDownList methods as shown below and it worked. You need to change the names as I did so that they do not conflict with each other and they load into there own columns.

Fernando
private void InitializeDataGrid()
{
    this.m_srcRevClaimComments = new BindingSource();
    this.gridRevClaimOpenAR.AutoGenerateColumns = false;

    //DataSource for Binding Sources are the datasets 
    this.m_srcRevClaimComments.DataSource = this.m_ds_RevClaimOpenARDataset.RevClaimComments;
    this.gridRevClaimOpenAR.DataSource = this.m_srcRevClaimComments;
    //Give each column a data source
    this.navReversedClaimOpenAR.BindingSource = this.m_srcRevClaimComments;

    //Set Binding for "BillNO" Drop-down column
    //this.BillNOCol.DataPropertyName = "BillNO";
    //this.BillNOCol.DisplayMember = "BillNO";
    //this.BillNOCol.ValueMember = "BillNO";
}

private void updateBillNODropDownList()
{
    int recordsReturned = this.m_adpRevClaimOpenAR.FillBySiteNO
              (this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR, mySiteNo);
    if (recordsReturned == 0)
        this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR.Rows.Add(99999);
    //this.BillNOCol.DataSource = this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR;

    IEnumerable<int> lstBillNo = this.m_ds_RevClaimOpenARDataset.RevClaimOpenAR
        .Select(b => b.Field<int>("BillNO")).ToList();
    List<int> ddList = lstBillNo.Except(this.m_ds_RevClaimOpenARDataset.RevClaimComments.AsEnumerable()
        .Select(b => b.Field<int>("BillNo"))).ToList();
    this.BillNum.DataSource = ddList;
}

Open in new window

Fernando,
I tried the code listed above; however, what we are trying to do is combine this functionality in one combo drop down column. Can this be done? If not, we will use your method.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fernando,
Yes, I would like to source from 2 different data sources. Thanks for your assistance.
Not a problem, always glad to help.  ;=)