Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中从其他表单更新datagridview#_C#_Sql - Fatal编程技术网

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

C# 在C中从其他表单更新datagridview#,c#,sql,C#,Sql,简而言之,我尝试过其他帖子的解决方案,但我无法让它工作。我是编程新手,我的工作给了我一个任务,就是制作一个软件 我创建了两个表单,其中第一个表单称为AddNewUser,另一个表单称为RegistrationPopOut,如果用户单击AddNewUser表单上的Register,则会弹出后一个表单 如果在RegistrationPopOut上单击“保存”,我希望AddNewUser中的datagridview自动更新自身 以下是AddNewUser的代码: private void

简而言之,我尝试过其他帖子的解决方案,但我无法让它工作。我是编程新手,我的工作给了我一个任务,就是制作一个软件

我创建了两个表单,其中第一个表单称为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();


    }
这是RegisterPoOut的代码

       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");

            }

        }



    }
我把显示器放进去();在AddNewUser中,使其刷新,但我没有工作

非常感谢你

2执行此操作的简单方法: 1-只需从SQL中再次阅读-如果很少人可以同时更改它,这是一个非常好的方法 2-数据绑定-更好的方式与大,相当稳定的表

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


预先准备一些从SQL读取命令作为参数的方法(可能还有一些用于处理读取数据的DataTable),而不是三次写入。并了解类似这样的应用程序应该如何从后面看

从第一个表单调用
registerpout
的方式是使用
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();
    }

您需要做的第一件事是阅读如何使用SQL参数。将数据粘贴到字符串中进行查询是错误的、过时的和危险的。然后研究数据绑定。如果这两个表单共享同一个数据源,您可以很容易地从其中任何一个添加更改和删除。