Datagrid - no value at index ### error when scroll and sort

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 3
Reputation: Kelleyj is an unknown quantity at this point 
Solved Threads: 0
Kelleyj's Avatar
Kelleyj Kelleyj is offline Offline
Newbie Poster

Datagrid - no value at index ### error when scroll and sort

 
0
  #1
Apr 11th, 2005
I have a form with a datagrid on it. If I 1) scroll to the right to a field that wasn't originally displayed on the grid 2) click on the header of that field to sort the datagrid 3) then scroll down to bottom area of the datagrid and selects a cell that is in the bottom righthand side of the grid (basically one that wasn't shown when datagrid 1st came up) 4) press a button that reloads the datagrid --- An error is generated "IndexOutOfRangeException: No value at index ###". The number(###) always changes. See full message below. I've tried just scrolling without sorting, and it had no error. If I sort without scrolling, it had no error. If I squish the fields over to the left so that the field I'm sorting with is on the original data grid -- then sort -- then scroll down to the bottom right cell --- it did not have an error. It seems to be a problem if I'm sorting on a colum is off to the right and I select a field that is below the area that was originally display. Does anyone have any ideas? I've included the error message and the area of code that reloads the datagrid. Line 3148 is dgForm1.DataSource = mydataview.


  1. System.IndexOutOfRangeException: No value at index 49.
  2.  
  3. at System.Windows.Forms.CurrencyManager.get_Item(Int3 2 index)
  4.  
  5. at System.Windows.Forms.DataGridColumnStyle.GetColumn ValueAtRow(CurrencyManager source, Int32 rowNum)
  6.  
  7. at System.Windows.Forms.DataGridTextBoxColumn.Edit(Cu rrencyManager source, Int32 rowNum, Rectangle bounds, Boolean readOnly, String instantText, Boolean cellIsVisible)
  8.  
  9. at System.Windows.Forms.DataGrid.Edit(String instantText)
  10.  
  11. at System.Windows.Forms.DataGrid.Edit()
  12.  
  13. at System.Windows.Forms.DataGrid.set_HorizontalOffset (Int32 value)
  14.  
  15. at System.Windows.Forms.DataGrid.LayoutScrollBars()
  16.  
  17. at System.Windows.Forms.DataGrid.ComputeLayout()
  18.  
  19. at System.Windows.Forms.DataGrid.OnLayout(LayoutEvent Args levent)
  20.  
  21. at System.Windows.Forms.Control.PerformLayout(Control affectedControl, String affectedProperty)
  22.  
  23. at System.Windows.Forms.Control.PerformLayout()
  24.  
  25. at System.Windows.Forms.DataGrid.OnColumnCollectionCh anged(Object sender, CollectionChangeEventArgs e)
  26.  
  27. at System.Windows.Forms.DataGridTableStyle.OnColumnCo llectionChanged(Object sender, CollectionChangeEventArgs e)
  28.  
  29. at System.Windows.Forms.GridColumnStylesCollection.On CollectionChanged(CollectionChangeEventArgs ccevent)
  30.  
  31. at System.Windows.Forms.GridColumnStylesCollection.Co lumnStylePropDescChanged(Object sender, EventArgs pcea)
  32.  
  33. at System.Windows.Forms.DataGridColumnStyle.OnPropert yDescriptorChanged(EventArgs e)
  34.  
  35. at System.Windows.Forms.DataGridColumnStyle.set_Prope rtyDescriptor(PropertyDescriptor value)
  36.  
  37. at System.Windows.Forms.DataGridTextBoxColumn.set_Pro pertyDescriptor(PropertyDescriptor value)
  38.  
  39. at System.Windows.Forms.DataGrid.PairTableStylesAndGr idColumns(CurrencyManager lm, DataGridTableStyle gridTable, Boolean forceColumnCreation)
  40.  
  41. at System.Windows.Forms.DataGrid.SetDataGridTable(Dat aGridTableStyle newTable, Boolean forceColumnCreation)
  42.  
  43. at System.Windows.Forms.DataGrid.Set_ListManager(Obje ct newDataSource, String newDataMember, Boolean force, Boolean forceColumnCreation)
  44.  
  45. at System.Windows.Forms.DataGrid.Set_ListManager(Obje ct newDataSource, String newDataMember, Boolean force)
  46.  
  47. at System.Windows.Forms.DataGrid.set_DataSource(Objec t value)
  48.  
  49. at SalesRaptorPC.form1.loaddatagrid() in C:\SalesRaptor PC\SalesRaptorPC\Form1.vb:line 3148
------------------------------------------------------------------------------------------------------------------------------


  1. Dim sqlConn As SqlConnection = New SqlConnection(Connection)
  2.  
  3. Try
  4.  
  5. sqlConn.Open()
  6.  
  7. Dim sqlcomm AsNew SqlDataAdapter("salesRaptor_GetActivityPC", sqlConn)
  8.  
  9. sqlcomm.SelectCommand.CommandType = CommandType.StoredProcedure
  10.  
  11. Dim workParam As SqlParameter = New SqlParameter
  12.  
  13. workParam = sqlcomm.SelectCommand.Parameters.Add("@Rep", SqlDbType.VarChar, 25)
  14.  
  15. If chkAllReps.CheckState = CheckState.Checked Then
  16.  
  17. sqlcomm.SelectCommand.Parameters("@Rep").Value = Rep_ID.ToString
  18.  
  19. Else
  20.  
  21. sqlcomm.SelectCommand.Parameters("@Rep").Value = txRepID.Text
  22.  
  23. EndIf
  24.  
  25. workParam.Direction = ParameterDirection.Input
  26.  
  27. workParam = sqlcomm.SelectCommand.Parameters.Add("@Status", SqlDbType.VarChar, 25)
  28.  
  29. sqlcomm.SelectCommand.Parameters("@Status").Value = "P"
  30.  
  31. workParam.Direction = ParameterDirection.Input
  32.  
  33. Dim tempdate1 As DateTime = MthCalForm1.SelectionStart.Date + " 12:00:01 AM"
  34.  
  35. workParam = sqlcomm.SelectCommand.Parameters.Add("@DateLower", SqlDbType.SmallDateTime)
  36.  
  37. If actdelsw <> "DELINQUENT" Then sqlcomm.SelectCommand.Parameters("@DateLower").Value = tempdate1
  38.  
  39. workParam.Direction = ParameterDirection.Input
  40.  
  41. workParam = sqlcomm.SelectCommand.Parameters.Add("@DateUpper", SqlDbType.SmallDateTime)
  42.  
  43. If actdelsw = "DELINQUENT" Then
  44.  
  45. Dim tempdate2 AsDate = Now()
  46.  
  47. sqlcomm.SelectCommand.Parameters("@DateUpper").Value = tempdate2
  48.  
  49. Else
  50.  
  51. Dim tempdate2 AsDate = MthCalForm1.SelectionEnd.Date + " 11:59:59 PM"
  52.  
  53. sqlcomm.SelectCommand.Parameters("@DateUpper").Value = tempdate2
  54.  
  55. EndIf
  56.  
  57. workParam.Direction = ParameterDirection.Input
  58.  
  59. workParam = sqlcomm.SelectCommand.Parameters.Add("@Delinquent", SqlDbType.VarChar, 1)
  60.  
  61. If actdelsw = "DELINQUENT" Then
  62.  
  63. sqlcomm.SelectCommand.Parameters("@Delinquent").Value = "D"
  64.  
  65. Else
  66.  
  67. sqlcomm.SelectCommand.Parameters("@Delinquent").Value = "A"
  68.  
  69. EndIf
  70.  
  71. workParam.Direction = ParameterDirection.Input
  72.  
  73. workParam = sqlcomm.SelectCommand.Parameters.Add("@WholeTeam", SqlDbType.VarChar, 1)
  74.  
  75. If chkAllReps.CheckState = CheckState.Checked Then
  76.  
  77. sqlcomm.SelectCommand.Parameters("@WholeTeam").Value = "Y"
  78.  
  79. Else
  80.  
  81. sqlcomm.SelectCommand.Parameters("@WholeTeam").Value = "N"
  82.  
  83. EndIf
  84.  
  85. workParam.Direction = ParameterDirection.Input
  86.  
  87. workParam = sqlcomm.SelectCommand.Parameters.Add("@Include_Inactive", SqlDbType.VarChar, 1)
  88.  
  89. sqlcomm.SelectCommand.Parameters("@Include_Inactive").Value = "N"
  90.  
  91. workParam.Direction = ParameterDirection.Input
  92.  
  93. Dim ds AsNew DataSet
  94.  
  95. sqlcomm.Fill(ds, "ACTIVITYGRID") 'holds data from query
  96.  
  97. ds.Tables(0).Columns(17).ColumnMapping = MappingType.Hidden ' hide the record status field
  98.  
  99. Dim x = ds.Tables(0).Rows.Count()
  100.  
  101. Dim mydatatable As DataTable = ds.Tables(0) 'table name
  102.  
  103. Dim mydataview As DataView = mydatatable.DefaultView
  104.  
  105. mydataview.Sort = "Date / Time" 'sort table
  106.  
  107. dgForm1.DataSource = mydataview 'binds data to grid
  108.  
  109. Dim ts1 AsNew DataGridTableStyle ' change column widths
  110.  
  111. ts1.MappingName = "ACTIVITYGRID"
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Try
  118. dgForm1.TableStyles.Add(ts1)
  119.  
  120.  
  121.  
  122. ts1.GridColumnStyles(0).Width = 130 ' change width of date/time fld
  123.  
  124. ts1.GridColumnStyles(1).Width = 150 ' change width of prospect name
  125.  
  126. ' date datetime fields to show time
  127.  
  128. Dim dgtbc As DataGridTextBoxColumn
  129.  
  130. dgtbc = CType(dgForm1.TableStyles(0).GridColumnStyles(0), DataGridTextBoxColumn)
  131.  
  132. IfNot dgtbc IsNothingThen
  133.  
  134. dgtbc.Format = "g"
  135.  
  136. EndIf
  137.  
  138. dgtbc = CType(dgForm1.TableStyles(0).GridColumnStyles(7), DataGridTextBoxColumn)
  139.  
  140. IfNot dgtbc IsNothingThen
  141.  
  142. dgtbc.Format = "g"
  143.  
  144. EndIf
  145.  
  146.  
  147.  
  148. Catch
  149.  
  150. EndTry
  151.  
  152. ts1.DataGrid.Refresh()
  153.  
  154. Try
  155.  
  156. ts1.DataGrid.Select(0)
  157.  
  158. Catch
  159.  
  160. EndTry
  161.  
  162. sqlConn.Close()
  163.  
  164. Catch tt As Exception
  165.  
  166. Errmsg = tt.ToString
  167.  
  168. EventSW = "Y"
  169.  
  170. errormsg() ' display error msg
  171.  
  172. sqlConn.Close()
  173.  
  174. Finally
  175.  
  176. sqlConn.Close()
  177.  
  178. EndTry
  179.  
  180. Dim numRows AsInteger = dgForm1.BindingContext(dgForm1.DataSource, dgForm1.DataMember).Count
  181.  
  182. txRecCount.Text = "Count: " + CStr(numRows.ToString)
  183.  
  184. If (dgForm1.CurrentRowIndex < numRows) And (dgForm1.CurrentRowIndex > -1) Then
  185.  
  186. Entityid = CStr(dgForm1(dgForm1.CurrentRowIndex, 13).ToString) 'store current entity id
  187.  
  188. Prospid = CStr(dgForm1(dgForm1.CurrentRowIndex, 12).ToString) 'store current prospect id
  189.  
  190. activityrepid = CStr(dgForm1(dgForm1.CurrentRowIndex, 17).ToString) 'store current activity rep id
  191.  
  192. Contactid = CStr(dgForm1(dgForm1.CurrentRowIndex, 15).ToString) 'store current contact id
  193.  
  194. ActContid = CStr(dgForm1(dgForm1.CurrentRowIndex, 14).ToString) 'store current activity id
  195.  
  196. activitypriority = CStr(dgForm1(dgForm1.CurrentRowIndex, 6).ToString) 'store current activity priority
  197.  
  198. ActivityNotes = CStr(dgForm1(dgForm1.CurrentRowIndex, 3).ToString) 'store current regarding
  199.  
  200. EndIf
  201.  
  202. validateEntityID()
  203.  
  204. getprospectinfo() ' get prospect information
  205.  
  206. getcontactinfo() ' get contact information
  207.  
  208. If tb1.Visible = TrueThen
  209.  
  210. showtabprospectinfo() ' display info on prospect tab
  211.  
  212. EndIf
  213.  
  214. If tb2.Visible = TrueThen
  215.  
  216. showtabcontactinfo() ' display info on contact tab
  217.  
  218. EndIf
  219.  
  220. If tb3.Visible = TrueThen
  221.  
  222. showtabspousechildreninfo() ' display info on spouse children tab
  223.  
  224. EndIf
  225.  
  226. If tb4.Visible = TrueThen
  227.  
  228. showtabhistoryinfo() ' display info on history tab
  229.  
  230. EndIf
  231.  
  232. If tb5.Visible = TrueThen
  233.  
  234. showtabmachinelisting() ' display info on machine listing tab
  235.  
  236. EndIf
  237.  
  238. If tb6.Visible = TrueThen
  239.  
  240. showtabnotes() ' display info on notes tab
  241.  
  242. EndIf
  243.  
  244. If tb7.Visible = TrueThen
  245.  
  246. showtabcompetitormachines() ' display info on competitor machine listing tab
  247.  
  248. EndIf
  249.  
  250. showinqbuttons() ' display inq tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 1
Reputation: BobBeix is an unknown quantity at this point 
Solved Threads: 0
BobBeix BobBeix is offline Offline
Newbie Poster

Re: Datagrid - no value at index ### error when scroll and sort

 
0
  #2
Sep 29th, 2006
I had the same problem. I would bind a datagrid to 65 rows of data. I'd scroll down to the end of the list and select a cell, perhaps in row 62. I'd then click on a control outside the grid that would cause a re-bind the grid. The new underlying collection of items had less than 65 items, perhaps 50 rows. After re-binding the grid the DataGridTextBoxColumn edit method would fire, but it still had the old row number of the current cell (in this case rowNum would be 62). This would cause a No Value at Index 62 error, because this row no longer exists in the underlying collection. I fixed the problem by inserting the following code before the rebind.

  1. MyDataGrid.CurrentCell = Nothing

By setting this property to nothing you're eliminating the call to edit that occurs after the binding. Hope this helps.

Bob

Originally Posted by Kelleyj View Post
I have a form with a datagrid on it. If I 1) scroll to the right to a field that wasn't originally displayed on the grid 2) click on the header of that field to sort the datagrid 3) then scroll down to bottom area of the datagrid and selects a cell that is in the bottom righthand side of the grid (basically one that wasn't shown when datagrid 1st came up) 4) press a button that reloads the datagrid --- An error is generated "IndexOutOfRangeException: No value at index ###". The number(###) always changes. See full message below. I've tried just scrolling without sorting, and it had no error. If I sort without scrolling, it had no error. If I squish the fields over to the left so that the field I'm sorting with is on the original data grid -- then sort -- then scroll down to the bottom right cell --- it did not have an error. It seems to be a problem if I'm sorting on a colum is off to the right and I select a field that is below the area that was originally display. Does anyone have any ideas? I've included the error message and the area of code that reloads the datagrid. Line 3148 is dgForm1.DataSource = mydataview.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC