Dim Table1 As DataTable
'Table1 = New DataTable("Contacts")
'creating a table named contacts
f1.DataGrid1.ReadOnly = True
'so that no changes are made to the data grid
Dim Row1, Row2, Row3, Row4, Row5 As DataRow
'declaring five rows for the table
Try
Dim Name As DataColumn = New DataColumn("First Name")
'declaring a column named First Name
Name.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
Table1.Columns.Add(Name)
'adding the column to table
Dim Last As DataColumn = New DataColumn("Last Name")
Last.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Last)
Dim Title As DataColumn = New DataColumn("Title")
Title.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Title)
Dim phone As DataColumn = New DataColumn("Phone")
phone.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(phone)
Dim Email As DataColumn = New DataColumn("Email")
Email.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Email)
Row1 = Table1.NewRow()
'declaring a new row
Row1.Item("First Name") = TextBox3.Text
'filling the row with values. Item property is used to set the field value.
Row1.Item("Last Name") = TextBox4.Text
'filling the row with values. adding Last Name
Row1.Item("Title") = TextBox2.Text
'filling the row with values. adding a title
Row1.Item("Phone") = TextBox5.Text
'filling the row with phone number
Row1.Item("Email") = TextBox6.Text
'filling the row with email id
Table1.Rows.Add(Row1)
f1.DataGrid1.Refresh()
'adding the completed row to the table
Catch
End Try
Dim ds As New DataSet
ds = New DataSet
'creating a dataset
ds.Tables.Add(Table1)
'adding the table to dataset
f1.DataGrid1.SetDataBinding(ds, "Contacts")
'binding the table to datagrid
' for resizing the column width of datagrid
'Clear the table styles for the datagrid
f1.DataGrid1.TableStyles.Clear()
'Declare local variables
Dim tsTableStyle As New DataGridTableStyle
Dim tcTextCol As DataGridTextBoxColumn
Dim intCounter As Integer
'Map the table style to the table in the dataset
tsTableStyle.MappingName = ds.Tables(0).TableName()
'Add textbox column style for each column in the dataset
For intCounter = 0 To ds.Tables(0).Columns.Count() - 1
'Reinstantiate the text box column and set mappings and attributes
tcTextCol = New DataGridTextBoxColumn
tcTextCol.MappingName = ds.Tables(0).Columns.Item(intCounter).ColumnName
tcTextCol.HeaderText = ds.Tables(0).Columns.Item(intCounter).ColumnName
'Set column size based on name
Select Case tcTextCol.MappingName()
Case "First Name"
tcTextCol.Width() = 90
Case "Last Name"
tcTextCol.Width() = 90
Case "Title"
tcTextCol.Width() = 40
Case "Phone"
tcTextCol.Width() = 100
Case "Email"
tcTextCol.Width() = 180
Case Else
tcTextCol.Width() = 0
End Select
'Add the column to the table style
tsTableStyle.GridColumnStyles.Add(tcTextCol)
Next
'Add the table styles to the datagrid
f1.DataGrid1.TableStyles.Add(tsTableStyle)
f1.Show()