问题描述:

I'm a C#.Net Beginner!

I have 2 forms Mainform and Derogatory form..

My datagridview is in the Mainform, then my textboxs is in the derogatory form.

Question:

How to display value from datagridview in Mainform to texthbox in Derogatory Form after clicking the cell of datagridview from Mainform?

this is my code:!!

in Mainform

private void datagridDero_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

UpdateDerogatory Derogatory = new UpdateDerogatory(ref datagridDero);

Derogatory.Show();

}

**in updateDerogatory Form**

private void UpdateDerogatory_Load(object sender, EventArgs e)

{

Mainform main = new Mainform();

string[] items = new string[main.datagridDero.Columns.Count];

for (int i = 0; i < main.datagridDero.Columns.Count; i++)

{

items[i] = datagridDero[i, datagridDero.CurrentCell.RowIndex].Value.ToString();

}

txtfirstname.Text = items[0];

loadDerogatory();

}

My problem is in updatederogatory form can't call datagridview (datagridDero) from Mainform.

Thanks for your help!

网友答案:

It's been a while since I used Winforms, but personally I would use events: bind the appropriate DataGridView event (in this case CellClick is probably most relevant) to a method that lives inside Derogatory Form. From that method you should be able to access the event information, which should allow you to get the cell value, and display to a textbox inside Derogatory Form.

网友答案:

You can do all of this in your cell click event, simply grab all the data you want from your gridview, show your second form, set your values to what ever in the second form, then dispose of the first form. Or if you don't close the first form, you can simply call it directly from the second form. Either way will work, it is just a matter of what is available when.

One of the reasons this isn't working for you is because in the second form you are creating a new instance of main form Mainform main = new Mainform(); , this new instance has no data. Just reference the form name if the form still exists at this point.

相关阅读:
Top