datagrid - no value at index ## error

Joined
Feb 21, 2005
Messages
9
Location
Wisconsin
Programming Experience
10+
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.



System.IndexOutOfRangeException: No value at index 49.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

at System.Windows.Forms.DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager source, Int32 rowNum)

at System.Windows.Forms.DataGridTextBoxColumn.Edit(CurrencyManager source, Int32 rowNum, Rectangle bounds, Boolean readOnly, String instantText, Boolean cellIsVisible)

at System.Windows.Forms.DataGrid.Edit(String instantText)

at System.Windows.Forms.DataGrid.Edit()

at System.Windows.Forms.DataGrid.set_HorizontalOffset(Int32 value)

at System.Windows.Forms.DataGrid.LayoutScrollBars()

at System.Windows.Forms.DataGrid.ComputeLayout()

at System.Windows.Forms.DataGrid.OnLayout(LayoutEventArgs levent)

at System.Windows.Forms.Control.PerformLayout(Control affectedControl, String affectedProperty)

at System.Windows.Forms.Control.PerformLayout()

at System.Windows.Forms.DataGrid.OnColumnCollectionChanged(Object sender, CollectionChangeEventArgs e)

at System.Windows.Forms.DataGridTableStyle.OnColumnCollectionChanged(Object sender, CollectionChangeEventArgs e)

at System.Windows.Forms.GridColumnStylesCollection.OnCollectionChanged(CollectionChangeEventArgs ccevent)

at System.Windows.Forms.GridColumnStylesCollection.ColumnStylePropDescChanged(Object sender, EventArgs pcea)

at System.Windows.Forms.DataGridColumnStyle.OnPropertyDescriptorChanged(EventArgs e)

at System.Windows.Forms.DataGridColumnStyle.set_PropertyDescriptor(PropertyDescriptor value)

at System.Windows.Forms.DataGridTextBoxColumn.set_PropertyDescriptor(PropertyDescriptor value)

at System.Windows.Forms.DataGrid.PairTableStylesAndGridColumns(CurrencyManager lm, DataGridTableStyle gridTable, Boolean forceColumnCreation)

at System.Windows.Forms.DataGrid.SetDataGridTable(DataGridTableStyle newTable, Boolean forceColumnCreation)

at System.Windows.Forms.DataGrid.Set_ListManager(Object newDataSource, String newDataMember, Boolean force, Boolean forceColumnCreation)

at System.Windows.Forms.DataGrid.Set_ListManager(Object newDataSource, String newDataMember, Boolean force)

at System.Windows.Forms.DataGrid.set_DataSource(Object value)

at SalesRaptorPC.form1.loaddatagrid() in C:\SalesRaptor PC\SalesRaptorPC\Form1.vb:line 3148
------------------------------------------------------------------------------------------------------------------------------


Dim sqlConn As SqlConnection = New SqlConnection(Connection)

Try

sqlConn.Open()

Dim sqlcomm AsNew SqlDataAdapter("salesRaptor_GetActivityPC", sqlConn)

sqlcomm.SelectCommand.CommandType = CommandType.StoredProcedure

Dim workParam As SqlParameter = New SqlParameter

workParam = sqlcomm.SelectCommand.Parameters.Add("@Rep", SqlDbType.VarChar, 25)

If chkAllReps.CheckState = CheckState.Checked Then

sqlcomm.SelectCommand.Parameters("@Rep").Value = Rep_ID.ToString

Else

sqlcomm.SelectCommand.Parameters("@Rep").Value = txRepID.Text

EndIf

workParam.Direction = ParameterDirection.Input

workParam = sqlcomm.SelectCommand.Parameters.Add("@Status", SqlDbType.VarChar, 25)

sqlcomm.SelectCommand.Parameters("@Status").Value = "P"

workParam.Direction = ParameterDirection.Input

Dim tempdate1 As DateTime = MthCalForm1.SelectionStart.Date + " 12:00:01 AM"

workParam = sqlcomm.SelectCommand.Parameters.Add("@DateLower", SqlDbType.SmallDateTime)

If actdelsw <> "DELINQUENT" Then sqlcomm.SelectCommand.Parameters("@DateLower").Value = tempdate1

workParam.Direction = ParameterDirection.Input

workParam = sqlcomm.SelectCommand.Parameters.Add("@DateUpper", SqlDbType.SmallDateTime)

If actdelsw = "DELINQUENT" Then

Dim tempdate2 AsDate = Now()

sqlcomm.SelectCommand.Parameters("@DateUpper").Value = tempdate2

Else

Dim tempdate2 AsDate = MthCalForm1.SelectionEnd.Date + " 11:59:59 PM"

sqlcomm.SelectCommand.Parameters("@DateUpper").Value = tempdate2

EndIf

workParam.Direction = ParameterDirection.Input

workParam = sqlcomm.SelectCommand.Parameters.Add("@Delinquent", SqlDbType.VarChar, 1)

If actdelsw = "DELINQUENT" Then

sqlcomm.SelectCommand.Parameters("@Delinquent").Value = "D"

Else

sqlcomm.SelectCommand.Parameters("@Delinquent").Value = "A"

EndIf

workParam.Direction = ParameterDirection.Input

workParam = sqlcomm.SelectCommand.Parameters.Add("@WholeTeam", SqlDbType.VarChar, 1)

If chkAllReps.CheckState = CheckState.Checked Then

sqlcomm.SelectCommand.Parameters("@WholeTeam").Value = "Y"

Else

sqlcomm.SelectCommand.Parameters("@WholeTeam").Value = "N"

EndIf

workParam.Direction = ParameterDirection.Input

workParam = sqlcomm.SelectCommand.Parameters.Add("@Include_Inactive", SqlDbType.VarChar, 1)

sqlcomm.SelectCommand.Parameters("@Include_Inactive").Value = "N"

workParam.Direction = ParameterDirection.Input

Dim ds AsNew DataSet

sqlcomm.Fill(ds, "ACTIVITYGRID")
'holds data from query

ds.Tables(0).Columns(17).ColumnMapping = MappingType.Hidden ' hide the record status field

Dim x = ds.Tables(0).Rows.Count()

Dim mydatatable As DataTable = ds.Tables(0) 'table name

Dim mydataview As DataView = mydatatable.DefaultView

mydataview.Sort = "Date / Time"
'sort table

dgForm1.DataSource = mydataview 'binds data to grid

Dim ts1 AsNew DataGridTableStyle ' change column widths

ts1.MappingName = "ACTIVITYGRID"





Try
dgForm1.TableStyles.Add(ts1)



ts1.GridColumnStyles(0).Width = 130
' change width of date/time fld

ts1.GridColumnStyles(1).Width = 150 ' change width of prospect name

' date datetime fields to show time

Dim dgtbc As DataGridTextBoxColumn

dgtbc =
CType(dgForm1.TableStyles(0).GridColumnStyles(0), DataGridTextBoxColumn)

IfNot dgtbc IsNothingThen

dgtbc.Format = "g"

EndIf

dgtbc = CType(dgForm1.TableStyles(0).GridColumnStyles(7), DataGridTextBoxColumn)

IfNot dgtbc IsNothingThen

dgtbc.Format = "g"

EndIf



Catch

EndTry

ts1.DataGrid.Refresh()

Try

ts1.DataGrid.Select(0)

Catch

EndTry

sqlConn.Close()

Catch tt As Exception

Errmsg = tt.ToString

EventSW = "Y"

errormsg()
' display error msg

sqlConn.Close()

Finally

sqlConn.Close()

EndTry

Dim numRows AsInteger = dgForm1.BindingContext(dgForm1.DataSource, dgForm1.DataMember).Count

txRecCount.Text = "Count: " +
CStr(numRows.ToString)

If (dgForm1.CurrentRowIndex < numRows) And (dgForm1.CurrentRowIndex > -1) Then

Entityid = CStr(dgForm1(dgForm1.CurrentRowIndex, 13).ToString) 'store current entity id

Prospid = CStr(dgForm1(dgForm1.CurrentRowIndex, 12).ToString) 'store current prospect id

activityrepid = CStr(dgForm1(dgForm1.CurrentRowIndex, 17).ToString) 'store current activity rep id

Contactid = CStr(dgForm1(dgForm1.CurrentRowIndex, 15).ToString) 'store current contact id

ActContid = CStr(dgForm1(dgForm1.CurrentRowIndex, 14).ToString) 'store current activity id

activitypriority = CStr(dgForm1(dgForm1.CurrentRowIndex, 6).ToString) 'store current activity priority

ActivityNotes = CStr(dgForm1(dgForm1.CurrentRowIndex, 3).ToString) 'store current regarding

EndIf

validateEntityID()

getprospectinfo()
' get prospect information

getcontactinfo() ' get contact information

If tb1.Visible = TrueThen

showtabprospectinfo() ' display info on prospect tab

EndIf

If tb2.Visible = TrueThen

showtabcontactinfo() ' display info on contact tab

EndIf

If tb3.Visible = TrueThen

showtabspousechildreninfo() ' display info on spouse children tab

EndIf

If tb4.Visible = TrueThen

showtabhistoryinfo() ' display info on history tab

EndIf

If tb5.Visible = TrueThen

showtabmachinelisting() ' display info on machine listing tab

EndIf

If tb6.Visible = TrueThen

showtabnotes() ' display info on notes tab

EndIf

If tb7.Visible = TrueThen

showtabcompetitormachines() ' display info on competitor machine listing tab

EndIf

showinqbuttons() ' display inq tab


 
Back
Top