Index was out of range. Must be non-negative and less than the size of the collection. – On close form – DataGrid column

When closing a Windows Form on a button using Me.Close then get the following:

Error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Solution

Tabs with Detail and List using a DataGridView, both

Had switched a BindingSource from a data table to a data source (object) which used a different field, so the DataGridView lookup column now had an invalid column.

Change to new column name and it will work.

End.

 

Advertisements

Windows Forms DataGridView Formatting

Time Formatting

To format Null set the Null Value in designer, or code for empty string.

Found when binding to a POCO class with TimeSpan that had some errors with some formats. hh\:mm seems to work.

http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

To format 00:00 as blank then:

    Private Sub TimeDataGridView_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles TimeDataGridView.CellFormatting

        If e.ColumnIndex <> myColumn.Index Then
            Exit Sub
        End If
        If e.Value = New TimeSpan(0, 0, 0) Then
            e.Value = ""
            e.FormattingApplied = True
        End If

    End Sub

End

WinForms Designer fails – warning …My.Resources.Resources has no property named … image

SOLUTION from Dave Anson C comment

http://connect.microsoft.com/VisualStudio/feedback/details/540265/designer-fails-warning-my-resources-resources-has-no-property-named

In the project References folder ensure there isn’t a reference to itself. (E.g. You have a project named WindowsApplication1. In the References folder you have a reference to WindowsApplication1)

This worked for me.

How do I … use Extension Methods in VB.Net and compile and use them in multiple projects

If you take the following example. Please note:

  • Import CompilerServices
  • Namespace
  • Public Module
  • <Extesion()> attribute

Compile as normal into a dll and reference this dll in another project.
Then to use this extension import the Namespace dll root namespace .NewFeatures to the top of the class/form that you wish to use it on. Should then work.

 

Imports System.Runtime.CompilerServices

Namespace NewFeatures
Public Module BindingSourceExtension

<Extension()> _
Sub RemoveFilterSort(ByVal aBindingSource As BindingSource)
If aBindingSource.Filter IsNot Nothing Then aBindingSource.RemoveFilter()
If aBindingSource.Sort IsNot Nothing Then aBindingSource.RemoveSort()
End Sub

End Module
End Namespace

 

 

How do I … modify the appearance of a Windows.Forms.TableLayoutPanel at run-time

http://social.msdn.microsoft.com/Forums/en-US/vbpowerpacks/thread/ad57487d-d7e0-4131-a388-cc3815809033/

The following adjusts the column width

Me.TableLayoutPanel1.ColumnStyles(0).Width = 10.0

The following gets the actual control. Making this invisible, leaves the table cell in place, same size.

Me.LayoutPanelGraph.GetControlFromPosition(colIndex, rowIndex).Visible = False

In my own code this is seen in SRS.Windows.Forms.ListBoxFilter

How do I hide missing data values in DataVisualization.Charting.Chart e.g. Saturdays and Sundays

 

If data is missing for say days of weekend then the chart will by default still include these dates in the X values so there will be a not pretty gap. This can be corrected with:

Series.IsXValueIndexed = True

How do I set the DataVisualization.Charting.Chart Column Gap

The column gap is set on the:

Chart > Series > Misc > Point Width – default = 0.8 for no gap use 1

DataVisualization.Charting Cursors, Zooming, and Scrolling (Chart Controls)

 

I like the Zooming and Scrolling features of these Charts but find it difficult to find in the Chart Control. It is at:

Chart Areas > CursorX >

IsUserEnabled = True
IsUserSelectionEnabled = True

http://msdn.microsoft.com/en-us/library/dd456730

 

 

LINQ and Entity Framework with DbContext – Convert Anonymous type to Strongly Typed – Bind to ListBox

[ Personally I have a sample of this in EFSample1.sln ]
To convert anonymous type to strongly typed, you can add a class that does what you want, then Select New … with {.ID = …}
This allows you to say get data from more than one table and still have a strongly typed output. Will not be updateable.

To fill a ListBox use

Imports System.Linq ‘if namespace not already imported

See http://wp.me/p17IS4-9S

Dim result = …GetEmployees
ListBox.DataSource = result.ToList

To save on setting the DisplayMember use .ToString in the class

Public Class EmployeeList
Property ID As Integer
Property FullName As String

Overrides Function ToString() As String
Return FullName
End Function

End Class

Public Class EmployeeEF

Private context As New EFWinFormsDBContext.TestEFIEntities

Function GetEmployees() As System.Linq.IQueryable(Of EmployeeList)

Dim result = From emp As Employee In context.Employees Join con As Contact In context.Contacts On emp.EmployeeID Equals con.ID Order By con.FullName
Select New EmployeeList With {.ID = emp.EmployeeID, .FullName = con.FullName}

Return result

End Function

End Class

How do I … create a Custom DataGridViewColumn and DataGridViewCell

Uses may include:

  • Storing extra data in a cell
  • Controlling SortMode for all columns
Public Class DataGridViewPlanColumn
    Inherits DataGridViewTextBoxColumn

    Sub New()
        Me.CellTemplate = New DataGridViewPlanCell
        Me.SortMode = DataGridViewColumnSortMode.NotSortable
    End Sub

End Class

Public Class DataGridViewPlanCell
    Inherits DataGridViewTextBoxCell

    Property WO As Nullable(Of Integer)
End Class