Solved

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

Posted on 2010-08-16
24
419 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:LenTompkins
  • 10
  • 10
  • +1
24 Comments
 
LVL 4

Expert Comment

by:BasiKobrA
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
What version of Visual Studio .Net and the .Net Framework are you using?
0
 

Author Comment

by:LenTompkins
Comment Utility
Fernando,
We are using Visual Studio 2008 and .NET Framework 3.5
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Can you upload the code file/s so I can try to see what is going on.
0
 
LVL 9

Expert Comment

by:Orcbighter
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem.
0
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
 

Author Comment

by:LenTompkins
Comment Utility
Fernando,
Yes, I would like to source from 2 different data sources. Thanks for your assistance.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, always glad to help.  ;=)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now