2010-11-20 20 views
0

我已經按照以下方式演示了DGV。 我想將文本框的輸入添加到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同樣沒有工作的罰款。我想將textBox的輸入添加到Bound DGV中。有沒有簡單的方法?

回答

0

存在用於編輯的簡單解決方案/添加/刪除行的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()方法將以不同的方式使用。

0

已經有一段時間了,但是你有沒有試過dataGridView.Rows [i] .Cells [1] .Value = textBox1.Text?

我會在你的texbox處理程序中放一個斷點,並檢查dataGridView1.CurrentCell的值,確保它是一個單一值,它也指向你期望的值。

+0

這是不工作,這是我用above.it的相同方式沒有差異。 – mahesh 2010-11-20 12:29:31

1
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 [email protected], [email protected], [email protected] where [email protected]", 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(); 
}