简体   繁体   English

索引超出范围错误; 在这里但不在那里?

[英]Index out of range error; Here but not There?

I have a winform app that fills a lot of its dropdomn fields fram a maintenance table at runtime. 我有一个winform应用程序,该应用程序在运行时填充了很多维护表,并在其中添加了维护表。 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! 每个窗体都有一个Private void FillMaintFields()我遇到了一个奇怪的错误,即在1种表单上设置列的可见性是Private void FillMaintFields()但在另一种表单上给我设置了索引超出范围的错误!

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 --> 它们都在DAL中使用相同的方法并访问具有->结构的EXACT相同表


|ItemID | | ItemID | CategoryID | 分类ID | ItemDescription | 商品说明| OrderID | 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. 如果我注释掉有问题的表单中的Columns []部分,它将返回没有错误的正确数据,但是,当然所有列都是可见的。

Ideas? 有想法吗? Thanks! 谢谢!

EDIT 1 编辑1

Based on some comments and concerns: The error appears on the first line that tries to access the Columns[]. 基于一些评论和关注:错误出现在尝试访问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. 如果我调试并查看myDataTable,它将显示正确的columnms和正确的数据。

Is the error happening on the "lkuReleaseInfoUpdateAnnually" or the "lkuMinContactSchedule"? 该错误是否发生在“ lkuReleaseInfoUpdateAnnually”或“ 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). 编辑:您似乎正在混淆dataTable中的列与控件上的列。 I'm not sure which control you're using, but doing: 我不确定您使用的是哪个控件,但可以这样做:

lkuReleaseInfoUpdateAnnually.Properties.Columns[0]

does NOT mean you're indexing into the DataTable. 并不意味着您正在索引DataTable。 You're indexing into the columns of your control, which may or may not be there. 您正在索引控件的列,该列可能存在也可能不存在。

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. 我们不知道您的数据看起来如何,我们必须对表结构有所了解-尽管C#似乎不同意。

All in all, any help anyone could offer is just fuzzy guesswork. 总而言之,任何人都可以提供的任何帮助只是模糊的猜测。

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. 您应该做的就是观察“ lkuDiabeticTeaching.Properties.Columns.Count”值,如果该值小于1,则意味着您没有任何列,并且可能是因为没有填充。

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). 我唯一能看到的是第一组代码缺少对PopulateForms()的调用(它包含在第二组代码中)。 Could that be it? 可以吗?

Well, I figured it out. 好吧,我知道了。 Kinda. 金田 For some reason I need to call lkuDiabeticTeaching.Properties.ForceInitialize(); 由于某些原因,我需要调用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. 感谢所有在那里度过的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM