Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 按文本框将行添加到绑定的DGV_C#_Visual Studio_Datagridview - Fatal编程技术网

C# 按文本框将行添加到绑定的DGV

C# 按文本框将行添加到绑定的DGV,c#,visual-studio,datagridview,C#,Visual Studio,Datagridview,我已经按照以下方式演示了DGV。 我正在尝试将文本框的输入添加到DGV中,如下所示 未绑定DGV: 绑定DGV: 上述方法适用于未绑定DGV,但不适用于绑定DGV。我想将textBox的输入添加到绑定的DGV中。有什么简单的方法吗?已经有一段时间了,但是您尝试过dataGridView.Rows[i].Cells[1].Value=textBox1.Text吗 我会在您的texbox处理程序中放置一个断点,并检查dataGridView1.CurrentCell的值,确保它是一个单一的值,并且它

我已经按照以下方式演示了DGV。 我正在尝试将文本框的输入添加到DGV中,如下所示

未绑定DGV: 绑定DGV:
上述方法适用于未绑定DGV,但不适用于绑定DGV。我想将textBox的输入添加到绑定的DGV中。有什么简单的方法吗?

已经有一段时间了,但是您尝试过dataGridView.Rows[i].Cells[1].Value=textBox1.Text吗


我会在您的texbox处理程序中放置一个断点,并检查dataGridView1.CurrentCell的值,确保它是一个单一的值,并且它也指向您期望的值。

有一个简单的解决方案,用于编辑/添加/删除要调用或删除的Datagridview行

在特定事件处理程序上挂起temparory的源,如下所示

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((Keys)e.KeyChar == Keys.Enter) 
        {

                bindingsource2.ResumeBinding (); // OR bindingsource2.SuspendBinding();       
                int i = dataGridView1.CurrentCell.RowIndex;
                dataGridView1[1, i].Value = textBox1.Text;
                dataGridView1.Focus();

        }

    }
在我的例子中,只有ResumeBinding()方法是Works。SuspendBinding()方法将以不同的方式使用。

private void btnUpdate\u单击(对象发送方,EventArgs e)
private void btnUpdate_Click(object sender, EventArgs e)
{
    //  private String connectionString = null;
    // private SqlConnection sqlConnection = null;

    btnBack.Enabled = true;
    sqlConnection.Open();

    dataGridView1.DataSource = bindingSource;

    //cmd = new SqlCommand("update empinfo set empname=@empname, empAdd=@empAdd, empMobile=@empMobile where empid=@empid", con);
    cmd = new SqlCommand("empinfo_Insert_Update_Delete", sqlConnection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd1 = new SqlCommand("Insert_Update_Delete_EmpSal", sqlConnection);
    cmd1.CommandType = CommandType.StoredProcedure;

    try
    {
        cmd.Parameters.AddWithValue("@empid", txtempId.Text);
        cmd.Parameters.AddWithValue("@empName", txtempName.Text);
        cmd.Parameters.AddWithValue("@empAdd", txtempAdd.Text);
        cmd.Parameters.AddWithValue("@empMobile", TxtempMobile.Text);
        cmd.Parameters.AddWithValue("@intflag", 1);
        //txtempId.Text = txtsalempId.Text;
        cmd1.Parameters.AddWithValue("@salId", txtsalId.Text);
        cmd1.Parameters.AddWithValue("@salAmount", txtsalary.Text);
        cmd1.Parameters.AddWithValue("@salDate", txtdos.Text);
        cmd1.Parameters.AddWithValue("@empId", txtempId.Text);
        cmd1.Parameters.AddWithValue("@intflag", 1);
        cmd.ExecuteNonQuery();

        cmd1.ExecuteNonQuery();
        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            dataTable.Rows[i][3] = dataTable.Rows[0][3];
        }

        sqlDataAdapter.Update(dataTable);
        //int b;
        //b = int.Parse(txtempId.Text);

        //selectQueryString1 = "SELECT * FROM empsal where empid=" + b;
        ////sqlDataAdapter1 = new SqlDataAdapter(selectQueryString1, sqlConnection);
        ////sqlCommandBuilder1 = new SqlCommandBuilder(sqlDataAdapter1);
        ////dataTable1 = new DataTable();
        ////sqlDataAdapter1.Fill(dataTable1);
        ////bindingSource1 = new BindingSource();
        ////bindingSource1.DataSource = dataTable1;
        ////dataGridView1.DataSource = bindingSource1;

        MessageBox.Show("data Updated");
    }
    catch (Exception exceptionObj)
    {
        MessageBox.Show(exceptionObj.Message.ToString());
    }                    
    cmd1 = null;            
    dataGridView1.DataSource = null;
    sqlConnection.Close();
    clearText();

    addcolumn();
    childform();
}
{ //私有字符串connectionString=null; //私有SqlConnection SqlConnection=null; btnBack.Enabled=true; sqlConnection.Open(); dataGridView1.DataSource=bindingSource; //cmd=new SqlCommand(“更新empinfo set empname=@empname,empAdd=@empAdd,empMobile=@empMobile,其中empid=@empid”,con); cmd=新的SqlCommand(“empinfo\u Insert\u Update\u Delete”,sqlConnection); cmd.CommandType=CommandType.storedProcess; cmd1=新的SqlCommand(“插入\更新\删除\临时”,sqlConnection); cmd1.CommandType=CommandType.StoredProcess; 尝试 { cmd.Parameters.AddWithValue(“@empid”,txtempId.Text); cmd.Parameters.AddWithValue(“@empName”,txtempName.Text); cmd.Parameters.AddWithValue(“@empAdd”,txtempAdd.Text); cmd.Parameters.AddWithValue(“@empMobile”,TxtempMobile.Text); cmd.Parameters.AddWithValue(“@intflag”,1); //txtempId.Text=txtsalempId.Text; cmd1.Parameters.AddWithValue(“@salId”,txtsalId.Text); cmd1.Parameters.AddWithValue(“@salAmount”,txtsalary.Text); cmd1.Parameters.AddWithValue(“@salDate”,txtdos.Text); cmd1.Parameters.AddWithValue(“@empId”,txtempId.Text); cmd1.Parameters.AddWithValue(“@intflag”,1); cmd.ExecuteNonQuery(); cmd1.ExecuteOnQuery(); for(int i=0;i
它不起作用,与我上面使用的方法相同。它不起作用。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((Keys)e.KeyChar == Keys.Enter) 
        {

                bindingsource2.ResumeBinding (); // OR bindingsource2.SuspendBinding();       
                int i = dataGridView1.CurrentCell.RowIndex;
                dataGridView1[1, i].Value = textBox1.Text;
                dataGridView1.Focus();

        }

    }
private void btnUpdate_Click(object sender, EventArgs e)
{
    //  private String connectionString = null;
    // private SqlConnection sqlConnection = null;

    btnBack.Enabled = true;
    sqlConnection.Open();

    dataGridView1.DataSource = bindingSource;

    //cmd = new SqlCommand("update empinfo set empname=@empname, empAdd=@empAdd, empMobile=@empMobile where empid=@empid", con);
    cmd = new SqlCommand("empinfo_Insert_Update_Delete", sqlConnection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd1 = new SqlCommand("Insert_Update_Delete_EmpSal", sqlConnection);
    cmd1.CommandType = CommandType.StoredProcedure;

    try
    {
        cmd.Parameters.AddWithValue("@empid", txtempId.Text);
        cmd.Parameters.AddWithValue("@empName", txtempName.Text);
        cmd.Parameters.AddWithValue("@empAdd", txtempAdd.Text);
        cmd.Parameters.AddWithValue("@empMobile", TxtempMobile.Text);
        cmd.Parameters.AddWithValue("@intflag", 1);
        //txtempId.Text = txtsalempId.Text;
        cmd1.Parameters.AddWithValue("@salId", txtsalId.Text);
        cmd1.Parameters.AddWithValue("@salAmount", txtsalary.Text);
        cmd1.Parameters.AddWithValue("@salDate", txtdos.Text);
        cmd1.Parameters.AddWithValue("@empId", txtempId.Text);
        cmd1.Parameters.AddWithValue("@intflag", 1);
        cmd.ExecuteNonQuery();

        cmd1.ExecuteNonQuery();
        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            dataTable.Rows[i][3] = dataTable.Rows[0][3];
        }

        sqlDataAdapter.Update(dataTable);
        //int b;
        //b = int.Parse(txtempId.Text);

        //selectQueryString1 = "SELECT * FROM empsal where empid=" + b;
        ////sqlDataAdapter1 = new SqlDataAdapter(selectQueryString1, sqlConnection);
        ////sqlCommandBuilder1 = new SqlCommandBuilder(sqlDataAdapter1);
        ////dataTable1 = new DataTable();
        ////sqlDataAdapter1.Fill(dataTable1);
        ////bindingSource1 = new BindingSource();
        ////bindingSource1.DataSource = dataTable1;
        ////dataGridView1.DataSource = bindingSource1;

        MessageBox.Show("data Updated");
    }
    catch (Exception exceptionObj)
    {
        MessageBox.Show(exceptionObj.Message.ToString());
    }                    
    cmd1 = null;            
    dataGridView1.DataSource = null;
    sqlConnection.Close();
    clearText();

    addcolumn();
    childform();
}