Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two DataGridView questions 1

Status
Not open for further replies.

kavsagol

Programmer
Nov 13, 2006
95
IL
I have a DataGridView, with a DataTable defined as its data source, in a Windows application built in VS 2005.

1. How can I make a specific column display ***** instead of its text (a password column)? I tried setting the format of column, but nothing works for me, since I have more experience with VC than .Net format options. The column is not read only and I prefer that the text will be 'hidden' when it is enterred as well as displayed.

2. I want to add a delete button, but I do not want to add it as a column, but as a button at the buttom of the form. I know I can access the DataGridView CurrentRow object, but how can I delete it?
 
For item 1, you can access the editing control for the column and change the password character:
Code:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

        CType(e.Control, DataGridViewTextBoxEditingControl).PasswordChar = Convert.ToChar(IIf(DataGridView1.CurrentCell.ColumnIndex = 0, "*", Nothing))

    End Sub
However, this only masks the text when being edited. You also need to format the cell so that when focus moves to another cell the password is not displayed in plain text:
Code:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

        If e.ColumnIndex = 0 AndAlso Not e.Value Is Nothing Then e.Value = "".PadLeft(e.Value.ToString.Length, Convert.ToChar("*"))

    End Sub
Note that the code above assumes column 0 is the password column.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top