Hi nid@patel,
Check this example. Now please take its reference and correct your code.
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindDataGrid();
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void BindDataGrid()
{
System.Data.DataTable dt = System.Data.new DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn("Name"), new System.Data.DataColumn("Password") });
dt.Rows.Add("John Hammond", "password123");
dt.Rows.Add("Mudassar Khan", "123");
dt.Rows.Add("Suzanne Mathews", "78963");
dt.Rows.Add("Robert Schidner", "a@12@jhg");
this.dataGridView1.DataSource = dt;
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Index == 1)
{
if (e.Value != null)
{
e.Value = new string('*', e.Value.ToString().Length);
e.FormattingApplied = true;
}
}
}
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox t = e.Control as TextBox;
if (t != null)
{
t.Text = (string)dataGridView1.SelectedCells[0].Value.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
string passwords = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
passwords += dataGridView1.Rows[i].Cells[1].Value + "\n";
}
MessageBox.Show(passwords);
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindDataGrid()
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
dataGridView1.MultiSelect = False
AddHandler dataGridView1.CellFormatting, AddressOf dataGridView1_CellFormatting
AddHandler dataGridView1.EditingControlShowing, AddressOf dataGridView1_EditingControlShowing
End Sub
Private Sub BindDataGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("Password")})
dt.Rows.Add("John Hammond", "password123")
dt.Rows.Add("Mudassar Khan", "123")
dt.Rows.Add("Suzanne Mathews", "78963")
dt.Rows.Add("Robert Schidner", "a@12@jhg")
Me.dataGridView1.DataSource = dt
End Sub
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
If dataGridView1.Columns(e.ColumnIndex).Index = 1 Then
If e.Value IsNot Nothing Then
e.Value = New String("*"c, e.Value.ToString().Length)
e.FormattingApplied = True
End If
End If
End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
Dim t As TextBox = TryCast(e.Control, TextBox)
If t IsNot Nothing Then
t.Text = CStr(dataGridView1.SelectedCells(0).Value.ToString())
End If
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim passwords As String = ""
For i As Integer = 0 To dataGridView1.Rows.Count - 1
passwords += dataGridView1.Rows(i).Cells(1).Value & vbLf
Next
MessageBox.Show(passwords)
End Sub
Screenshot
