Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

On some event, I am adding a new row in DataTable using NewRow function. which results in calling another event of DataTable "OnColumnChanged". I want to get a RowIndex of recently added row.

When I try to obtain row index of newly added row using code :
VB
lDataRow = e.Row
lRowNumber = ldatatable.Rows.IndexOf(lDataRow)



I am getting lRowNumber i.e RowIndex as "-1". Instead of actual index.

I require, TableName, ColumnName, RowIndex, ProposedValue parameters for my next functioning.

How can I Obtain current RowIndex in this event, OR whats the other way around.

Thanks in advance.
Posted
Updated 27-Jul-12 22:19pm
v2

The NewRow() simply creates a row with the schema of the table. It does not add the row to the DataTable & you cannot get the index until you add the new row to the table. Here's an example
(assuming dt is the DataTable)

VB
Dim row As DataRow = dt.NewRow()
' IndexOf() will return -1 if called here
dt.Rows.Add(row)
' get the index now
MsgBox(dt.Rows.IndexOf(row))
 
Share this answer
 
Thanks strogg. You are absolutely correct.

Actually my problem is I am dynamically setting the data like SerialNumber etc in newly created row which gets updated into datasource and then goes to the client side where this new row is reflected in datagrid.

I have an idea about solving this problem. I'll be using RowChanged event of datatable to get index of newly added row and then use that for further procedure.

I am yet to implement this idea though. Hope this idea work for me.
 
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