Windows Develop Bookmark and Share   
 index > Windows Forms General > how to delegate the data in datagridview from form1 to the datagridview in form2?
 

how to delegate the data in datagridview from form1 to the datagridview in form2?

good morning,

how to delegate the data in datagridview from form1 to the datagridview in form2?

i had try with a code shown below but it ain't work.



  • this is the data in gridview of form1. this data need to be transfer to the data gridview in form2.

public partial class Form1 : Form
{
public delegate void TransferAHPEA1DataHandler(object sender, TransferAHPEA1DataHandlerEventArgs e);
public event TransferAHPEA1DataHandler TransferAHPEA1Data;
private void button4_Click(object sender, EventArgs e)
{
TotalAHP totalAhp = new TotalAHP();
totalAhp.Show();
double b;
b = Convert.ToDouble(dataGridView1.Rows[0].Cells["Ratio1"].Value);
TransferAHPEA1DataHandlerEventArgs transfer = new TransferAHPEA1DataHandlerEventArgs(b);
TransferAHPEA1Data(this, transfer);

}
}

public class TransferAHPEA1DataHandlerEventArgs : System.EventArgs
{
double b1;
public TransferAHPEA1DataHandlerEventArgs( double b2)
{

this.b1 = b2;
}
public double b2
{
get
{
return b1;
}
}
}

  • thisi s the code for the data transferred from form1 in form2.
private void button1_Click(object sender, EventArgs e)
{
AHPEA1 ahpea1 = new AHPEA1();
ahpea1.TransferAHPEA1Data+=new AHPEA1.TransferAHPEA1DataHandler(ahpea1_TransferAHPEA1Data);
ahpea1.Show();
}
private void ahpea1_TransferAHPEA1Data(object sender, TransferAHPEA1DataHandlerEventArgs e)
{

double b2 = e.b2;
dataGridView1.Rows.Add(e.b2);
}


can somebody help me??
gzakri  Wednesday, May 20, 2009 9:58 AM
How does it not work?

If you step through the code, does it seem to be generating the event and passing the data along?

Are you pressing Button1 before button4? If not, it looks like you are setting up the event handler after you have raised the event....
www.insteptech.com
DeborahK  Wednesday, May 20, 2009 6:46 PM
Always check for null before invoking an event.

TransferAHPEA1DataHandlerEventArgs transfer = new TransferAHPEA1DataHandlerEventArgs(b);
if (TransferAHPEA1Data != null)
TransferAHPEA1Data(this, transfer);

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Wednesday, May 20, 2009 9:22 PM
Hi gzakri,

John's suggestion is right. But I have the same doubt as DeborahK, you were setting up event handler after you rasied the event, so the handler code will have no chance to execute. Would you please clarify?

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Tuesday, May 26, 2009 1:16 PM
Good point Bruce

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Tuesday, May 26, 2009 1:24 PM
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post editor window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Tuesday, June 02, 2009 5:21 AM

You can use google to search for other answers

Custom Search

More Threads

• Question about collapsing and resizing the splitContainer.
• How to toggle the highlighting on a row with every mouse click?
• message box dose not display.
• inserting the empty column in datagridview
• How to access a form behind ModelForm( dailog window)
• how to usercontrol with a dock property ?
• Easiest way to have a child initiate a parent's method?
• FlowLayoutPanel scroll
• Convert VBA To C#
• HELP... mail message opens with no attachment