1 2 3 4 5  ... Previous Next 

DataGridView Caputere the characters from a cell input.

IronRazerz from the forums has shown this code. If the cells in your 6th column are TextBox types then you can do it like this using a DataGridViewTextBoxEditingControl. You will notice if you look at the DataGridViewTextBoxEditingControl events that it has a KeyDown event too if you need that instead.
Public Class Form1
    Private WithEvents EditControl As DataGridViewTextBoxEditingControl
    'this event is raised when a cell enters the editing mode
    Private Sub DGV_EditingControlShowing(ByVal sender As ObjectByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgsHandles DataGridView1.EditingControlShowing
        'here you are just checking if the Edit Control is Nothing and that the current cell is in the 6th column
        If EditControl Is Nothing AndAlso DataGridView1.CurrentCell.ColumnIndex = 5 Then
            'this casts the EditControl to the current cell`s edit control. This way you can access its events (TextChanged, KeyDown, or whatever)
            EditControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
        End If
    End Sub
    'this is the EditControl`s Leave event where you will set it to Nothing. Otherwise you will catch keys even if you edit another cell.
    Private Sub EditControl_Leave(sender As Object, e As EventArgsHandles EditControl.Leave
        EditControl = Nothing
    End Sub
    Private Sub EditControl_TextChanged(ByVal sender As ObjectByVal e As System.EventArgsHandles EditControl.TextChanged
        'cast the sender Object to a DataGridViewTextBoxEditingControl so that you can access the text of the cell that is in the editing mode
        Dim ec As DataGridViewTextBoxEditingControl = DirectCast(sender, DataGridViewTextBoxEditingControl)
        Label1.Text = ec.Text 'you will see the label change as the selected cell text is changing
    End Sub
End Class

DataGridView: Detect changed row

This sample shows how to use the currencymanager with a datagridview.
This sample needs only a form with two DataGridViews
Untitled 9
Public Class Form1
    Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgsHandles MyBase.Load
        Dim dt As New DataTable
        dt.LoadDataRow(New Object() {"Cor""Holland"}, True)
        dt.LoadDataRow(New Object() {"Ken""Florida"}, True)
        dt.LoadDataRow(New Object() {"Paul""Illinois"}, True)
        dt.LoadDataRow(New Object() {"Herfried""Austria"}, True)
        dt.LoadDataRow(New Object() {"Armin""Germany"}, True)
        dt.LoadDataRow(New Object() {"John""UK"}, True)
        dt.LoadDataRow(New Object() {"Mike""SjangHai"}, True)
        DataGridView1.DataSource = dt.DefaultView
        Dim cma As CurrencyManager = DirectCast _
        (BindingContext(dt.DefaultView), CurrencyManager)
        AddHandler cma.CurrentChanged, AddressOf rowchanging
    End Sub
    Public Sub rowchanging(ByVal sender As Object, _
        ByVal e As EventArgs)
        Dim dv1 = DirectCast(DataGridView1.DataSource, DataView)
        Dim dv2 As New DataView(dv1.Table)
        Dim cma = DirectCast(BindingContext(dv1), CurrencyManager)
        dv2.RowFilter = "Name = '" & dv1(cma.Position)("Name").ToString & "'"
        DataGridView2.DataSource = dv2
    End Sub
End Class

Notify that a Sort Event takes place

When you sort a DataGridView the dataview will fire a listchanged event. To be notified when the grid is sorted add a handler to the dataviews listchanged event. Here is some sample code.

Untitled 1

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object,

                           ByVal e As System.EventArgsHandles MyBase.Load

        Using da As New SqlDataAdapter("Select * From Products",

                                       "Server = YourServerName;Database" & _

                                       " = NorthWind;Integrated Security = SSPI;")

            Dim dt As New DataTable


            DataGridView1.DataSource = dt.DefaultView

            AddHandler dt.DefaultView.ListChanged, AddressOf ListChanged

        End Using

    End Sub


    Private Sub ListChanged(ByVal sender As Object,

                            ByVal e As System.ComponentModel.ListChangedEventArgs)

        Dim dv = DirectCast(DataGridView1.DataSource, DataView)

        MessageBox.Show(String.Format("Sort on column {0}"dv.Sort))

    End Sub

End Class

Sort a string containing only values numeric

To try this sample, drag a datagrid or a datagridview on a form, past this code in and run
Untitled 9
Public Class Form1
 Private Sub Form1_Load(ByVal sender _
       As System.ObjectByVal e As System.EventArgs) _
       Handles MyBase.Load
        'Creating test table
        Dim dt As New DataTable
        For i As Integer = 0 To 100
            dt.Rows(i)(0) = i.ToString
        dt.Columns.Add("integer", _
                    GetType(System.Int32), "string")
        dt.DefaultView.Sort = "integer"
        dt.Columns("integer").ColumnMapping = _
        DataGridView1.DataSource = dt
    End Sub
End Class

DataGridView: A DataGridViewCombobox which value member is showed in a textbox after committing.

DataGridView cells have no events beside disposing. Therfore changed values are only committed after a row change or endedit in the underlying collection (and therefore in the grid)
Here a small sample how to do that, it is a Windows Forms Project with only a DataGridView dragged on the surface of the form.
Be aware the two values are bound to each other, so we cannot for instance add a member Mike Feng from Sjanghai in this sample
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object,
                           ByVal e As System.EventArgsHandles MyBase.Load
        Dim DGVCombo As New DataGridViewComboBoxColumn
        Dim TextBox1 As New DataGridViewTextBoxColumn
        DataGridView1.AutoGenerateColumns = False
        Dim dt As New DataTable("Names")
        dt.LoadDataRow(New Object() {"Florida""Ken Tucker"}, True)
        dt.LoadDataRow(New Object() {"Netherlands""Cor Ligthert"}, True)
        dt.LoadDataRow(New Object() {"England""John Antonny Oliver"}, True)
        dt.LoadDataRow(New Object() {"Germany""Armin Zingler"}, True)
        dt.LoadDataRow(New Object() {"Illinois""Paul Clement"}, True)
        dt.LoadDataRow(New Object() {"SjangHai""Liliane Teng"}, True)
        DataGridView1.DataSource = dt
        DataGridView1.AllowUserToAddRows = False
        Dim dtCombo = dt.Copy
        dtCombo.TableName = "States"
        DGVCombo.DataSource = dtCombo
        DGVCombo.DisplayMember = "Name"
        DGVCombo.ValueMember = "State"
        DGVCombo.DataPropertyName = "State"
        TextBox1.DataPropertyName = "State"
    End Sub
End Class

1 2 3 4 5  ... Previous Next