Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Clear or hide specific cell in datagrid view
 

Clear or hide specific cell in datagrid view

Hello,

I have a single datagrid on my windows form that gets popluated with an item description and item price when a user clicks the various item buttons.

For example the grid might look like below:

Item1 $1.25
Item2 $2.25
Item3 $4.25

The question I have is that I need to hide or block out the price for specific items. For instance, I may not want the user to see the price for "Item1" but I do want them to see prices for hte other items. I have tried to work with changing colors but if someone selects the column you can still see the text.

Any ideas on how this could be implemented using WIndows Forms and C#?
darrin_b  Monday, August 24, 2009 9:56 PM
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.
Rong-Chun Zhang  Tuesday, August 25, 2009 3:53 AM
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.
Rong-Chun Zhang  Tuesday, August 25, 2009 3:53 AM

Thanks so much for the sample code. However, this changes the Item column to "****". I would like the price column formated to the "****".

Therefore, when I try to change the price column using this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "*****"; I get an error message that the format is not in decimal form. Appears that since that column is a decimal it is looking for that format. Anyways around this?

Item1 $2.32
Item2 *****
Item3 $4.32
Item4 *****
Item 5 $2.36
Item6 $1.56
....etc.

UPDATE:--------
See you had more code in the load property so your examples works great!!!!!! Therefore, ingnore the question above.


Thanks!!
darrin_b  Tuesday, August 25, 2009 1:35 PM

You can use google to search for other answers

Custom Search

More Threads

• Differing Row Heights
• How to Auto-Adjust row heights for multiline cell contents?
• <ENTER> doesn't trigger KeyDown event while DataGridView is in edit mode
• DISTINCT values of Datatable columns???
• DataGridView select all
• Interacting with Flash Movie from .Net Application C#
• DataGridView Custom BackGround
• Show relational data on datagridview
• weird bug... taking me forever...
• Null values being displayed