Hello,
Thanks for your post on MSDN forum.
To hide the value for some cells in the DataGridView control, we can handle the CellFormatting event for the DataGridView and set the FormatValue of the cell to some other character(e.g. the password char). Here is a simple sample for your information.
private void Form4_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("price", typeof(decimal));
dt.Rows.Add("item1", 1.25);
dt.Rows.Add("item2", 2.25);
dt.Rows.Add("item3", 4.25);
dt.AcceptChanges();
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[1].DefaultCellStyle.Format = "C2";
this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1&&e.RowIndex !=this.dataGridView1.NewRowIndex)
{
if (this.dataGridView1[0, e.RowIndex].Value.ToString() == "item1")
{
//change the cell's formatvalue
e.Value = "****";
e.CellStyle.BackColor = Color.Gray;
this.dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = true;
e.FormattingApplied = true;
}
else
e.FormattingApplied = false;
}
}
Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact
msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.