首页 > 解决方案 > 函数未在 C# 中执行

问题描述

我有一个函数,它用DataGridView学生表中合并的学生姓名和学生姓氏填充列,具体取决于表中的学生 ID,我前面提到的列所在的表中。

功能是:

public void replace_namesSurnames()
        {
            foreach (DataGridViewRow row in attendance_dgw.Rows)
            {
                int student_id = Convert.ToInt32(row.Cells["student_id"].Value);
                if (row.Index < attendance_dgw.Rows.Count - 1)
                {
                    string text = attendanceRecording.Tables["students"].Rows[student_id - 1]["student_name"].ToString() + " " + attendanceRecording.Tables["students"].Rows[student_id - 1]["student_surname"].ToString();
                    row.Cells["stud_nameSurname"].Value = text;
                }
            }
        }

如果我通过单击按钮执行此功能,它就可以工作。它在第一次初始化表单时也有效。

我试图向其中添加一个CellValueChanged事件,DataGridView但该函数没有执行:

        private void attendance_dgw_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            replace_namesSurnames();
        }

有什么解决办法吗?我真的没有看到任何问题。

标签: c#winformsado.net

解决方案


好的,所以我的朋友帮我找到了解决方案。

首先,我忘了说,这个函数的主要目的是,将姓名相应地更改为更改的 student_id。但!学生证的更改是在单独表格的帮助下进行的。

所以主要问题(为什么这一切都开始了)是,更新那些姓氏值的函数位于主表单中,但我是从表单中执行它,这会更改学生 ID。

在更改学生 ID 的表单中,我创建了一个主表单对象,以从该主表单访问功能。但是,似乎程序没有看到该函数内部的内容,这就是它没有执行的原因。

因此,解决方法是 - 将主表单传递给进行更改的表单。

detailsTableForm details = new detailsTableForm(attendanceRecording, cellValue,e.RowIndex,e.ColumnIndex,0,columnName,attendance_dgw, this);

那里的主要重点是将“this”(主表单)传递给“detailsTableForm”(表单,在主表单中更改学生ID)。当我将“this”(主表单)传递给“detailsTableForm”时,我将收到的表单分配给“detailsTableForm”中的表单对象,然后一切正常。


推荐阅读