views:

289

answers:

5

I have a winform app that fills a lot of its dropdomn fields fram a maintenance table at runtime. Each Form has a Private void FillMaintFields() I have run into a strange error where setting the column visibility on 1 form works but on another gives me an index out of range error!

Here is the abriged method on the offending form -->

 private void FillMaintFields()
{
 var myMCPTableMaint = new TableMaint();
 // Index 27 is Diabetic Teaching Topics
 var myDataSet = myMCPTableMaint.GetMaintItem(27);
 var myDataTable = myDataSet.Tables[0];
 // Diabetic TeachingTopics dropdown
 chkcboDiabeticTeachingTopics.Properties.DataSource = myDataTable;
 chkcboDiabeticTeachingTopics.Properties.DisplayMember = "ItemDescription";
 chkcboDiabeticTeachingTopics.Properties.ValueMember = "ItemID";
 // Index 26 is Diabetic Teaching
 myDataSet = myMCPTableMaint.GetMaintItem(26);
 myDataTable = myDataSet.Tables[0];
 lkuDiabeticTeaching.Properties.DataSource = myDataTable;
 lkuDiabeticTeaching.Properties.PopulateColumns();
 lkuDiabeticTeaching.Properties.DisplayMember = "ItemDescription";
 lkuDiabeticTeaching.Properties.ValueMember = "ItemID";
 lkuDiabeticTeaching.Properties.Columns[0].Visible = false;
 lkuDiabeticTeaching.Properties.Columns[1].Visible = false;
 lkuDiabeticTeaching.Properties.Columns[3].Visible = false;
 lkuDiabeticTeaching.Properties.Columns[4].Visible = false;
}

Now here is the working function on a sister form -->

        private void FillMaintFields()
    {
        var myMCPTableMaint = new TableMaint();
        // Index 4 is Minimum Contact Schedule
        var myDataSet = myMCPTableMaint.GetMaintItem(4);
        var myDataTable = myDataSet.Tables[0];
        lkuMinContactSchedule.Properties.DataSource = myDataTable;
        lkuMinContactSchedule.Properties.PopulateColumns();
        lkuMinContactSchedule.Properties.DisplayMember = "ItemDescription";
        lkuMinContactSchedule.Properties.ValueMember = "ItemID";
        lkuMinContactSchedule.Properties.Columns[0].Visible = false;
        lkuMinContactSchedule.Properties.Columns[1].Visible = false;
        lkuMinContactSchedule.Properties.Columns[3].Visible = false;
        lkuMinContactSchedule.Properties.Columns[4].Visible = false;

        // Index 5 is Release of Information Updated Annually
        myDataSet = myMCPTableMaint.GetMaintItem(5);
        myDataTable = myDataSet.Tables[0];
        lkuReleaseInfoUpdateAnnually.Properties.DataSource = myDataTable;
        lkuReleaseInfoUpdateAnnually.Properties.PopulateColumns();
        lkuReleaseInfoUpdateAnnually.Properties.DisplayMember = "ItemDescription";
        lkuReleaseInfoUpdateAnnually.Properties.ValueMember = "ItemID";
        lkuReleaseInfoUpdateAnnually.Properties.Columns[0].Visible = false;
        lkuReleaseInfoUpdateAnnually.Properties.Columns[1].Visible = false;
        lkuReleaseInfoUpdateAnnually.Properties.Columns[3].Visible = false;
        lkuReleaseInfoUpdateAnnually.Properties.Columns[4].Visible = false;}

They are all using the same method in the DAL and accessing the EXACT same table which has a structure of -->


|ItemID | CategoryID | ItemDescription | OrderID | Active|

I am at a loss here as to why it would work in 1 spot and not another. If I comment out the Columns[] portion in the offending Form it returns the correct data with NO errors but, of course ALL columns visible.

Ideas? Thanks!

EDIT 1

Based on some comments and concerns: The error appears on the first line that tries to access the Columns[]. Wherever and whatever column that may be on the offending Form. If I debug and look at myDataTable it shows the correct columnms and correct data.

+1  A: 

Create a minimal code that still reproduces a problem. Debug that code; or, if that doesn't help, post it here. The above code doesn't really tell us anything: it's too large, too specific to your problem. We don't know how your data looks, we've got to take your word for the table structure – although C# seems to disagree.

All in all, any help anyone could offer is just fuzzy guesswork.

Konrad Rudolph
I am not sure how I could make it any more minimal? Thank you for taking the time to post though.
Refracted Paladin
+3  A: 

Is the error happening on the "lkuReleaseInfoUpdateAnnually" or the "lkuMinContactSchedule"? Which control is that exactly? It seems the error is on the control side of things, seems like your control in the second form doesn't have all the columns you're expecting it to have.

EDIT: You seem to be confusing the Columns in the dataTable with the columns on your control(s). I'm not sure which control you're using, but doing:

lkuReleaseInfoUpdateAnnually.Properties.Columns[0]

does NOT mean you're indexing into the DataTable. You're indexing into the columns of your control, which may or may not be there.

BFree
Yup, I understand what you are saying. I am using the DevExpress LookUpEdit control in all cases. It appears you are also on the right track as when I step through the code my column count on the offending form for those controls is 0 before and after PopulateColumns() is run where as on the Form it works on the column count is 0 then PopulateColumns() runs and the column count is 5. I may have to take this to DevExpress as to why PopulateColumns() isn't running in this situation....Thanks!
Refracted Paladin
+1  A: 

I have some ideas on how to better specify bugs and their origin. Something you should do, is to watch "lkuDiabeticTeaching.Properties.Columns.Count" value, if, it's less than 1 means, you do not have any columns, and probably because no population occurred.

Ada
It seems like you may be on to something here though why PopulateColumns() works in one place and not the other baffles me....
Refracted Paladin
A: 

The only thing I can see is that the first set of code is missing a call to PopulateForms() (it is included in the second set). Could that be it?

Michael Todd
A: 

Well, I figured it out. Kinda. For some reason I need to call lkuDiabeticTeaching.Properties.ForceInitialize(); after setting the data source on the offending form.

I can only guess that because I have nested forms that somehow this was trying to fire before the control was initialized.

It now looks like this -->

            // Index 26 is Diabetic Teaching
        harDataSet = myHARTableMaint.GetMaintItem(26);
        harDataTable = harDataSet.Tables[0];
        lkuDiabeticTeaching.Properties.DataSource = harDataTable;
        lkuDiabeticTeaching.Properties.ForceInitialize();
        lkuDiabeticTeaching.Properties.PopulateColumns();
        lkuDiabeticTeaching.Properties.DisplayMember = "ItemDescription";
        lkuDiabeticTeaching.Properties.ValueMember = "ItemID";
        if(lkuDiabeticTeaching.Properties.Columns.Count > 0)
        {
            lkuDiabeticTeaching.Properties.Columns[0].Visible = false;
            lkuDiabeticTeaching.Properties.Columns[1].Visible = false;
            lkuDiabeticTeaching.Properties.Columns[3].Visible = false;
            lkuDiabeticTeaching.Properties.Columns[4].Visible = false; 
        }

Thanks to all who gave of there time.

Refracted Paladin