BindingManager detecting new row

P

PeterZ

Hi,

I have two tables and a dataRelation (win forms project). The dataRelation
is bound to a dataGrid. I'm trying to detect when a user navigates to a new
row in the datagrid so I can obtain a new record ID number from the db.

Screenshot:
http://www.insightgis.com.au/web/stuff/new_row_detect.gif


I do all this by assinging an event handler on the BindingManagerBase. The
sample code below works fine if the dataGrid is bound to a base table, but
throws an exception when bound to a relation. This is the line that makes
the baby cry - it's not happy casting the grid's dataSrouce as a DataTable:

bool lastRow = bm.Count > ((DataTable)myDataGrid.DataSource).Rows.Count



....... Declare the bindingManagerBase variable:

private BindingManagerBase bm


....... Create the event on the bindingManager:

bm = this.BindingContext[myDataGrid.DataSource,
"myParentTable.myChildRelation"];
bm.PositionChanged += new EventHandler(bm_PositionChanged);



....... Event handler:

private void bm_PositionChanged(object sender, EventArgs e)
{
try
{
// Check if we are on a new row.

bool lastRow = bm.Count > ((DataTable)myDataGrid.DataSource).Rows.Count;
// Above line throws an exception "Specified cast is not valid".
// This only happens when the datagrid is bound to a relation, works
// fine on base tables.

if (lastRow)
{
string sNewID = ..... Fire off stored procedure to get next ID .....
DataRowView drv = (DataRowView) bm.Current;
drv["LIST_ID"] = sNewID;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


I repeat once more, all of the above works fine except when the dataGrid is
bound to a relation.

Anyone have any ideas?

Cheers,
PeterZ
 

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