Home All Groups Group Topic Archive Search About

Datagrid 's TextBox input format question.

Author
12 Apr 2005 10:30 AM
Agnes
In my Textbox, I can set input mask by using keypressevent.
E.g If Me.txtID.length = 3  then Me.txtId.appendtext("-") [ I use
usercontrol to do. So I only modify my usercontrol and then can apply all
the textboxes. ]

Now, in DataGridcolumns' textbox, How can I do the simliar approach ? Thanks

Author
12 Apr 2005 1:24 PM
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

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

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:%23aw%23iq0PFHA.3076@TK2MSFTNGP12.phx.gbl...
In my Textbox, I can set input mask by using keypressevent.
E.g If Me.txtID.length = 3  then Me.txtId.appendtext("-") [ I use
usercontrol to do. So I only modify my usercontrol and then can apply all
the textboxes. ]

Now, in DataGridcolumns' textbox, How can I do the simliar approach ? Thanks