My form1 has datagridview, that is populated by a dataset.

I want to pass the selected value of the datagridview to my other form which is form2. How could I do this?

Edited by AngelicOne: n/a

6 Years
Discussion Span
Last Post by SHatcher

Please be specific. in what format do you want to pass the data? are you gonna store them in local variables or you want to pass them into a form control? one way could be to make a method in the other form and pass the arguments which you need to it, then update whatever you want to update in the next form or just pass it with reference


I've tested the following and it works.

My form1 has a gridview and a button for edit

private void editbutton_Click(object sender, EventArgs e)
            EditStudent EditStudent = new EditStudent(Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value));

My EditStudent is my second form which has textboxes for editing. The primary key(student number), which is the cells[0] to be pass to my textbox1 in second form. My textbox1 in the second is disabled.

public EditStudent(string srno)

            textBox1.Text = srno;

Then I use a sql select statement to populate the other textboxes for editing.

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student WHERE Student_No = '"+textBox1.Text+"'", con);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            if (dt.Rows.Count > 0)
                textBox2.Text = dt.Rows[0]["Last_Name"].ToString();
                textBox3.Text = dt.Rows[0]["First_Name"].ToString();
                comboBox1.SelectedItem = dt.Rows[0]["Department"].ToString();
                comboBox2.SelectedItem = dt.Rows[0]["Course"].ToString();
                textBox4.Text = dt.Rows[0]["Address"].ToString();
                textBox5.Text = dt.Rows[0]["Contact_No"].ToString();

Do you think this is a good or maybe a fair approach?

Votes + Comments
A good way to provide a details view.

personally its good, as long as it works and doesnt violate any security / code quality. you know what they say about coding bra, there can only be standards but there arent any rules as to how you should do something. goodluck with your code. if you got any question feel free to post them here for some comments.


There's one error though.

I can't simply convert the numericupdown.value to string. It says decimal cannot implicitly convert to string. I have posted a separate thread about this, but please tell me if you know how to fix it.


AngelicOne Generally when it tells you that you cannot implicitly convert a variable to another type there's a simple solution :)

What cannot convert implicitly must be converted explicitly. What this means is... Rather than use something like

string varString1 = someInteger;

Which would be an implicit cast from integer to string you would use:

//Option 1
string varString1 = someInteger.ToString();
//Option 2
string varString1 = Convert.ToString(someInteger);

The first method works great for strings because virtually every data type has a .ToString() method. Many of the other possible conversions, however, do require the 2nd option :)

I'm not specifically seeing the variable conversion that's causing your issue based on the source code you provided so I can't give a specific example to match the error (I could just be missing it) but I hope this helps :)

Votes + Comments
well written

would anybody have this code in VB, my insertion to the textboxes on another form works but it goes to the first person in the grid even though i selected the third person
Thanks Steve

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.