Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Events DataTable DataRow , +
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 :
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
v2
Rate this: bad
good
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
dt.Rows.Add(row)
' get the index now
MsgBox(dt.Rows.IndexOf(row))
  Permalink  
Rate this: bad
good
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.
  Permalink  

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



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