1

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   
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
Claire
  • 63
  • 1
  • 9

5 Answers5

2

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.

aperezfals
  • 1,341
  • 1
  • 10
  • 26
0

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

Adil Ansari
  • 346
  • 2
  • 13
0

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));
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0

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

Community
  • 1
  • 1
Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
0

.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;
Gary Huckabone
  • 402
  • 3
  • 10