C# 在一个项目中运行良好的C代码在另一个项目中不起作用
以下代码完全相同,在另一个项目上运行良好C# 在一个项目中运行良好的C代码在另一个项目中不起作用,c#,c#-4.0,datagridview,ado.net,C#,C# 4.0,Datagridview,Ado.net,以下代码完全相同,在另一个项目上运行良好 private void AddComboBoxCells() { DataGridViewComboBoxCell dgvcell; _query = "select ProductName from Product"; com = new SqlCommand(_query, con); for (int i =
private void AddComboBoxCells()
{
DataGridViewComboBoxCell dgvcell;
_query = "select ProductName from Product";
com = new SqlCommand(_query, con);
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dgvcell = new DataGridViewComboBoxCell();
dataGridView1[1, i] = dgvcell;
myrdr = com.ExecuteReader();
while (myrdr.Read())
{
dgvcell.Items.Add(myrdr.GetValue(0));
}
myrdr.Close();
}
}
此Add按钮代码有效,因此输入的数据确实有效。请提供帮助。首先,您可能希望将处理程序附加到DataGridView控件的DataError事件,以获取有关所获得错误的更多信息 其次,需要检查的明显内容是您在第二个项目中使用的数据上下文。这似乎很可能不一定是编程错误,而更像是与您正在查询和/或进行数据绑定的基础数据相关的问题 您可能需要调查查询返回的数据类型和值:从产品中选择ProductName 查看myrdr.getValue0-重写行dgvcell.Items.Addmyrdr.getValue0;像这样:
object val = myrdr.GetValue(0);
dgvcell.Items.Add(val); // put a break-point here and check val
编辑
为了回答您关于如何处理DataError事件的问题,您只需将处理程序附加到“dataGridView1”对象的事件成员上即可。如果您不知道什么是事件,或者不知道如何将处理程序附加到C中的事件,那么我建议您在线阅读这些内容—到处都有大量的资源
// you will want to add this code to attach the handler in your initialization code - maybe in the Load event handler
dataGridView1.DataError += new DataGridViewDataErrorEventHandler(DataGridView1);
// ....
// an then define this function somewhere in your class to handle the event
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
// this function will be called when an error occurs.
// you can then use the anError paramenter to get insight into the type and cause of the error
}
首先,您可能希望向DataGridView控件的DataError事件附加一个处理程序,以获取有关所获取错误的更多信息 其次,需要检查的明显内容是您在第二个项目中使用的数据上下文。这似乎很可能不一定是编程错误,而更像是与您正在查询和/或进行数据绑定的基础数据相关的问题 您可能需要调查查询返回的数据类型和值:从产品中选择ProductName 查看myrdr.getValue0-重写行dgvcell.Items.Addmyrdr.getValue0;像这样:
object val = myrdr.GetValue(0);
dgvcell.Items.Add(val); // put a break-point here and check val
编辑
为了回答您关于如何处理DataError事件的问题,您只需将处理程序附加到“dataGridView1”对象的事件成员上即可。如果您不知道什么是事件,或者不知道如何将处理程序附加到C中的事件,那么我建议您在线阅读这些内容—到处都有大量的资源
// you will want to add this code to attach the handler in your initialization code - maybe in the Load event handler
dataGridView1.DataError += new DataGridViewDataErrorEventHandler(DataGridView1);
// ....
// an then define this function somewhere in your class to handle the event
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{
// this function will be called when an error occurs.
// you can then use the anError paramenter to get insight into the type and cause of the error
}
您的第二个项目中是否有有效数据?我猜您对myrdr.GetValue0的调用在某个点上返回null或其他伪值?您的第二个项目中是否有有效数据?我猜您对myrdr.GetValue0的调用在某个时刻返回null或其他伪值?在哪个源代码行上会发生错误?最后一行-它会很好地填充第一行,并保持前一行的值正确,但在第二行,它将桌面更改为传真。表中的第二行包含:2桌面2 3 60000我看不到错误消息!Daniel Persson的意思是在哪一行代码上?不是在哪个数据行上。错误发生在哪个源代码行上?最后一行-它很好地填充第一行并保持上一个值正确,但是在第二行中它将桌面更改为传真表中的第二行包含:2 Desktop 2 3 6000I看不到错误消息!Daniel Persson的意思是在哪一行代码上?不在哪个数据行上。一切都是一样的。即使是类型。你能举个例子,如何写一个数据处理程序吗?你有没有像我建议的那样尝试附加事件处理程序。如果是这样的话,当您运行调试器时会发生什么?在事件处理程序中放置一个断点。一切都是一样的。即使是类型。你能举个例子,如何写一个数据处理程序吗?你有没有像我建议的那样尝试附加事件处理程序。如果是这样,那么在使用调试器运行时会发生什么情况?在事件处理程序中放置一个断点。很可能返回的是DBNull,而不是null。很可能返回的是DBNull,而不是null。