从C#中的其他表单更新datagridview

时间:2019-01-23 13:47:41

标签: c# sql

为了简短起见,我尝试了其他帖子中的解决方案,但无法使其正常工作。我是编程新手,从工作中我得到了制作软件的任务。

我创建了两种形式,第一种形式称为AddNewUser,另一种形式称为RegistrationPopOut,如果用户单击AddNewUser表单上的Register,则后一种形式会弹出。

如果要在RegistrationPopOut中单击“保存”,我希望AddNewUser中的datagridview自动更新

这是AddNewUser的代码:

       private void Register_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "")
        {

            MessageBox.Show("Please fill in every fields");
        }
        else
        {
            int i = 0;
            SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
            cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted. 
            cmd.CommandText = "select * from registration where username='" + textBox3.Text + "' or idno='" + textBox2.Text + "'";
            //cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
            cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected. 
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            i = Convert.ToInt32(dt.Rows.Count.ToString());
            if (i == 0) //If ok nada masalah register
            {
                SqlCommand cmd1 = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
                cmd1.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted. 
                cmd1.CommandText = "insert into registration values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "')";
                cmd1.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.

                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = "";
                display();

                MessageBox.Show("Successfully registered");
            }
            else //if ada masalah register
            {
                MessageBox.Show("User already registered");
            }

        }
    }

   private void display()
    {
        SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
        cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted. 
        cmd.CommandText = "select IDNO as 'Employee ID',Username,Name as 'Full Name',Istana,Position,Area from registration";
        //cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
        cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected. 
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        dataGridView1.DataSource = dt;

    }

    private void AddNewUser_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Open)  //apa maksudnya?
        {
            con.Close();
        }
        con.Open();
        display();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Employees.RegistrationPopOut RegPO = new Employees.RegistrationPopOut();
        RegPO.Show();
        display();


    }

这是RegisterPopOut的代码

       private void BtnRegister_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "")
        {

            MessageBox.Show("Please fill in every fields");
        }
        else
        {
            int i = 0;
            SqlCommand cmd = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
            cmd.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted. 
            cmd.CommandText = "select * from registration where username='" + textBox3.Text + "' or idno='" + textBox2.Text + "'";
            //cmd.CommandText = "select * from registration where username='" + textBox3.Text + "'";
            cmd.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected. 
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            i = Convert.ToInt32(dt.Rows.Count.ToString());
            if (i == 0) //If ok nada masalah register
            {
                SqlCommand cmd1 = con.CreateCommand(); //Creates and returns a SqlCommand object associated with the SqlConnection.
                cmd1.CommandType = CommandType.Text; //CommandType = Specifies how a command string is interpreted. 
                cmd1.CommandText = "insert into registration values('" + textBox3.Text + "','" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "')";
                cmd1.ExecuteNonQuery(); //used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected.

                textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; comboBox1.Text = ""; comboBox2.Text = ""; comboBox3.Text = "";

                MessageBox.Show("Successfully registered");



            }
            else //if ada masalah register
            {
                MessageBox.Show("User already registered");

            }

        }



    }

我放了display();在AddNewUser中,以便刷新,但我没有工作。

非常感谢您!

2 个答案:

答案 0 :(得分:1)

执行此操作的2种简单方法: 1-只需从SQL重新读取-如果很少有人可以同时更改它,这是一个很好的方法 2-数据绑定-使用大型且稳定的表的更好方法。

在表单加载时不要使用con.open!在命令前使用它,在执行后关闭。 甚至更好的方法是using(SqlConnection connection = new SqlConnection(connectionString))

准备一些方法,以命令作为参数从SQL读取(也许还有一些DataTable用于处理读取的数据),而不是将其写入3次。并从后面了解这种应用的外观。

答案 1 :(得分:0)

从第一个表单调用RegisterPopOut的方式是使用Show(),它使您可以单击后面的表单,而实际上不会冻结它。如果您不需要此功能,请尝试以下操作:

RegisterPopOut regPopOut = new RegisterPopOut();
regPopOut.ShowDialog();

这将暂停表单,并且直到使用this.Close()关闭新创建的表单后才会恢复。关闭新表单后,您应该可以安全地调用display()并且gridview将会更新

因此,请尝试更改此内容:

private void button2_Click(object sender, EventArgs e)
    {
        Employees.RegistrationPopOut RegPO = new Employees.RegistrationPopOut();
        RegPO.Show();
        display();
    }

对此:

private void button2_Click(object sender, EventArgs e)
    {
        Employees.RegistrationPopOut RegPO = new Employees.RegistrationPopOut();
        regPopOut.ShowDialog();
        display();
    }
相关问题