2010-11-20 7 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で同じではない作品に正常に動作します。 Bound DGVにtextBoxの入力を追加したいと思います。簡単な方法はありますか?

答えて

0

想起または

は、以下のような特定のイベントハンドラにtemparoryのソースを中断するの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()メソッドのみがWorksです。 SuspendBinding()メソッドは異なる方法で利用されます。

0

これはしばらくしていますが、データグリッドビューを試しましたか?[i] .Cells [1] .Value = textBox1.Text?

私はあなたのtexboxハンドラにブレークポイントを置き、dataGridView1.CurrentCellの値をチェックし、それが単一の値であること、そしてそれがあなたが期待しているものを指していることを確認します。

+0

これは機能しません。これは私が上記で使用したのと同じ方法です。違いはありません。 – mahesh

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(); 
} 
関連する問題