Click here to Skip to main content
13,201,050 members (56,698 online)
Rate this:
Please Sign up or sign in to 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 16-Jan-11 21:08pm
ks ravi849
Updated 16-Jan-11 21:51pm
Dalek Dave433.3K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

"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.
Dalek Dave 17-Jan-11 3:52am
Seems Reasonable
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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. :)
Nuri Ismail 17-Jan-11 3:56am
From OP: hi Nuri Ismail, i'v DataGridView which having C1,C2,C3 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.


if(e.rowIndex>=0 && gridview.RowIndex>0)
 Collection_Object SelectedRow = gridview.Rows[e.RowIndex].DataBoundItem as Collection_Object;
Form2 ret = new Form2(this, SelectedRow);

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 ..
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

form1.text1.Text = DataGridView1.item(0,0)Value.ToString() try this one
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

txtRegID.Text = dt.Rows[0]["RegistrationID"].ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 7

On Form2 Take 4 Public Variable having Appropriate data Type

Let Say

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

Form2 ObjFrm2=new Form2();

Hope It Will Help You..:)
Rate this: bad
Please Sign up or sign in to vote.

Solution 9

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.

// 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


Session["userId"] = UserId;

PAGE 2: (using the session variable)

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]
Rate this: bad
Please Sign up or sign in to vote.

Solution 10

Try This...

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 3 Feb 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100