Kamis, 02 Desember 2010

DataGridView adding rows and columns in VB.NET

The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control is used to display data from a variety of external data sources. Alternatively, you can add rows and columns to the control and manually populate it with data. The following vb.net source code shows how to manually create Columns and Rows in a DataGridView.

DataGridView1.Columns(Index).Name = "Column Name"

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataGridView1.ColumnCount = 3
DataGridView1.Columns(0).Name = "Product ID"
DataGridView1.Columns(1).Name = "Product Name"
DataGridView1.Columns(2).Name = "Product_Price"

Dim row As String() = New String() {"1", "Product 1", "1000"}
DataGridView1.Rows.Add(row)
row = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Add(row)
row = New String() {"3", "Product 3", "3000"}
DataGridView1.Rows.Add(row)
row = New String() {"4", "Product 4", "4000"}
DataGridView1.Rows.Add(row)
End Sub
End Class

Jumat, 29 Oktober 2010

Delete row in DataGridView

we will discuss how to delete a row in DataGridView. its very easy way of typing the following syntax:
Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
  Dim id As String = e.Row.Cells("ISBN").FormattedValue.ToString() 'Taking value to the ISBN field
  Dim name As String = e.Row.Cells("Title").FormattedValue.ToString()
'Displays a dialog windows
Dim result As DialogResult = MessageBox.Show("Are you sure you want to delete ISBN " & id & " - " & name & "?", "Delete?", MessageBoxButtons.OKCancel)
'If the Cancel button that is selected then the process will be deleted.
If result = DialogResult.Cancel Then
e.Cancel = True
End If