Click here to Skip to main content
Click here to Skip to main content
Go to top

Editable and Multi-Functional Datagridview

, 22 May 2008
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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:

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:

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:

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)

Share

About the Author

Viram
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

 
GeneralMy vote of 5 PinmemberJOE MIS13-Aug-13 21:27 
GeneralMy vote of 5 PinmemberVitorHugoGarcia21-Mar-13 6:49 
GeneralMy vote of 5 Pinmemberabosohayla ahmad kelany8-May-12 10:30 
GeneralMy vote of 3 PinmvpDave 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. Pinmemberiaminterested28-May-10 7:44 
Generalc# code Pinmembernabila.arfeen1-Jan-10 20:53 
GeneralGreat Work Sir... PinmemberB0nCelAja30-Sep-09 0:03 
GeneralC# Code Pinmemberarpitgold11-Aug-09 1:56 
GeneralNice work PinmemberThiago Tozim5-Sep-08 7:31 
Questionnested grid PinmemberDailyCoding3-Jul-08 3:01 
GeneralNice approach.. PinmemberVisheshpd25-May-08 23:34 
GeneralVery useful article PinmemberVivek_Bhat25-May-08 22:53 
General[Message Removed] PinmemberMojtaba Vali25-May-08 0:49 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 22 May 2008
Article Copyright 2008 by Viram
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid