Click here to Skip to main content
13,193,441 members (24,696 online)
Rate this:
Please Sign up or sign in to vote.

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 :
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 27-Jul-12 22:18pm
Updated 27-Jul-12 22:19pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

Dim row As DataRow = dt.NewRow()
' IndexOf() will return -1 if called here
' get the index now
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171018.2 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100