Click here to Skip to main content
11,496,146 members (636 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: VB.NET Events DataTable DataRow , +

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 23:18pm
Edited 27-Jul-12 23:19pm
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
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
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2015
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