c# - 函数未在 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();
}
有什么解决办法吗?我真的没有看到任何问题。
解决方案
好的,所以我的朋友帮我找到了解决方案。
首先,我忘了说,这个函数的主要目的是,将姓名相应地更改为更改的 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”中的表单对象,然后一切正常。
推荐阅读
- ios - Swift - 带有 TabBar 的 NavigationBar 无法正常工作
- json - 使用 JSON 休眠(无数据库表)
- php - 数据库中每 7 个结果的行跨度
- c++ - C++ 使用较新的编译器构建应用程序而不重建库
- java - 如何在 Android Studios 中从 java 类到 kotlin 类
- c# - 如何首先放置 HttpWebRequest Host 标头?
- blockchain - 有关于在比特币网络中创建区块的问题
- javascript - 将颜色填充到 d3 世界地图(等值线地图)
- php - 从python调用php脚本
- excel - excel:如何计算平均间隔时间