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!
我有一个winform应用程序,它在运行时填充了许多dropdomn字段。每个Form都有一个Private void FillMaintFields()我遇到了一个奇怪的错误,在1个表单上设置列可见性有效但在另一个表单上给出了索引超出范围错误!
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 | 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.
我在这里不知道为什么它会在一个地方而不是另一个地方工作。如果我在违规表格中注释掉Columns []部分,它会返回没有错误的正确数据,但当然所有列都可见。
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.
根据一些注释和疑虑:错误出现在尝试访问Columns []的第一行。无论在违规表格上的哪个栏目和任何栏目。如果我调试并查看myDataTable,它会显示正确的列和正确的数据。
5 个解决方案
#1
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.
错误发生在“lkuReleaseInfoUpdateAnnually”或“lkuMinContactSchedule”上吗?究竟是哪种控制?似乎错误在控制方面,似乎你的第二种形式的控件没有你期望它拥有的所有列。
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:
编辑:您似乎混淆了dataTable中的列与控件上的列。我不确定你正在使用哪种控件,但是:
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.
并不意味着你要索引到DataTable。您正在索引控件的列,这些列可能存在也可能不存在。
#2
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.
总而言之,任何人都能提供的任何帮助都只是模糊的猜测。
#3
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意味着,您没有任何列,可能是因为没有出现任何人口。
#4
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?
我唯一能看到的是第一组代码缺少对PopulateForms()的调用(它包含在第二组中)。可能是吗?
#5
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.
好吧,我明白了。均田。出于某种原因,我需要调用lkuDiabeticTeaching.Properties.ForceInitialize();在违规表格上设置数据源之后。
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.
感谢所有给予时间的人。
#1
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.
错误发生在“lkuReleaseInfoUpdateAnnually”或“lkuMinContactSchedule”上吗?究竟是哪种控制?似乎错误在控制方面,似乎你的第二种形式的控件没有你期望它拥有的所有列。
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:
编辑:您似乎混淆了dataTable中的列与控件上的列。我不确定你正在使用哪种控件,但是:
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.
并不意味着你要索引到DataTable。您正在索引控件的列,这些列可能存在也可能不存在。
#2
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.
总而言之,任何人都能提供的任何帮助都只是模糊的猜测。
#3
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意味着,您没有任何列,可能是因为没有出现任何人口。
#4
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?
我唯一能看到的是第一组代码缺少对PopulateForms()的调用(它包含在第二组中)。可能是吗?
#5
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.
好吧,我明白了。均田。出于某种原因,我需要调用lkuDiabeticTeaching.Properties.ForceInitialize();在违规表格上设置数据源之后。
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.
感谢所有给予时间的人。