c#中没有填充DataGridView
本文关键字:填充 DataGridView | 更新日期: 2023-09-27 18:12:21
我不知道我在我的项目中写错了什么,请帮助。基本上,我是在为程序创建一个函数来检查某个字符串是否在字符串中。我已经连接到Access数据库,返回一个字符串,如"asdf,dfgh,ghjk",然后函数将检查"dfgh"是否在其中。下面是我的代码:
private void RefreshAppliedLessonsTable()
{
Table_AppliedLessons.Rows.Clear();
for (int i = 0; i < Table_Lessons.Rows.Count; i++)
{
string CursorLessonName = Table_Lessons.Rows[i].Cells[0].Value.ToString();
string LessonID = functions.ReturnLessonID(CursorLessonName);
string AppliedStudentsPerLesson = functions.ReturnAppliedStudents(LessonID);
if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)
{
string LessonName = string.Empty;
string LessonCourse = string.Empty;
string LessonTeacher = string.Empty;
string Level = string.Empty;
string Time = string.Empty;
string QuotaLeft = string.Empty;
string Price = string.Empty;
LessonName = Table_Lessons.Rows[i].Cells[0].Value.ToString();
LessonCourse = Table_Lessons.Rows[i].Cells[1].Value.ToString();
LessonTeacher = Table_Lessons.Rows[i].Cells[2].Value.ToString();
Level = Table_Lessons.Rows[i].Cells[3].Value.ToString();
Time = Table_Lessons.Rows[i].Cells[4].Value.ToString();
QuotaLeft = Table_Lessons.Rows[i].Cells[5].Value.ToString();
Price = Table_Lessons.Rows[i].Cells[6].Value.ToString();
Table_AppliedLessons.Rows.Add(new object[] { LessonName, LessonCourse, LessonTeacher, Level, Time, QuotaLeft, Price });
}
}
然后我将在表单加载时执行此代码。然而,datagridview表"Table_AppliedLessons"将永远不会被填充。正在确认数据库中是否包含该字符串。有人能帮忙吗?
Answer to this problem:
正如@SchlaWiener建议的那样,我可能在默认情况下排除了程序中的一些错误。使用"CTRL + ALT + E
"后,勾选"Common Language Runtime exceptions -> thrown
"复选框。我看到在functions.ReturnLessonID(CursorLessonName);
中有一个错误,我向函数提供了错误的参数,导致它返回string.Empty;
,因此它不能被添加到表中。再次感谢@SchlaWiener的建议。
最后,我建议在visual studio中修改这些设置,这样你就不会错过一个"隐藏的bug"。
I'm sorry that I have to post this in my question since I cannot answer my own question due to my points.
在64位上调试Windows窗体应用程序默默地吞噬Form_Load事件中的所有错误,尝试点击CTRL + ALT + E
并勾选Common Language Runtime exceptions -> thrown
复选框。可能会漏掉一个异常
不带参数的IndexOf是大小写敏感的,如果你的学生名字拼写不同,你的测试将失败
尝试更改测试以使用允许忽略大小写
if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text,
StringComparison.CurrentCultureIgnoreCase) != -1)
......
设置好值后需要设置为DataSource
RefreshAppliedLessonsTable();
dataGridView1.DataSource = Table_AppliedLessons;
首先检查代码是否进入for循环,但在那里有断点。
第二,因为Table_AppliedLesson是在if语句中填充的,所以在 处放置一个断点if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)
来确定你的代码是否在"if语句"中运行。