I am a beginner in the visual basic language and I have been given an assignment to design a small file editor application using an unbound DataGridView control.I am not familiar with how to go about this task as i have never used this control before. However I did check the help in VB IDE and I copied some code samples which i am finding it hard to implement for my project. code sample below
Imports System
Imports System.Drawing
Imports System.Windows. Forms
Public Class Form1
Inherits System.Windows. Forms.Form
Private buttonPanel As New Panel
Private WithEvents songsDataGridVi ew As New DataGridView
Private WithEvents addNewRowButton As New Button
Private WithEvents deleteRowButton As New Button
<br /><span space="preserve ">...</span><br />
<STAThreadAttri bute()> _
Public Shared Sub Main()
Application.Ena bleVisualStyles ()
Application.Run (New Form1())
End Sub
End Class
Private Sub SetupLayout()
Me.Size = New Size(600, 500)
With addNewRowButton
.Text = "Add Row"
.Location = New Point(10, 10)
End With
With deleteRowButton
.Text = "Delete Row"
.Location = New Point(100, 10)
End With
With buttonPanel
.Controls.Add(a ddNewRowButton)
.Controls.Add(d eleteRowButton)
.Height = 50
.Dock = DockStyle.Botto m
End With
Me.Controls.Add (Me.buttonPanel )
End Sub
Private Sub SetupDataGridVi ew()
Me.Controls.Add (songsDataGridV iew)
songsDataGridVi ew.ColumnCount = 5
With songsDataGridVi ew.ColumnHeader sDefaultCellSty le
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataG ridView.Font, FontStyle.Bold)
End With
With songsDataGridVi ew
.Name = "songsDataGridV iew"
.Location = New Point(8, 8)
.Size = New Size(500, 250)
.AutoSizeRowsMo de = _
DataGridViewAut oSizeRowsMode.D isplayedCellsEx ceptHeaders
.ColumnHeadersB orderStyle = DataGridViewHea derBorderStyle. Single
.CellBorderStyl e = DataGridViewCel lBorderStyle.Si ngle
.GridColor = Color.Black
.RowHeadersVisi ble = False
.Columns(0).Nam e = "Release Date"
.Columns(1).Nam e = "Track"
.Columns(2).Nam e = "Title"
.Columns(3).Nam e = "Artist"
.Columns(4).Nam e = "Album"
.Columns(4).Def aultCellStyle.F ont = _
New Font(Me.songsDa taGridView.Defa ultCellStyle.Fo nt, FontStyle.Itali c)
.SelectionMode = DataGridViewSel ectionMode.Full RowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
End With
End Sub
Private Sub PopulateDataGri dView()
Dim row0 As String() = {"11/22/1968", "29", "Revolution 9", _
"Beatles", "The Beatles [White Album]"}
Dim row1 As String() = {"1960", "6", "Fools Rush In", _
"Frank Sinatra", "Nice 'N' Easy"}
Dim row2 As String() = {"11/11/1971", "1", "One of These Days", _
"Pink Floyd", "Meddle"}
Dim row3 As String() = {"1988", "7", "Where Is My Mind?", _
"Pixies", "Surfer Rosa"}
Dim row4 As String() = {"5/1981", "9", "Can't Find My Mind", _
"Cramps", "Psychedeli c Jungle"}
Dim row5 As String() = {"6/10/2003", "13", _
"Scatterbra in. (As Dead As Leaves.)", _
"Radiohead" , "Hail to the Thief"}
Dim row6 As String() = {"6/30/1992", "3", "Dress", "P J Harvey", "Dry"}
With Me.songsDataGri dView.Rows
.Add(row0)
.Add(row1)
.Add(row2)
.Add(row3)
.Add(row4)
.Add(row5)
.Add(row6)
End With
With Me.songsDataGri dView
.Columns(0).Dis playIndex = 3
.Columns(1).Dis playIndex = 4
.Columns(2).Dis playIndex = 0
.Columns(3).Dis playIndex = 1
.Columns(4).Dis playIndex = 2
End With
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
SetupLayout()
SetupDataGridVi ew()
PopulateDataGri dView()
End Sub
Private Sub songsDataGridVi ew_CellFormatti ng(ByVal sender As Object, _
ByVal e As System.Windows. Forms.DataGridV iewCellFormatti ngEventArgs) _
Handles songsDataGridVi ew.CellFormatti ng
If Me.songsDataGri dView.Columns(e .ColumnIndex).N ame = _
"Release Date" Then
If e IsNot Nothing Then
If e.Value IsNot Nothing Then
Try
e.Value = DateTime.Parse( e.Value.ToStrin g()) _
.ToLongDateStri ng()
e.FormattingApp lied = True
Catch ex As FormatException
Console.WriteLi ne("{0} is not a valid date.", e.Value.ToStrin g())
End Try
End If
End If
End If
End Sub
Private Sub addNewRowButton _Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles addNewRowButton .Click
Me.songsDataGri dView.Rows.Add( )
End Sub
Private Sub deleteRowButton _Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles deleteRowButton .Click
If Me.songsDataGri dView.SelectedR ows.Count > 0 AndAlso _
Not Me.songsDataGri dView.SelectedR ows(0).Index = _
Me.songsDataGri dView.Rows.Coun t - 1 Then
Me.songsDataGri dView.Rows.Remo veAt( _
Me.songsDataGri dView.SelectedR ows(0).Index)
End If
End Sub
Imports System
Imports System.Drawing
Imports System.Windows. Forms
Public Class Form1
Inherits System.Windows. Forms.Form
Private buttonPanel As New Panel
Private WithEvents songsDataGridVi ew As New DataGridView
Private WithEvents addNewRowButton As New Button
Private WithEvents deleteRowButton As New Button
<br /><span space="preserve ">...</span><br />
<STAThreadAttri bute()> _
Public Shared Sub Main()
Application.Ena bleVisualStyles ()
Application.Run (New Form1())
End Sub
End Class
Private Sub SetupLayout()
Me.Size = New Size(600, 500)
With addNewRowButton
.Text = "Add Row"
.Location = New Point(10, 10)
End With
With deleteRowButton
.Text = "Delete Row"
.Location = New Point(100, 10)
End With
With buttonPanel
.Controls.Add(a ddNewRowButton)
.Controls.Add(d eleteRowButton)
.Height = 50
.Dock = DockStyle.Botto m
End With
Me.Controls.Add (Me.buttonPanel )
End Sub
Private Sub SetupDataGridVi ew()
Me.Controls.Add (songsDataGridV iew)
songsDataGridVi ew.ColumnCount = 5
With songsDataGridVi ew.ColumnHeader sDefaultCellSty le
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataG ridView.Font, FontStyle.Bold)
End With
With songsDataGridVi ew
.Name = "songsDataGridV iew"
.Location = New Point(8, 8)
.Size = New Size(500, 250)
.AutoSizeRowsMo de = _
DataGridViewAut oSizeRowsMode.D isplayedCellsEx ceptHeaders
.ColumnHeadersB orderStyle = DataGridViewHea derBorderStyle. Single
.CellBorderStyl e = DataGridViewCel lBorderStyle.Si ngle
.GridColor = Color.Black
.RowHeadersVisi ble = False
.Columns(0).Nam e = "Release Date"
.Columns(1).Nam e = "Track"
.Columns(2).Nam e = "Title"
.Columns(3).Nam e = "Artist"
.Columns(4).Nam e = "Album"
.Columns(4).Def aultCellStyle.F ont = _
New Font(Me.songsDa taGridView.Defa ultCellStyle.Fo nt, FontStyle.Itali c)
.SelectionMode = DataGridViewSel ectionMode.Full RowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
End With
End Sub
Private Sub PopulateDataGri dView()
Dim row0 As String() = {"11/22/1968", "29", "Revolution 9", _
"Beatles", "The Beatles [White Album]"}
Dim row1 As String() = {"1960", "6", "Fools Rush In", _
"Frank Sinatra", "Nice 'N' Easy"}
Dim row2 As String() = {"11/11/1971", "1", "One of These Days", _
"Pink Floyd", "Meddle"}
Dim row3 As String() = {"1988", "7", "Where Is My Mind?", _
"Pixies", "Surfer Rosa"}
Dim row4 As String() = {"5/1981", "9", "Can't Find My Mind", _
"Cramps", "Psychedeli c Jungle"}
Dim row5 As String() = {"6/10/2003", "13", _
"Scatterbra in. (As Dead As Leaves.)", _
"Radiohead" , "Hail to the Thief"}
Dim row6 As String() = {"6/30/1992", "3", "Dress", "P J Harvey", "Dry"}
With Me.songsDataGri dView.Rows
.Add(row0)
.Add(row1)
.Add(row2)
.Add(row3)
.Add(row4)
.Add(row5)
.Add(row6)
End With
With Me.songsDataGri dView
.Columns(0).Dis playIndex = 3
.Columns(1).Dis playIndex = 4
.Columns(2).Dis playIndex = 0
.Columns(3).Dis playIndex = 1
.Columns(4).Dis playIndex = 2
End With
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
SetupLayout()
SetupDataGridVi ew()
PopulateDataGri dView()
End Sub
Private Sub songsDataGridVi ew_CellFormatti ng(ByVal sender As Object, _
ByVal e As System.Windows. Forms.DataGridV iewCellFormatti ngEventArgs) _
Handles songsDataGridVi ew.CellFormatti ng
If Me.songsDataGri dView.Columns(e .ColumnIndex).N ame = _
"Release Date" Then
If e IsNot Nothing Then
If e.Value IsNot Nothing Then
Try
e.Value = DateTime.Parse( e.Value.ToStrin g()) _
.ToLongDateStri ng()
e.FormattingApp lied = True
Catch ex As FormatException
Console.WriteLi ne("{0} is not a valid date.", e.Value.ToStrin g())
End Try
End If
End If
End If
End Sub
Private Sub addNewRowButton _Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles addNewRowButton .Click
Me.songsDataGri dView.Rows.Add( )
End Sub
Private Sub deleteRowButton _Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles deleteRowButton .Click
If Me.songsDataGri dView.SelectedR ows.Count > 0 AndAlso _
Not Me.songsDataGri dView.SelectedR ows(0).Index = _
Me.songsDataGri dView.Rows.Coun t - 1 Then
Me.songsDataGri dView.Rows.Remo veAt( _
Me.songsDataGri dView.SelectedR ows(0).Index)
End If
End Sub
Comment