Search
VB
-
Tips
Visual Basic .Net tips and tricks
1
2
3
4
5
...
Previous
Next
DataGrid: Validate data entered into textboxcolumn
To validate the text entered into a datagrid add a handler to the DatagridTextboxColumn's validating event. Here is some sample code.
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As SqlConnection
Dim strConn As String
Dim strSQL As String
Dim da As SqlDataAdapter
strConn = "Server = (local);"
strConn &= "Database = NorthWind;"
strConn &= "Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * From Products", conn)
da.Fill(ds, "Products")
Dim ts As New DataGridTableStyle
ts.MappingName = ds.Tables("Products").TableName
Dim colDiscontinued As New DataGridBoolColumn
With colDiscontinued
.MappingName = "Discontinued"
.HeaderText = "Discontinued"
.Width = 80
End With
Dim colName As New DataGridTextBoxColumn
With colName
.MappingName = "ProductName"
.HeaderText = "Product Name"
.Width = 180
End With
AddHandler colName.TextBox.Validating, AddressOf CellValidating
ts.GridColumnStyles.Add(colName)
ts.GridColumnStyles.Add(colDiscontinued)
DataGrid1.TableStyles.Add(ts)
ts = Nothing
colDiscontinued = Nothing
colName = Nothing
DataGrid1.DataSource = ds.Tables("Products")
End Sub
Private Sub CellValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Debug.WriteLine(DirectCast(sender, DataGridTextBox).Text)
End Sub
DataGrid: Raise Event When User Presses Delete Key using OS messages
To be notified when the user presses the delete key to delete a row in a datagrid you have to create a new datagrid. You must override the PreprocessMessage and the ProcessDialogKey events in the datagrid. Here is a sample class that inherits from the datagrid that raises an event when a user deletes a row.
Public Class ConfirmDeleteDataGrid
Inherits DataGrid
Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)
Private Const WM_KEYDOWN = &H100
Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)
If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then
If MessageBox.Show("Delete This Row?", "Confirm Delete", _
MessageBoxButtons.YesNo) = DialogResult.No Then
Return True
Else
RaiseEvent DeletedRow(Me, New EventArgs)
End If
End If
Return MyBase.PreProcessMessage(msg)
End Function
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim pt As Point
Dim hti As DataGrid.HitTestInfo
pt = Me.PointToClient(Cursor.Position)
hti = Me.HitTest(pt)
If keyData = Keys.Delete Then
If hti.Type = Me.HitTestType.RowHeader Then
If MessageBox.Show("Delete this row?", "Confirm Delete", _
MessageBoxButtons.YesNo) = DialogResult.No Then
Return True
Else
RaiseEvent DeletedRow(Me, New EventArgs)
End If
End If
End If
Return MyBase.ProcessDialogKey(keyData)
End Function
End Class
DataGrid: Alert by a delete of rows using change row
Datagrid Alert by delete of rows
AddHandler ds.Tables(0).RowDeleted, _
New DataRowChangeEventHandler(AddressOf Row_Delete)
Private Sub Row_Delete(ByVal sender As Object, ByVal e _
As DataRowChangeEventArgs)
If e.Action = DataRowAction.Delete Then
If MessageBox.Show("Delete row? ", _
"", MessageBoxButtons.OKCancel) = DialogResult.Cancel Then
e.Row.RejectChanges()
End If
End If
End Sub
DataGrid: Prevent Row And Column Resize
To prevent the user from resizing the rows or columns of a datagrid you need to create a new datagrid. To create a new grid create a class that inherits from datagrid. To prevent the resize prevent the OnMouseDown and OnMouseMove messages from firing when the user is at the resize spot.
Public Class NoResizeGrid
Inherits DataGrid
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))
If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type = DataGrid.HitTestType.RowResize Then
Return 'no baseclass call
End If
MyBase.OnMouseDown(e)
End Sub
Public Sub New()
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))
If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type = DataGrid.HitTestType.RowResize Then
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
End Class
DataGrid: Detect changed row
Datagrid dectect changed row
This sample shows how to use the currencymanager with a datagrid.
This sample needs only a form with two long datagrids on it the one above the other.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("Name")
dt.Columns.Add("Place")
dt.LoadDataRow(New Object() {"Cor", "Holland"}, True)
dt.LoadDataRow(New Object() {"Ken", "Florida"}, True)
dt.LoadDataRow(New Object() {"Jay", "New York"}, True)
dt.LoadDataRow(New Object() {"Herfried", "Austria"}, True)
dt.LoadDataRow(New Object() {"Armin", "Germany"}, True)
dt.LoadDataRow(New Object() {"Terry", "UK"}, True)
DataGrid1.DataSource = dt.DefaultView
dt.DefaultView.AllowDelete = False
dt.DefaultView.AllowEdit = False
dt.DefaultView.AllowNew = False
Dim cma As CurrencyManager = DirectCast _
(BindingContext(dt.DefaultView), CurrencyManager)
AddHandler cma.CurrentChanged, AddressOf rowchanging
rowchanging(Me, Nothing)
End Sub
Public Sub rowchanging(ByVal sender As Object, _
ByVal e As EventArgs)
Dim dv1 As DataView = DirectCast(DataGrid1.DataSource, DataView)
Dim dv2 As New DataView(DirectCast(DataGrid1.DataSource, DataView).Table)
Dim cma As CurrencyManager = DirectCast(BindingContext(dv1), CurrencyManager)
dv2.RowFilter = "Name = '" & dv1(cma.Position)("Name").ToString & "'"
DataGrid2.DataSource = dv2
End Sub
1
2
3
4
5
...
Previous
Next
Syndication
Rss feed
Select a Category
General Grid Tips
DataGrid Tips
DataGridView Tips
Windows Forms Controls
General Ado.Net Tips
SqlClient Ado.Net Tips
OleDB Ado.Net Tips
Asp.Net Tips
Miscellanous Tips
Vista programming Tips
WPF Tips
Silverlight
Articles
Links
Code Camp
FAQs
VB Related Websites
Microsoft Visual Basic Home Page
Visual Basic .Net Wikipedia Page
Visual Basic Forums
Visual Basic Newsgroup
VB City
Thinq Linq
VB Helper
I love VB