Solved

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

Posted on 2010-08-16
24
442 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
ID: 33449646
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
ID: 33449923
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 63

Expert Comment

by:Fernando Soto
ID: 33547347
What version of Visual Studio .Net and the .Net Framework are you using?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:LenTompkins
ID: 33558301
Fernando,
We are using Visual Studio 2008 and .NET Framework 3.5
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33561183
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
ID: 33581543
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 63

Expert Comment

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

Expert Comment

by:Orcbighter
ID: 33593970
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
ID: 33627928
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 63

Expert Comment

by:Fernando Soto
ID: 33630104
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
ID: 33630573
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33633579
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
ID: 33650630
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 63

Expert Comment

by:Fernando Soto
ID: 33651135
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
ID: 33677099
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 63

Expert Comment

by:Fernando Soto
ID: 33677437
Not a problem.
0
 

Author Comment

by:LenTompkins
ID: 33686164
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 63

Expert Comment

by:Fernando Soto
ID: 33688171
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
ID: 33692680
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 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33692837
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
ID: 33693295
Fernando,
Yes, I would like to source from 2 different data sources. Thanks for your assistance.
0
 
LVL 63

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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