Solved

Winform DataGridView with a pair of cascaded comboboxes

Posted on 2009-06-29
9
888 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
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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