Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: GridviewDataTable
Hye, I use this code to convert gridview dataset into new datable.
Then when certain row for DEPT cell meets same condition as variable prodName,it will delete the datarow.
If dtDeleteSh contains only 1 row, this codes work. However datatable with more than 1 datarow will display an error "There is no row at position 1/2/3"
 
Dim dtDeleteSh As New DataTable
                         dtDeleteSh.Columns.Add("check")
                         dtDeleteSh.Columns.Add("DEPT")
                         dtDeleteSh.Columns.Add("NAME_T")
                         dtDeleteSh.Columns.Add("EMPID_T")
 

 
For shRow As Integer = 0 To gdShop.Rows.Count - 1
 
      Dim rowdeleteSh As DataRow = dtDeleteSh.NewRow()
      rowdeleteSh = dtDeleteSh.NewRow
      rowdeleteSh("check") = gdShop.Rows(shRow).Cells(0).Text
      rowdeleteSh("DEPT") = gdShop.Rows(shRow).Cells(1).Text
      rowdeleteSh("EMPID_T") = gdShop.Rows(shRow).Cells(2).Text
      rowdeleteSh("NAME_T") = gdShop.Rows(shRow).Cells(3).Text
 
      dtDeleteSh.Rows.Add(rowdeleteSh)
 
Next
 

For countSh As Integer = 0 To dtDeleteSh.Rows.Count - 1
 

     If prodName = dtDeleteSh.Rows(countSh)("DEPT").ToString Then
             dtDeleteSh.Rows(countSh).Delete()
 
     End If
Next
 

     gdShop.DataSource = dtDeleteSh
     gdShop.DataBind()
 
The error occurs here
 
 If prodName = dtDeleteSh.Rows(countSh)("DEPT").ToString Then
Posted 28-Aug-12 15:39pm
snamyna1.1K
Comments
dimpledevani at 29-Aug-12 0:46am
   
when you are deleting a row the total number of rows in your datatable also changes so the error is valid as you are in a loop.Try to make a copy of your datatable and in for loop for assigning the max value use one table and delete rows in other table

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this c#, plz convert to VB.net, I did some conversion hints try:
dtDeleteSh =   dtDeleteSh.AsEnumerable().Where(a => a["DEPT"].ToString()!=prodName ).CopyToDataTable();
 

Sample vb:(dont know it's correct syntax)
dtDeleteSh = (From del In dtDeleteSh _
Where del!DEPT.ToString <> prodName _
Select del).CopyToDataTable()
 
the logic is select rows that are to keep and avoid the one's to delete and reassign this to existing datatable.
  Permalink  
v4
Comments
snamyna at 29-Aug-12 0:45am
   
By using remove row, The next function would not execute because the datatable is modified. Am I right?
Kuthuparakkal at 29-Aug-12 1:56am
   
updated soln, plz check.. thats Linq method.
snamyna at 31-Aug-12 3:08am
   
Thanks. :)
Kuthuparakkal at 31-Aug-12 9:11am
   
You're welcome... did it help you ? if so mark answer

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 462
1 sanket saxena 355
2 OriginalGriff 235
3 Abhinav S 233
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,784
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 29 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid