Solved

Winform DataGridView with a pair of cascaded comboboxes

Posted on 2009-06-29
9
882 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 27

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 34
Create XML 5 33
Calling stored proc in EDMX 11 19
C# TextBox 11 0
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

19 Experts available now in Live!

Get 1:1 Help Now