which Grid to use in vb.net?

M. Zafar Iqbal

New member
Joined
Jan 24, 2007
Messages
2
Programming Experience
Beginner
Hi All
I was working in vb6, and i was using sheridan grid in vb6.
Now I am going to switch to vb.net 2005,
which grid should i use in vb 2005 ?


Thnks
 
You should use the DataGridView control. (Or if you choose, the older .Net 1.0 DataGrid control)
You will in some cases find better use of the ListView control.
 
when i add a button to the DataGridView, it occupy the whole Cell, whereas when i was using Sheridan in vb6. the button was taking a minor space, and also the button wasn't visible when we move away from that Cell.

Should i have the same functionality in DataGridView.
 
The button cell/column in DataGridView fill the whole cell because the buttoncell is the cell. If you want for example a textbox cell and also display a button you can inherit from a DataGridViewTextBoxCell and use the Initialize/Detach overrides for EditingControl and add button there. I tried this and found it best to add/remove the button to the DataGridView instead of the editing control since the textbox doesn't fill the cell in edit mode. Handling the button click event can be done internally in cell class and/or externally by the user of DataGridView. Here is example code for cell class:

VB.NET:
Class tbCell
    Inherits DataGridViewTextBoxCell
 
    Private WithEvents btn As Button
    Public Event ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, _
                                                  ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
        btn = New Button
        btn.Text = "Z"
        btn.Font = DataGridView.EditingControl.Font
        Dim rct As Rectangle = DataGridView.GetCellDisplayRectangle(Me.ColumnIndex, Me.RowIndex, True)
        Dim pt As Point = rct.Location
        pt.X += rct.Width - btn.Width
        btn.Size = New Size(20, rct.Height)
        btn.Location = pt
        DataGridView.Controls.Add(btn)
        btn.BringToFront()
    End Sub
 
    Public Overrides Sub DetachEditingControl()
        MyBase.DetachEditingControl()
        DataGridView.Controls.Remove(btn)
        btn = Nothing
    End Sub
 
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btn.Click
        MsgBox("clicked button")
        RaiseEvent ButtonClick(DataGridView, e)
    End Sub
End Class
example usage:
VB.NET:
DataGridView1.Columns.Add(New DataGridViewColumn(New tbCell))
DataGridView1.RowCount = 5
For Each row As DataGridViewRow In DataGridView1.Rows
    row.Cells(0).Value = row.Index.ToString
Next row
The cells ButtonClicked event can also be arranged to be subscribed by a custom column class that expose this instead of having to subscribe it for all cells.

Another way to do it that makes it easier to handle the event from 'outside' is to use the DataGridViews CellBeginEdit and CellEndEdit events to manipulate a Button control to display similar as done above. You then only got one button and event to handle and can relate this to CurrentCell at all times.
 
Here's the example code for the "other way" just mentioned, using a single Button control for all edit cells and just use the events to show/hide and place it. This is easier and beneficial for external code and events.
VB.NET:
Sub initializeDGVandButton()
    DataGridView1.RowCount = 5
    DataGridView1.ColumnCount = 2
    btn.Visible = False
    btn.Text = "Z"
    btn.Font = DataGridView1.Font
    DataGridView1.Controls.Add(btn)
End Sub
 
Private WithEvents btn As New Button
 
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) _
Handles DataGridView1.CellBeginEdit
    Dim rct As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
    btn.Size = New Size(20, rct.Height)
    Dim pt As Point = rct.Location
    pt.X += rct.Width - btn.Width
    btn.Location = pt
    btn.Visible = True
End Sub
 
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellEndEdit
    btn.Visible = False
End Sub
 
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles btn.Click
    Dim fmt As String = "cell edit button clicked, columnindex {0} rowindex {1}"
    MsgBox(String.Format(fmt, DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex))
End Sub
 
Back
Top