Claire
Claire

Reputation: 63

Hiding a value in a datagridview cell

I want to be able to hide the second entry of a string in a datagridview cell.

e.g. Data in the cell will look like this:

Apples  1 

Apples  2

Bananas 2

Pears   12

but I want it to look like this (with the numbers hidden, rather than removed):

Apples  

Apples 

Bananas 

Pears   

Upvotes: 1

Views: 2857

Answers (5)

Gary Huckabone
Gary Huckabone

Reputation: 412

.NET won't let you set the .Visible property of an individual cell; only the entire column. I have tricked out individual cells to seem invisible by setting the background color to that of the DataGridView. Eg, where my DataGridView is named dgvAccounts:

cells[2].Style.BackColor = dgvAccounts.BackgroundColor;

Upvotes: 0

Tobias Knauss
Tobias Knauss

Reputation: 3509

It looks like the numbers are at a fixed position inside the cells. If so, you could use padding to shrink the display area of the cell, like I described in my answer on sometimes I want to hide buttons in a DataGridViewButtonColumn

Upvotes: 0

aperezfals
aperezfals

Reputation: 1389

You can work with the format of the column, and chage the text, like this:

private void dataGridView1_CellFormatting(object sender, 
        System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("YourColumnName"))
        {
            string val = e.Value.ToString();
            e.Value = val.SubString(0, val.IndexOf(" "));
        }
    }

You must suscribe to the CellFormatting event of the datagrid. For more info about CellFormatting look here.

If you want to play with the color of the second word use this

void datagridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex  == 1 && e.Value != null)
            {
                string content = e.Value.ToString();
                string[] line = content.Split(' ');
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);

                SizeF[] size = new SizeF[line.Length];
                for (int i = 0; i < line.Length; ++i)
                {
                    size[i] = e.Graphics.MeasureString(line[i], e.CellStyle.Font);
                }

                RectangleF rec = new RectangleF(e.CellBounds.Location, new Size(0, 0));
                using (SolidBrush bblack = new SolidBrush(Color.Black), white = new SolidBrush(Color.White))
                {
                    for (int i = 0; i < line.Length; ++i)
                    {
                        rec = new RectangleF(new PointF(rec.Location.X + rec.Width, rec.Location.Y), new SizeF(size[i].Width, e.CellBounds.Height));
                        if (i % 2 == 0)
                        {
                            e.Graphics.DrawString(line[i], e.CellStyle.Font, bblack,  rec, sf);
                        }
                        else
                        {
                            e.Graphics.DrawString(line[i], e.CellStyle.Font, white, rec, sf);
                        }
                    }

                }

                e.Handled = true;
            }

        }

Suscribe the datagrid to the CellPainting event.

Upvotes: 2

Koby Douek
Koby Douek

Reputation: 16675

If you want to make the number invisible on white background, without removing it from the string, you can use this trick:

string[] arr = cell.Text.Split(' ');
cell.Text = HttpUtility.HtmlDecode(string.Format("{0} <font color='{1}'>{2}</font>", arr[0], arr[1], e.sub.backcolor));

Upvotes: 0

Adil Ansari
Adil Ansari

Reputation: 356

You can use Split function of string. Split function will split your string with given character parameter. For example cell.value.split(' ')[0]. https://www.dotnetperls.com/split

Upvotes: 0

Related Questions