Click here to Skip to main content
11,790,816 members (54,994 online)
Rate this: bad
Please Sign up or sign in to vote.
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 22:18pm
Edited 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
0 OriginalGriff 385
1 ppolymorphe 339
2 Richard MacCutchan 118
3 Maciej Los 70
4 George Swan 70
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web03 | 2.8.1509028.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