Hi,
I'm new to c#.
I am buil
I have a datagrid where that displays a few cells about projects and when the user clicks on a row I want to open a new instance of the projects form and display all the projects info (from a MySQL db).
I'm stuck on how to pass the data to the new instance of the projects form.
The new instance of the form displays ok, but how to reference the original form's text boxes and other objects is the problem I have.
Please help!
below is the line of code:
public void projectGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Public String sid;
//string sid = projectGrid.Rows[projectGrid.CurrentRow.Index].Cells["ID"].Value.ToString();
project f1 = new project();
f1.Show();// How to make this form load with the correct data?
}
Thanks
David
Loading
David DitudidiPosted Jul 27, 2011, 11:40 AM
Thanks a mil, I followed ur tip and with some more twiking I got it working.
Thanks evryone for ur answers!
Dave
Sam HobbsPosted Jul 25, 2011, 7:13 AM
There is a managed (.Net) interface to MySQL; you need to use that. I think you can use it to create a data source that includes a query that takes the id as a parameter. I would create a form bound to the data source. Then I would create a member in the form for the id and set that member in Form1 before showing the project form. Then in the project form there will be a function to fill the data source and that function will take the id as a parameter. You could create the project form without writing any code if you know how to use data sources. Yes, without writing any code except the two lines I described.
theLizardPosted Jul 25, 2011, 6:51 AM
public void projectGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Public String sid;
string sid = projectGrid.Rows[projectGrid.CurrentRow.Index].Cells["ID"].Value.ToString();
project f1 = new project();
f1.Controls["textBox2"].Text = sid;
f1.Show();// How to make this form load with the correct data?
}
David DitudidiPosted Jul 25, 2011, 3:39 AM
This is the scenario:
I have a datagrid in Form1 and it displays some information (e.g. project number, name, lead, and end date) about the projects that I currently have in a MySQL database.
What I want to do is, when a user double clicks any of the rows of the projects datagridview I want to open a form called project that will display all the information about the selected project.
The line below is to make sure that a new form is displayed each time (to cater for multiple users):
project f1 = new project();
f1.Show();
So f1 will display the result of a query ("select * from projects where id = sid" - sid being the value in the cell of the ID column for the selected project).
The problem that I have is this:
How to display the data (from the query result) in the correct text boxes, e.g. textBox1.text = rdr["projectName"];
How to get access to textBoxes etc of the project form from f1 - an instance of the project form.
Please if you still need more info, just let me know.
Thanks
David
Sam HobbsPosted Jul 24, 2011, 6:27 PM
You also are not making it clear whether you need to do a query of the database to get data for the new form. I assume you do and if so then it helps to make that clear.
I also don't understand why you must access the first form's controls instead of going the other direction as I describe. You can do it the way you want to if you want to or need to, but if you need to also do a database query then there are alternatives that will be easier.
David DitudidiPosted Jul 24, 2011, 6:01 PM
first thanks for the replies.
I think I did not give you a clear explanation of what i need.
I need to get access to the projects form's controls (e.g. text boxes etc) from f1, in order to display a query result.
Basicaly some way to reference projects form's controls.
thanks
David
Sam HobbsPosted Jul 24, 2011, 5:24 PM
I think it is strange that you are using the same form for both purposes, and perhaps you are not actuallly doing that; perhaps you just are not explaining what you are actually doing. If however you are using the same form, then it might work better to do as I explained above since for the first instance of the form there will not be a previous instance.
VulpesPosted Jul 24, 2011, 8:29 AM