索引超出范围错误; 这里而不是那里?
我有一个 winform 应用程序,它在运行时填充维护表中的许多 dropdomn 字段。 每个表单都有一个Private void FillMaintFields()
我遇到了一个奇怪的错误,在一个表单上设置列可见性有效,但在另一个表单上设置列可见性却给我一个索引超出范围错误!
这是违规表单上的简化方法 -->
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;
}
现在这是姐妹形式的工作功能 -->
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;}
它们都在 DAL 中使用相同的方法并访问完全相同的表,该表的结构为 -->
|项目ID | 类别ID | 项目描述 | 订单ID | 活跃|
我在这里不知道为什么它可以在一个地方工作,而不能在另一个地方工作。 如果我注释掉有问题的表单中的 Columns[] 部分,它会返回正确的数据,没有错误,但当然所有列都可见。
有想法吗? 谢谢!
编辑 1
基于一些评论和担忧: 该错误出现在尝试访问 Columns[] 的第一行。 无论违规表格上的任何位置和任何列。 如果我调试并查看 myDataTable,它会显示正确的列和正确的数据。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
错误是否发生在“lkuReleaseInfoUpdateAnnually”或“lkuMinContactSchedule”上? 到底是哪个控件? 看来错误是在控制方面,似乎第二种形式的控件没有您期望的所有列。
编辑:您似乎将数据表中的列与控件上的列混淆了。 我不确定您正在使用哪个控件,但是这样做:
并不意味着您正在对数据表建立索引。 您正在对控件的列进行索引,该控件可能存在也可能不存在。
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:
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.
创建仍然重现问题的最小代码。 调试该代码; 或者,如果这没有帮助,请将其发布到此处。 上面的代码并没有真正告诉我们任何事情:它太大了,对于您的问题来说太具体了。 我们不知道您的数据看起来如何,我们必须相信您所说的表结构 - 尽管 C# 似乎不同意。
总而言之,任何人可以提供的任何帮助都只是模糊的猜测。
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.
我对如何更好地指定错误及其起源有一些想法。 您应该做的事情是观察“lkuDiabeticTeaching.Properties.Columns.Count”值,如果它小于 1,则意味着您没有任何列,可能是因为没有发生人口。
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.
我唯一能看到的是第一组代码缺少对 PopulateForms() 的调用(它包含在第二组代码中)。 会是这样吗?
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?
好吧,我想通了。 有点。 由于某种原因,我需要在有问题的表单上设置数据源后调用
lkuDiabeticTeaching.Properties.ForceInitialize();
。我只能猜测,因为我有嵌套表单,不知何故,它试图在控件初始化之前触发。
现在看起来像这样 -->
感谢所有付出时间的人。
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 -->
Thanks to all who gave of there time.