DataGridView CodeSample - Faking Password character and loading image in CellFormatting

From Visual WebGui Wiki

Jump to: navigation, search


Contents

Overview

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

Samples of use

VB.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 bound 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 for the DataGridViewImageColumn, out of that image name. The image itself is stored on the Resources folder.

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

Rererences

Code samples

Personal tools