簡體   English   中英

DataGridView未在C#中填充

[英]DataGridView not populating in C#

我不知道我在項目中編碼錯誤是什么,請幫忙。 基本上,我正在為程序創建一個函數,以檢查字符串中是否包含某個字符串。 我已連接到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);中有錯誤functions.ReturnLessonID(CursorLessonName); 我向該函數提供了錯誤的參數,導致該函數返回string.Empty; 因此無法將其添加到表中。 再次感謝@SchlaWiener的建議。

最后,我建議對Visual Studio中的設置進行此修改,以使您不會錯過“隱藏的錯誤”。

I'm sorry that I have to post this in my question since I cannot answer my own question due to my points.

不帶參數的IndexOf區分大小寫,如果您的學生姓名拼寫不同,則測試將失敗。
嘗試更改測試以使用允許忽略大小寫的IndexOf重載

if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text, 
            StringComparison.CurrentCultureIgnoreCase) != -1)
     ......

首先檢查代碼是否進入for循環,但進入該循環的斷點。

其次,由於在if語句中填充了Table_AppliedLesson,因此在

if (AppliedStudentsPerLesson.IndexOf(LTB_StudentID.Text) != -1)

找出您是否實際在“ if語句”中進行編碼。

設置值后,需要將其設置為DataSource

 RefreshAppliedLessonsTable();
 dataGridView1.DataSource = Table_AppliedLessons;

在64位上調試Windows Forms應用程序會靜默地吞下Form_Load事件中的所有錯誤,嘗試按CTRL + ALT + E並選中“ Common Language Runtime exceptions -> thrown復選框。 您可能會錯過一個例外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM