Home All Groups Group Topic Archive Search About
Author
5 Apr 2005 9:06 AM
Starbuck
Hi

Using VB.Net.
We have a datagrid which users can type data directly into, all seems to
work fine but what we want is when a user changes a numeric value in column
3 or 4 (via keypress) we want to call a routine that will change some data
on the screen.
What is the best way of catching the keypress and column number please.

Thanks

Author
5 Apr 2005 11:22 AM
Ken Tucker [MVP]
Hi,

        Add a handler to the datagrid textbox columns key press event.  Here
is an example.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim ds As New DataSet

Dim daCustomers As OleDbDataAdapter

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn += "Data Source = C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daCustomers = New OleDbDataAdapter("Select * from Customers", conn)

daCustomers.Fill(ds, "Customers")

SetupGrid()



DataGrid1.DataSource = ds.Tables("Customers")

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Customers"

Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ContactName"

..HeaderText = "Name"

..Width = 150

End With

AddHandler colName.TextBox.KeyPress, AddressOf ColumnKeyPress

Dim colID As New DataGridTextBoxColumn

With colID

..MappingName = "CustomerID"

..HeaderText = "ID"

..Width = 80

End With

Dim colRegion As New DataGridTextBoxColumn

With colRegion

..MappingName = "Region"

..HeaderText = "Region"

..Width = 80

..NullText = ""

End With

ts.GridColumnStyles.Add(colID)

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colRegion)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colRegion = Nothing

colName = Nothing

colID = Nothing

End Sub







Private Sub ColumnKeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)

Trace.WriteLine(String.Format("You Press the {0} key", e.KeyChar))

End Sub



Ken

--------------------------------

"Starbuck" <k**@kevsbox.com> wrote in message
news:%23SChJ7bOFHA.3076@TK2MSFTNGP12.phx.gbl...
Hi

Using VB.Net.
We have a datagrid which users can type data directly into, all seems to
work fine but what we want is when a user changes a numeric value in column
3 or 4 (via keypress) we want to call a routine that will change some data
on the screen.
What is the best way of catching the keypress and column number please.

Thanks
Author
5 Apr 2005 1:20 PM
Starbuck
Thanks Ken

As always you are the man

Starbuck

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:e4QEWHdOFHA.2568@TK2MSFTNGP14.phx.gbl...
> Hi,
>
>        Add a handler to the datagrid textbox columns key press event.
> Here
> is an example.
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> Dim conn As OleDbConnection
>
> Dim strConn As String
>
> Dim strSQL As String
>
> Dim ds As New DataSet
>
> Dim daCustomers As OleDbDataAdapter
>
> strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
>
> strConn += "Data Source = C:\Northwind.mdb;"
>
> conn = New OleDbConnection(strConn)
>
> daCustomers = New OleDbDataAdapter("Select * from Customers", conn)
>
> daCustomers.Fill(ds, "Customers")
>
> SetupGrid()
>
>
>
> DataGrid1.DataSource = ds.Tables("Customers")
>
> End Sub
>
> Private Sub SetupGrid()
>
> Dim ts As New DataGridTableStyle
>
> ts.MappingName = "Customers"
>
> Dim colName As New DataGridTextBoxColumn
>
> With colName
>
> .MappingName = "ContactName"
>
> .HeaderText = "Name"
>
> .Width = 150
>
> End With
>
> AddHandler colName.TextBox.KeyPress, AddressOf ColumnKeyPress
>
> Dim colID As New DataGridTextBoxColumn
>
> With colID
>
> .MappingName = "CustomerID"
>
> .HeaderText = "ID"
>
> .Width = 80
>
> End With
>
> Dim colRegion As New DataGridTextBoxColumn
>
> With colRegion
>
> .MappingName = "Region"
>
> .HeaderText = "Region"
>
> .Width = 80
>
> .NullText = ""
>
> End With
>
> ts.GridColumnStyles.Add(colID)
>
> ts.GridColumnStyles.Add(colName)
>
> ts.GridColumnStyles.Add(colRegion)
>
> DataGrid1.TableStyles.Add(ts)
>
> ts = Nothing
>
> colRegion = Nothing
>
> colName = Nothing
>
> colID = Nothing
>
> End Sub
>
>
>
>
>
>
>
> Private Sub ColumnKeyPress(ByVal sender As System.Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs)
>
> Trace.WriteLine(String.Format("You Press the {0} key", e.KeyChar))
>
> End Sub
>
>
>
> Ken
>
> --------------------------------
>
> "Starbuck" <k**@kevsbox.com> wrote in message
> news:%23SChJ7bOFHA.3076@TK2MSFTNGP12.phx.gbl...
> Hi
>
> Using VB.Net.
> We have a datagrid which users can type data directly into, all seems to
> work fine but what we want is when a user changes a numeric value in
> column
> 3 or 4 (via keypress) we want to call a routine that will change some data
> on the screen.
> What is the best way of catching the keypress and column number please.
>
> Thanks
>
>
>