ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

CurrentCellValue

This extension retrieves from the current row in a DataGridView a column value as a string. There are three ways to call this extension 1. No parameters returned the current active column for the active row. 2. Pass Column name for current active row. 3. Pass Column index for current active row.

Source

<Runtime.CompilerServices.Extension()> _
Function CurrentRowCellValue(ByVal GridView _
         As DataGridView) As String
    Return GridView.Rows(GridView.CurrentRow.Index) _
        .Cells(GridView.SelectedColumnNameIndex).Value.ToString
End Function

<Runtime.CompilerServices.Extension()> _
Function SelectedColumnNameIndex(ByVal GridView _
         As DataGridView) As Int32
    Return GridView.Columns(GridView _
                   .CurrentCell.ColumnIndex).Index
End Function

<Runtime.CompilerServices.Extension()> _
Function CurrentRowCellValue(ByVal GridView As DataGridView, _
                             ByVal ColumnIndex As Integer) As String
    Return GridView.Rows(GridView.CurrentRow.Index) _
            .Cells(ColumnIndex).Value.ToString
End Function

Example

Private Sub Button1_Click() Handles Button1.Click
    ' get current active cell
    Console.WriteLine(DataGridView1.CurrentRowCellValue)
    ' ask for company column, index 1
    Console.WriteLine(DataGridView1.CurrentRowCellValue("company"))
    ' ask for company by index
    Console.WriteLine(DataGridView1.CurrentRowCellValue(1))
End Sub

Author: Kevin S Gallagher

Submitted on: 26 okt. 2009

Language: VB

Type: System.Windows.Forms.DataGridview

Views: 4724