Add Row To Bound DGV By Textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    Add Row To Bound DGV By Textbox

    I have demonstrate the DGV as per following way.

    bound DGV:-

    I am trying to add input of textbox into DGV as below.
    Code:
            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 :

    Code:
     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();
    
                }
                
            }
    The above works fine on UnBound DGV but the same not works in Bound DGV. I wants to add the input of textBox into Bound DGV. Is there any simple way?.
  • maheshwag
    New Member
    • Aug 2010
    • 71

    #2
    There are a simple solution for Editing/Adding/Removing Row of Datagridview To Evoke or Suspend the source for temparory on specific event handler like below.

    Code:
    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();
    
            }
    
        }

    Here In my Case There is only ResumeBinding() Method is Works. The SuspendBinding( ) Method will utilize in different way.

    Comment

    Working...