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?
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?
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));
EditStudent.Show();
}
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)
{
InitializeComponent();
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);
da.Fill(ds);
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?
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 :)
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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.