Solved

Winform DataGridView with a pair of cascaded comboboxes

Posted on 2009-06-29
9
891 Views
Last Modified: 2012-06-21
I am trying to set up a 2 comboboxes in a DataGridView, one with a State, the second holding a list of counties for that particular state.  I have bound the state combobox to an array of state names, and that works fine.  I have county column bound to a filtered collection of counties.  I update the filtered list in the 'RowEnter' and the 'CellValueChanged" event.  This seems to work fine, as I can select a state, and the county list displays the correct list of counties.  However, when I try and go to the next row(which is a newly created row), I get the following error message:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.

The form load looks like this
        private void frmEditor_Load(object sender, EventArgs e)
        {
           /* omit code to fetch list of states and counties */
            colState.DataSource = oStates;
            colState.ValueMember = "StateFipsCode";
            colState.DisplayMember = "State";

             colCounty.DataSource = oFiltered;
             colCounty.DisplayMember = "County";
             colCounty.ValueMember = "CountyFipsCode";
        }

        // function called on RowEnter and CellValueChanged event (in state column)
        void UpdateFilter()
        {
            oFiltered.Clear();
            int nStateFips = SafeObjToInt(dgFarms.Rows[e.RowIndex].Cells[1].Value);
            // adding a blank county row did not help anything
            //oFiltered.Add(new FarmSimData.cCountyFips("", -1, "", -1));
            for (int i = 0; i <= oCountyByState.GetUpperBound(0); i++)
                if (nStateFips == (int)oCountyByState[i][0].StateFipsCode)
                {
                    for (int j = 0; j <= oCountyByState[i].GetUpperBound(0); j++)
                        oFiltered.Add(oCountyByState[i][j]);
                    break;
                }
        }

Thanks for any help
0
Comment
Question by:bnonnemann
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 11

Expert Comment

by:azarc3
ID: 24737517
Is a default "State" selected when you enter go the newly created row?
0
 

Author Comment

by:bnonnemann
ID: 24737603
Nothing specific was selected for the State
0
 
LVL 11

Expert Comment

by:azarc3
ID: 24739077
That sounds like the problem then... I recommend that you build in a test to see if the State has a valid selected index before you try to use it. I would do it something like this...
// assumes C# 9.0 ... you'll need to adjust the syntax if you're using an earlier version.
// function called on RowEnter and CellValueChanged event (in state column) 
void UpdateFilter()
{
    if (!string.IsNullOrEmpty(dgFarms.Rows[e.RowIndex].Cells[1].Value))
    {
        var intCheck = default(int);
        int.TryParse(dgFarms.Rows[e.RowIndex].Cells[1].Value, intCheck);
 
        if (intCheck > 0)
        {
            oFiltered.Clear();
            int nStateFips = SafeObjToInt(dgFarms.Rows[e.RowIndex].Cells[1].Value);
            // adding a blank county row did not help anything
            //oFiltered.Add(new FarmSimData.cCountyFips("", -1, "", -1));
            for (int i = 0; i <= oCountyByState.GetUpperBound(0); i++)
            {
                if (nStateFips == (int)oCountyByState[i][0].StateFipsCode)
                {
                        for (int j = 0; j <= oCountyByState[i].GetUpperBound(0); j++)
                            oFiltered.Add(oCountyByState[i][j]);
                        break;
                }
            }
        }
    }
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:azarc3
ID: 24739126
A couple of syntax updates (my bad)...

if (!string.IsNullOrEmpty(dgFarms.Rows[e.RowIndex].Cells[1].Value))
SHOULD BE
if (!string.IsNullOrEmpty((string)dgFarms.Rows[e.RowIndex].Cells[1].Value))


int.TryParse(dgFarms.Rows[e.RowIndex].Cells[1].Value, intCheck);
SHOULD BE
int.TryParse((string)dgFarms.Rows[e.RowIndex].Cells[1].Value, intCheck);

Try those and see what you get.
0
 

Author Comment

by:bnonnemann
ID: 24757587
It turns out that your suggestion was only part of the solution.   The problem comes up when you wwitch between rows that have different states.  For example, say in the first row you select the state 'Alabama', and then pick 'Butler' county.  You then go to the next row, and you select 'Illinois', and the 'Grundy'as the county.  So far, so good -- but when you return to the first line, the county list reverts back to those for Alabama, and the second row (containing the Illinois data) now displays 'Greene' county -- the county in Alabama that matches the FIPS code for 'Grundy' county in Illinois.  When you switch back to the row with Illinois as the state, then 'Grundy' county reappears in the county column.  Argh!   I'm going to try to see if making the county fips codes unique helps things, but I have the sinking feeling that the end result will be blank county entries when you switch states.

Thanks for you help so far!!
0
 

Author Comment

by:bnonnemann
ID: 24757649
As I expected, changing the county fips codes to be unique (by combining state and county codes into a composite value) results in county columns going blank in rows whose state does not make the state in the current row.
0
 
LVL 28

Accepted Solution

by:
Ark earned 125 total points
ID: 24758378
This is because your filtered list applies to all rows. Try to populate countly combo directly (without datasource) in EditingControlShowing event like
cboCountly.Items.Clear();
//.............
   for (int j = 0; j <= oCountyByState[i].GetUpperBound(0); j++)
          cboCountly.Items.Add(oCountyByState[i][j]);
                        break;

BTW, unique code is a good idea to save table data and initialy populating it (you can use CellFormatting event for this)
0
 
LVL 11

Expert Comment

by:azarc3
ID: 24762543
Sorry; was away from the Internet yesterday.
I agree with Ark's logic. That's actually the technique I would have used for my own needs.
0
 

Author Closing Comment

by:bnonnemann
ID: 31597993
Sorry, was away from the office for a while -- this suggestion works just fine.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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