首页 > 解决方案 > 我的代码在同时运行另一个表单后找不到 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();
        }
    }

标签: c#formsdatagridviewparametersdatasource

解决方案


推荐阅读