代码之家  ›  专栏  ›  技术社区  ›  mahesh

将行添加到绑定的dgv by文本框

  •  0
  • mahesh  · 技术社区  · 14 年前

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

    未绑定的dgv:

    private void Form2_Load(object sender, EventArgs e)
        {
            DataGridViewColumn srno =  new DataGridViewTextBoxColumn();
            dataGridView1.Columns.Insert(0, srno);
            DataGridViewColumn part = new  DataGridViewTextBoxColumn();
            dataGridView1.Columns.Insert(0, part);
            DataGridViewColumn cts = new  DataGridViewTextBoxColumn();
            cts.ValueType = typeof(decimal);
            dataGridView1.Columns.Insert(0, cts);
            DataGridViewColumn rt =new  DataGridViewTextBoxColumn();
            rt.ValueType = typeof(decimal);
            dataGridView1.Columns.Insert(0, rt);
            DataGridViewColumn debit =new  DataGridViewTextBoxColumn();
            debit.ValueType = typeof(decimal);
            dataGridView1.Columns.Insert(0, debit);
    
    
        }
        // textBox EventHandler
         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter) 
            {
                    int i = dataGridView1.CurrentCell.RowIndex;
                    dataGridView1[1, i].Value = textBox1.Text;
                    dataGridView1.Focus();
    
            }
    
        }
    

    绑定DGV:

           private void Form1_Load(object sender, EventArgs e)
        {
            string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
            SqlConnection con = new SqlConnection(connstr);
            con.Open();
            DataSet mydatasett;
            string dgv = " select srno,particulars,carats,rate,debit from depurchaseA";
            SqlCommand dgvcmd = new SqlCommand(dgv, con);
            SqlDataAdapter dgvdap = new SqlDataAdapter(dgvcmd);
            mydatasett = new DataSet();
            dgvdap.Fill(mydatasett);
            bindingsource2 = new BindingSource();
            bindingsource2.DataSource = mydatasett;
            bindingsource2.DataMember = mydatasett.Tables[0].TableName;
            dataGridView1.DataSource = bindingsource2;
    
        }
    
       **//And textbox Event handler :**
       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter) 
            {
                    int i = dataGridView1.CurrentCell.RowIndex;
                    dataGridView1[1, i].Value = textBox1.Text;
                    dataGridView1.Focus();
    
            }
    
        }
    

    上述方法在未绑定的DGV上效果良好,但在绑定的DGV中效果不佳。我想将文本框的输入添加到绑定的DGV中。有什么简单的方法吗?.

    3 回复  |  直到 14 年前
        1
  •  1
  •   Stephen Gerard Darragh    13 年前
    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();
    }
    
        2
  •  0
  •   Sprague    14 年前

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

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

        3
  •  0
  •   mahesh    14 年前

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

    在特定事件处理程序上挂起临时源,如下所示。

    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()方法有效。suspendbinding()方法将以不同的方式使用。