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
LenTompkinsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BasiKobrACommented:
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

0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
What version of Visual Studio .Net and the .Net Framework are you using?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

LenTompkinsAuthor Commented:
Fernando,
We are using Visual Studio 2008 and .NET Framework 3.5
0
Fernando SotoRetiredCommented:
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

0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
Can you upload the code file/s so I can try to see what is going on.
0
OrcbighterCommented:
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?
0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
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
0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
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
0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
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
0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
Not a problem.
0
LenTompkinsAuthor Commented:
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
0
Fernando SotoRetiredCommented:
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

0
LenTompkinsAuthor Commented:
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.
0
Fernando SotoRetiredCommented:
Hi LenTompkins;

Is that not what your code does, see code snippet below, if so then make BillNO a textbox in the grid and just hide it just leaving the ComboBox from the lsit of BillNum which is the ComboBox I added in the above code.

But if your question is that you want to supply the ComboBox from two source then that can not be done.

Fernando
    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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LenTompkinsAuthor Commented:
Fernando,
Yes, I would like to source from 2 different data sources. Thanks for your assistance.
0
Fernando SotoRetiredCommented:
Not a problem, always glad to help.  ;=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.