Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Visual Basic
Article

Editable and Multi-Functional Datagridview

Rate me:
Please Sign up or sign in to vote.
4.56/5 (38 votes)
22 May 2008CPOL2 min read 168.3K   9.3K   229   14
The article or rather a code snippet demonstrates a simple application of insert, update, delete using datagridview. There are some unique features in this datagridview.

Introduction

The article or rather a code snippet demonstrates a simple application of insert, update, delete using datagridview. Here, sample data is used and one can customize this gridview according to her or his need. There are some unique features in this datagridview like:

  • Adding columns at runtime
  • Changing the values of a combobox on any event like selecting the specified values from another combobox
  • Forcing the user to type only the numeric values in the datagridview textbox, if Integer is selected in corresponding combobox
  • DateTime picker control in the datagridview
  • Enabling-Disabling the cells on a particular event
  • Setting the tooltip on the cells
  • Changing the cell style at runtime

Background

When I was working on the datagridview while developing a Windows application, I came across the problems about the Datagridview that it doesn't support many functionalities directly, rather one has to play with its events. Like catching the keypress event of a combobox column in gridview is not so simple, but with using the right event, one can do that. So I am presenting this editable and multi-functional grid which can be customized according to one's need.

Using the Multi-Functional Gridview

Note: This gridview is developed and tested in Visual Studio 2005. It may not work with older versions or any non-Microsoft browser.

This sample code can help those who wish to use the unbound gridview in their Windows application. In this code, all the tricky events are handled with the demo data. The user has to just change the demo data with her or his original data. In this code, the user will find all the basic features which one should require in a gridview like:

  • Adding columns at runtime
  • Changing the values of a combobox on any event like selecting the specified values from another combobox
  • Forcing the user to type only the numeric values in the datagridview textbox, if Integer is selected in corresponding combobox
  • DateTime picker control in the datagridview
  • Enabling-Disabling the cells on a particular event
  • Setting the tooltip on the cells
  • Changing the cell style at runtime

Using this code is simple. Open the solution file 'Editable Grid' and just copy-paste the code blocks of functions or events renaming the control names for your own, and it will work for you. You must have the Project Folder 'Editable Grid' for opening the solution file.

Here are some code blocks I'll explain.

This code in a function adds columns at runtime:

VB.NET
colBtnDelete.Text = "Delete"
colBtnDelete.HeaderText = "Delete"
colBtnDelete.Name = "Delete"
colBtnDelete.Width = 100
colBtnDelete.UseColumnTextForButtonValue = True
EditableGrid.Columns.Add(colBtnDelete)

This function adds the value into the gridview combobox through the list:

VB.NET
Private arrOptionColumn1 As New List(Of String)
Dim cmbOpt1 As DataGridViewComboBoxColumn = CType(EditableGrid.Columns"Opt1"), _
    DataGridViewComboBoxColumn)
With arrOptionColumn1
.Add("Opt1")
.Add("Opt2")
.Add("Opt3")
.Add("Opt4")
End With
cmbOpt1.DataSource = arrOptionColumn1

This function changes the values in the cells when any particular event is fired:

VB.NET
Private Sub OptionsChanged(ByVal lngRowIndex As Integer)

Dim cellOptColumn2 As DataGridViewComboBoxCell =
CType(EditableGrid.Rows(lngRowIndex).Cells("Opt2"), DataGridViewComboBoxCell)
    
Try
With EditableGrid
If (.Rows.Count > 1) Then
If (.Item("Opt1", lngRowIndex).Value.ToString = "Opt1") Then
cellOptColumn2.Dispose()
cellOptColumn2.DataSource = arrOptionColumn2
ElseIf (.Item("Opt1", lngRowIndex).Value.ToString = "Opt2") Then
cellOptColumn2.Dispose()
cellOptColumn2.DataSource = arrOptionColumn3
End If
End If
End With
Catch ex As Exception
End Try

End Sub

Through this event of datagridview, we can enable-disable cells on any action performed on the datagridview:

VB.NET
Private Sub EditableGrid_CellValueChanged(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
EditableGrid.CellValueChanged

Try
With EditableGrid
If (.Rows.Count > 1) Then
If (e.ColumnIndex = 2) Then
If (.Rows(e.RowIndex).Cells("CheckBox1").Value.ToString() = "True") Then
.Rows(e.RowIndex).Cells(4).ReadOnly = True
.Rows(e.RowIndex).Cells(4).Style.BackColor = Color.FromName("Control")
Else
.Rows(e.RowIndex).Cells(4).ReadOnly = False
.Rows(e.RowIndex).Cells(4).Style.BackColor = Color.White
End If
End If
End If
End With
Catch ex As Exception
End Try
End Sub

This function is very important, as it casts the DatagridviewTextBox into a normal textbox to catch its key press and key down events:

VB.NET
Private Sub EditableGrid_EditingControlShowing(ByVal sender As Object, ByVal e
    As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles
    EditableGrid.EditingControlShowing

Try If (e.Control.GetType().BaseType().Name = "TextBox") Then
mobjInnerTextBox = CType(e.Control, TextBox)
End If
Catch ex As Exception

End Try

End Sub

This function sets the tooltip for each cell of the datagridview:

VB.NET
Private Sub SetToolTip(ByVal lngRowindex As Integer, _
    ByVal lngColumnIndex As Integer)
Try With EditableGrid
Select Case lngColumnIndex

Case 0
.Rows(lngRowindex).Cells(0).ToolTipText = "Please Select a value, Combo2 values
will be changed accordingly"

Case 1
.Rows(lngRowindex).Cells(1).ToolTipText = "Values change according to first
combo"

Case 2
.Rows(lngRowindex).Cells(2).ToolTipText = "Text Box 2 will get disable on
checking this CheckBox"

Case 3
.Rows(lngRowindex).Cells(3).ToolTipText = "Only Integers can be entered if
Combo2 has Integer type selected"

Case 4
.Rows(lngRowindex).Cells(4).ToolTipText = "This will be disabled if CheckBox is
checked"

Case 5
.Rows(lngRowindex).Cells(5).ToolTipText = "Select a Date, default will be the
current date"

Case 6
.Rows(lngRowindex).Cells(6).ToolTipText = "This will delete the whole Row"

End Select
End With
Catch ex As Exception
End Try
End Sub

History

  • 23rd May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Tata Consultancy Services
India India
Dear Friends,I'm from Lucknow and currently working with TCS, Lucknow.I'm having almost 6 years of exp in software field and have worked on areas like ASP.NET, C#,VB.Net,Web services, Windows Services. I'm Micrsoft certified web developer. If you like any of my articles or you want to suggest some changes and improve the way I code, write articles feel free to mail me at: virampandey@gmail.com

Comments and Discussions

 
Question[My vote of 2] Oh no ... this is VB Pin
Yves29-Mar-17 7:29
Yves29-Mar-17 7:29 
GeneralMy vote of 5 Pin
JOE Heart Under Blade13-Aug-13 21:27
JOE Heart Under Blade13-Aug-13 21:27 
GeneralMy vote of 5 Pin
_Vitor Garcia_21-Mar-13 6:49
_Vitor Garcia_21-Mar-13 6:49 
Thank you
GeneralMy vote of 5 Pin
Ahmad_kelany8-May-12 10:30
Ahmad_kelany8-May-12 10:30 
GeneralMy vote of 3 Pin
Dave Kreskowiak1-Apr-11 3:13
mveDave Kreskowiak1-Apr-11 3:13 
GeneralAdding rows to gridview or edit row in gridview but not update the source of the gridview until save button click. Pin
iaminterested28-May-10 7:44
iaminterested28-May-10 7:44 
Generalc# code Pin
nabila.arfeen1-Jan-10 20:53
nabila.arfeen1-Jan-10 20:53 
GeneralGreat Work Sir... Pin
B0nCelAja30-Sep-09 0:03
B0nCelAja30-Sep-09 0:03 
GeneralC# Code Pin
ArpitNagar11-Aug-09 1:56
ArpitNagar11-Aug-09 1:56 
GeneralNice work Pin
Thiago Tozim5-Sep-08 7:31
Thiago Tozim5-Sep-08 7:31 
Questionnested grid Pin
DailyCoding3-Jul-08 3:01
DailyCoding3-Jul-08 3:01 
GeneralNice approach.. Pin
Visheshpd25-May-08 23:34
Visheshpd25-May-08 23:34 
GeneralVery useful article Pin
Vivek_Bhat25-May-08 22:53
Vivek_Bhat25-May-08 22:53 
General[Message Removed] Pin
Mojtaba Vali25-May-08 0:49
Mojtaba Vali25-May-08 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.