Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Winform DataGridView with a pair of cascaded comboboxes

Posted on 2009-06-29
9
Medium Priority
?
900 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:Kelvin McDaniel
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:Kelvin McDaniel
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Kelvin McDaniel
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 500 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:Kelvin McDaniel
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

783 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