Search KB Filter article types
DataGridView CodeSample - Faking Password character and loading image in CellFormatting
Categories: Lists & Grids, Non-ORM
Tags: Architects, Developers, Resource Handlers, C#, VB.NET, 1. Beginner, 2. Intermediate, 3. Advanced, v6.3, v6.4 and Later
Revision: 1
Posted: 17/Dec/2009
Updated: 23/July/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This article will demonstrate how you can use CellFormatting event in DataGridView "fake" passwordcharacter, by outputting dummy asterisks in the password columns.

Samples of useVB.NET Code
''' <summary>
''' CellFormatting gets fired for every column of every row, including the 
''' empty row at the bottom, as we allow adding new rows in this sample DataGridView.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As Gizmox.WebGUI.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.RowIndex > DataGridView1.Rows.Count() _
        OrElse e.RowIndex < 0 _
        OrElse DataGridView1.Rows(e.RowIndex) Is Nothing _
        OrElse DataGridView1.Rows(e.RowIndex).DataBoundItem Is Nothing Then
        ' Special handling for the bottom empty row.
        e.Value = Nothing
    ElseIf e.ColumnIndex = 2 AndAlso e.Value IsNot Nothing Then
        ' For our image column, create the resource handle out of the image name.
        e.Value = New ImageResourceHandle(e.Value)
    ElseIf e.ColumnIndex = 1 Then
        ' Faking password character
        e.Value = "*********"
    End If
End Sub

C# Code
// '' <summary>
// '' CellFormatting gets fired for every column of every row, including the 
// '' empty row at the bottom, as we allow adding new rows in this sample DataGridView.
// '' </summary>
// '' <param name="sender"></param>
// '' <param name="e"></param>
// '' <remarks></remarks>
private void DataGridView1_CellFormatting(object sender, Gizmox.WebGUI.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (((e.RowIndex > DataGridView1.Rows.Count )
                || ((e.RowIndex < 0)
                || ((DataGridView1.Rows[e.RowIndex] == null)
                || (DataGridView1.Rows[e.RowIndex].DataBoundItem == null)))))
    {
        //  Special handling for the bottom empty row.
        e.Value = null;
    }
    else if ((e.ColumnIndex == 2) && (e.Value != null))
    {
        //  For our image column, create the resource handle out of the image name.
        e.Value = new ImageResourceHandle((string) ( e.Value ));
    }
    else if ((e.ColumnIndex == 1))
    {
        //  Faking password character
        e.Value = "*********";
    }
}

The demo application

The demo application is a very simple DataGridView to a DataTable with 4 rows of data. One of the columns contains a name of an image, an on CellFormatting we create a ResourceHandle the DataGridViewImageColumn, out of that image name. The image itself is stored on the Resources.

Tips and Tricks

In CellFormatting you need to take special care if you allow adding of new rows to the grid. See the demo for one way to do that.

SDK Version highlights
  • This sample requires version 6.4 or higher for some of the DataGridView to fully work.
RererencesCode samples

About the author

Related Articles

Lists & Grids  
Title Update Author
I played a bit around with the new 6.4 ListView control, its quite amazing what you can do with it. It opens a lot of new ways to present data in a better and more userfriendly way.
Tags: Architects, Developers, Data Binding, C#, 2. Intermediate, 3. Advanced, Customization, Data Binding, v6.4 and Later
02/July/2012    2012/07/02
Tags: Developers, Data Binding, 2. Intermediate, 3. Advanced, Customization, Layouting, Pre v6.3, v6.3, v6.4 and Later
05/Jan/2009    2009/01/05
Tags: Developers, Data Binding, Navigation, 1. Beginner, 2. Intermediate, Data Binding, Navigation, ASP.NET, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
11/Jan/2009    2009/01/11
Tags: Architects, Developers, Data Binding, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
11/Jan/2007    2007/01/11
Tags: Developers, Data Binding, Windows & Dialogs, 1. Beginner, 2. Intermediate, Customization, Data Binding, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
03/Jan/2009    2009/01/03
The DataListView control is an extension to the Visual WebGui ListView. It inherits from the Visual WebGui ListView control and adds DataSource/DataMemeber support to provide a DataGridView behavior in terms of data population.
Tags: Developers, Data Binding, 1. Beginner, 2. Intermediate, 3. Advanced, Data Binding, Pre v6.3
06/Jan/2007    2007/01/06
.NET HTML5 Web, Cloud and Mobile application delivery | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2012 Visual WebGui®       Visual WebGui weblog on ASP.NET Gizmox Blog Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS