Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating a form in .net windows application. Just watch the below codes.These codes for update process.

C#
 private void imgbtnUpdate1_Click(object sender, EventArgs e)
        {
            if ((txtPhoneNo.Text != "") && (dtpDateofRecharge.Text != "") && (cmbOperator.Text != "") && (txtAmount.Text != ""))
            {
                DialogResult dr = MessageBox.Show("Do you Want to Update?","Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update phonebill set PhoneNo='"+txtPhoneNo.Text+"',DateOfRecharge='"+dtpDateofRecharge.Value+"',
Operator='"+cmbOperator.Text+"',Amount='"+txtAmount.Text+"' where PhoneNo='"+txtPhoneNo.Text+"'",con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Updated Successfully","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);                                      
                }
            }
        }


C#
private void dgvPhone_CellEnter(object sender, DataGridViewCellEventArgs e)
       {
           txtPhoneNo.Text=dgvPhone.Rows[e.RowIndex].Cells["PhoneNo"].Value.ToString();
           dtpDateofRecharge.Text=dgvPhone.Rows[e.RowIndex].Cells["DateOfRecharge"].Value.ToString();
           cmbOperator.Text=dgvPhone.Rows[e.RowIndex].Cells["Operator"].Value.ToString();
           txtAmount.Text=dgvPhone.Rows[e.RowIndex].Cells["Amount"].Value.ToString();
       }



Now My Question is,if i use datagridview in a form 2, how can i place these datas in corresponding text boxes present in form 1 by using cell enter event.

Kindly help me.

Thanks,

Viswanathan.M
Posted
Updated 7-Aug-11 5:35am
v7

1 solution

hi,there may be two methods to do this task
>access two text boxes by setting properties
>make them public

Create the object of Next form which have text boxes.
Form2 f2=new Form2();
now on cell enter event you can use the code

C#
 f2.textbox1.text=dgvPhone.Rows[e.RowIndex].Cells["data1"].Value.ToString();
f2.textbox2.text=dgvPhone.Rows[e.RowIndex].Cells["data2"].Value.ToString();
f2.show();
 
Share this answer
 
v2
Comments
Viswanthan.M. 6-Aug-11 14:04pm    
Hi, where(location) should i create a object for form 2?
uspatel 6-Aug-11 14:10pm    
on cell enter event method
Viswanthan.M. 6-Aug-11 14:17pm    
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
Form2 f2 = new Form2();
f2.txtName.Text = dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
f2.txtId.Text = dataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
f2.Show();
}

If i do like this , it continuously open form 2.
uspatel 6-Aug-11 14:30pm    
use form taht have textboxes on that.
may be Form1 or form2 ,as you take it in your application.
if you want to show values on next form by clicking cells again and again
then create an object of Form(with twotext boxes) on previous form load event
and use
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
f2.close();
f2=new Form2();
f2.txtName.Text = dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
f2.txtId.Text = dataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
f2.Show();
}
Viswanthan.M. 7-Aug-11 6:44am    
I am using text boxes in form 1.In button click event in form 1, i use this code,
private void button4_Click(object sender, EventArgs e)
{
if ((txtName.Text != "") && (txtId.Text != ""))
{
DialogResult dr = MessageBox.Show("Do you Want to Update?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
con.Open();
SqlCommand cmd = new SqlCommand("update report set name='" + txtName.Text + "' where id='" + txtId.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
}

}

In form 2,in cell enter event of datagridview, i use this code,

Form1 f2 = new Form1();
f2.txtName.Text = dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
f2.txtId.Text = dataGridView1.Rows[e.RowIndex].Cells["Id"].Value.ToString();
}

If use like this i will get form1 again and again,If i click any row in database of datagridview, i will get form 1 again and again.

Kindly help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900