Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all.

I have two forms form1 & form2.

Form1 contains 4 text boxes, edit button and form2 contains DataGridView.

My question is I want to display selected row data of DatagridView in respective text boxes as I clicked edit button.

Tell me whether this code is correct,

form1.text1.Text = DataGridView1.SelectedRows[0].Cells[0].Value.ToString();//and this is not retrieving data 


Help me out
Posted
Updated 16-Jan-11 21:51pm
v3

"Tell me whether this code is correct"
No, it isn't.


Oh, sorry, you want more?

No. It won't compile by default, and it rightfully shouldn't.
TextBoxes (and all other controls) are not declared public by default, so that you cannot access them from outside the form class.

Set up public properties in Form1 which take the string and put it into the relevant TextBox.

In addition, I would check each of the array accesses you make before trying to use it to access the next, to avoid null reference errors.
 
Share this answer
 
Comments
Dalek Dave 17-Jan-11 3:52am    
Seems Reasonable
Well, your question is not very clear for me.
You didn't specify the relationship between your forms (Are they Parent-Child or Siblings?).
And you also didn't specify, what kind of error you get (Compilation error, Exception, etc.).
At this moment the best I can do is to redirect you to this[^] excellent CodeProject article about passing values between forms. I hope it will be useful for you. :)
 
Share this answer
 
Comments
Nuri Ismail 17-Jan-11 3:56am    
From OP: hi Nuri Ismail, i'v DataGridView which having C1,C2,C3 columns.my question is i need to pass SelectedRow data in DataGridView to textBoxes (text1,text2,text3).plz tell me with an example
Nuri Ismail 17-Jan-11 4:02am    
@ks ravi: Please do not post fake answers, use Add Comment link (on the bottom-right corner of each answer). Your comment still does not clarify the relationship of your forms so I still recommend you to check out the link that I gave you in my answer. The article is well written and you will learn some useful stuff. Please give it a try.
Try this way please :


1. in grid view cell double click call the form in which u have the text boxes .

2. Pass the values of the data grid view through the forms object to the other forms constructor .

3. Assign the values to the local variables which you fetched through constructor .

4. assign the local variables to the respective text boxes.


----------

C#
if(e.rowIndex>=0 && gridview.RowIndex>0)
{

 Collection_Object SelectedRow = gridview.Rows[e.RowIndex].DataBoundItem as Collection_Object;

Form2 ret = new Form2(this, SelectedRow);
                ret.ShowDialog();

}



this will pass the grid view information to the next form , then you have to receive the values on the form2's constructor then assign to the controls as u desire ..
 
Share this answer
 
v2
form1.text1.Text = DataGridView1.item(0,0)Value.ToString() try this one
 
Share this answer
 
C#
txtRegID.Text = dt.Rows[0]["RegistrationID"].ToString();
 
Share this answer
 
v2
On Form2 Take 4 Public Variable having Appropriate data Type

Let Say

C#
public string Name;
public strint SurName;
 Public int CodeNo;
Public string  City;

public void FillControlsWith_Data()
{

                DataGridViewRow row = new DataGridViewRow();
                row = dataGridView1.CurrentRow;
                intRow = row.Index;
                Name = dataGridView1[0, row.Index].Value.ToString();
                surName= dataGridView1[1, row.Index].Value.ToString();
                CodeNo=int.parse( dataGridView1[2, row.Index].Value.ToString());
                City= dataGridView1[3, row.Index].Value.ToString();

}


On Form 1

Create An Object of form2 Because by Creating object
of from 2 we can access its public propertie by . operator

C#
Form2 ObjFrm2=new Form2();
TextBox1.Text=ObjFrm2.Name.tostring();
TextBox2.Text=ObjFrm2.SurName.tostring();
TextBox3.Text=ObjFrm2.Codeno..tostring();
TextBox4.Text=ObjFrm2.City.tostring();




Hope It Will Help You..:)
 
Share this answer
 
v2
If you are using a LINK BUTTON in your grid view, you can use the following code in the ROWCOMMAND method... This code with retrieve all the values in the particular selected row.

C#
// to get the value of the link use the command argument 
FaultId = Convert.ToInt32(e.CommandArgument);

// to get the other column values 
UserId = Convert.ToInt32(((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[1].Text);  

Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;

ProblemType = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[3].Text;


create a session variables for all these collected information in the same page. Use them in the next page... Assign those values to the corresponding text boxes in it

PAGE 1 (SESSION VARIABLE CREATION)

C#
Session["userId"] = UserId;
Session["dept"]=Department;



PAGE 2: (using the session variable)

C#
txtUserId.Text = Convert.ToInt32(Session["UserId"]);
txtDept.Text =  Session["Department"].ToString();


Similar way you can do it for all variables fetched from the seleted row.

[edit]code block added[/edit]
 
Share this answer
 
v2
Try This...

textbox1.text=Gridview.CurrentRow.Cells[1].Value.ToString();
 
Share this answer
 
Comments
CHill60 3-Feb-14 7:41am    
Reason for my downvote - apart from the facts that the original post is 3 years old and already correctly answered, your suggestion is very similar to other "solutions" below - all downvoted for good reasons. Finally - this will not work - the controls are on different forms.

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