Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I have a window form application which i created with C# in it i created a UserControl Form with different control, i also have a main form which contain datagrid with values gotten from my database, i have a selected cell event, that opens my Usercontrol form any time each row is click, i want to be able to get the value of the selected row and pass it onto my UserControl that is automatically created, so the the different text box in my Usercontrol form can have the values of my Datagrid in my main form. Is this possible ?? please includes example codes. Thanks
Posted

void SelectedCellHandler(object sender, EventArgs e){
 object value = ((DataGrid)sender).SelectedCell.Value;
 Control newControl = new MyMagicUserControl(value);
 // do whatever you already do with the control
}


and in the user control, accept the value as a constructor parameter and do whatever you want with it, e.g.

class MyMagicUserControl: UserControl {
 public MyMagicUserControl(object value){
  // things that are already here e.g. InitializeComponent
  someTextBox.Text = value.ToString();
 }
}
 
Share this answer
 
No one will 'give' you code, but here are the steps you need to go through.

1. Create an event handler for the selectedindex changed event of your grid.

2. Create a user control and create an access to it. This basically can be done in a couple of ways:
2.a. Create public properties that can set and/or get the properties of the controls on the user control.
2.b. Create an object class with set/get properties of the object and pass that object entirely to the user control via a public property in your user control, or via the constructor.
2.c. Any variation of this.

3. For each event fired in the selectedindexchanged event you need to:
3.a remove the current user control from the form
3.b create the new user control and pass the information like described in 2
3.c. Add the newly created user control to the form.
--> Alternatively you can keep the user control and just change its data.


Hope this helps.
 
Share this answer
 

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