c# - 我的代码在同时运行另一个表单后找不到 datagridview 数据源
问题描述
我正在为学校设置一个 IT 项目,但我在 c# Winform 中遇到了 datagridview 的问题。
我用连接到我的数据库的 datagridview 制作了第一个表单,我想制作一个按钮,如果需要,可以从 datagridview 中删除一行。
为了确保客户不会犯错误,我创建了另一个表单,它询问“你想删除这一行吗?(是/否)”,这是一个基本表单(标签 + 按钮)。
但是当我想关闭这个表单时,我会转到我的第一个数据网格中的一个函数,该函数将从程序开始初始化这个网格,在这段代码中,我使用 Datagridview.DataSource 进行循环。
问题被标记在这里,当我打印我的数据源时,它打印我什么都没有,就像没有数据源一样......
当您弹出一个新表单时,有没有人有理由或如何使 DataSource 与以前的表单保持一致?
我的初始网格代码
public void InitDatagrid()
{
//Initialize the tools
CancelMod.Visible = false;
ApplyMod.Visible = false;
Modifie.Visible = true;
Prevbtn.Visible = true;
ClientCommand.Enabled = true;
Client_Pieces.Enabled = true;
Prices.Enabled = true;
PieceCommand.Enabled = true;
Pieces.Enabled = true;
textBox1.Enabled = true;
RowAdd.Visible = false;
RowDelete.Visible = false;
textboxDel.Visible = false;
labelDel.Visible = false;
dataGridView1.EditMode =
DataGridViewEditMode.EditProgrammatically;
textBox1.Text = "";
this.pieceTableAdapter.Fill(this.mykitboxDataSet5.piece);
//Put the color of the columns as white (initial color)
for (var i = 0; i < ((DataTable)dataGridView1.DataSource).Columns.Count; i++)
{
dataGridView1.Columns[i].DefaultCellStyle.BackColor = Color.White;
}
int count = ((DataTable)dataGridView1.DataSource).Columns.Count;
//Put the changed cells color as white (initial color)
if (count == 8)
{
dataGridView2.EditMode =
DataGridViewEditMode.EditProgrammatically;
DeleteCommand.Visible = false;
textBoxDel2.Visible = false;
labelDel2.Visible = false;
for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count.ToString()); i++)
{
//Column = payment_status and command_status
dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.White;
dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.White;
}
}
}
这是我的弹出代码
public PopUpDel(string codedb, string text, DataGridView ds)
{
this.codedb = codedb;
InitializeComponent();
code.Text = codedb;
label1.Text = text;
Console.WriteLine(ds);
}
private void yes_Click(object sender, EventArgs e)
{
SKGridPage sk = new SKGridPage();
sk.SqlConnection();
MySqlCommand command = conn.CreateCommand();
command.CommandText = string.Format("DELETE FROM piece WHERE code = '{0}'", this.codedb);
Console.WriteLine(command.CommandText);
try
{
conn.Open();
command.ExecuteNonQuery();
}
catch (Exception x)
{
Console.WriteLine(x.Message);
}
conn.Close();
this.Close();
sk.InitDatagrid();
}
private void no_Click(object sender, EventArgs e)
{
this.Close();
}
}
解决方案
推荐阅读
- google-sheets - 如何将多个游戏的玩家结果相加?
- swift - 在 Safari 扩展中打开没有工具栏的 Safari 窗口
- postgresql - PostgresConnector:在此连接器上未发现任何主题
- c# - 如何从arraylist中的文本框输入文本
- android-room - 如何使用房间 android 中的类型转换器将整数转换为字符串?
- php - Codeigniter mysqli 连接
- javascript - 查找给定数字n的所有偶数分解 - javascript python
- android - 从我的 firebase db 的单个字段中获取值
- php - Docker - 环境变量不覆盖 php.ini
- java - 如何在 Maven 中禁用任何类型的依赖项日志记录?[登录,以太坊J]