how do I call dataGridView_RowEnter event from within buttonSave_ Click event?

H

hazz

dataGridView1.RowEnter += new
DataGridViewCellEventHandler(dataGridView1_RowEnter);


private void buttonSave_Click(object sender, EventArgs e)
{
//how do I activate RowEnter event below to obtain the the CustomerID as
I did below??????? <----------------------
}

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs
e)
{
Customer c = m_Customers[e.RowIndex];
MessageBox.Show(c.SID.ToString());
}


Thank you,
-hazz
 
H

hazz

private void buttonSave_Click(object sender, EventArgs e) {

Customer c = (CustomerdataGridView1.SelectedRows[0].DataBoundItem;
or
Customer c =
(Customer)dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].DataBoundItem;
// and then get the CustomerID property from the Customer object.
}
 
B

Bruce Wood

You might consider writing a handy property:

private Customer SelectedCustomer
{
get
{
if (dataGridView1.SelectedRows.Count == 0)
{
return null;
}
else
{
return
(Customer)dataGridView1.SelectedRows[0].DataBoundItem;
}
}
}

which would make your code easier to read:

private void buttonSave_Click(object sender, EventArgs e)
{
Customer c = this.SelectedCustomer;
if (c != null)
{
... do something with the customer ...
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top