Home > Net >  C# Datagridview Cell Value Showing or Hiding Other Value
C# Datagridview Cell Value Showing or Hiding Other Value

Time:05-21

What I am trying to do is in each row if the datagridview there is a Cell (2) called Serial. If the serial contains the word Meco or Melco it will show the other cell values (Siding / Roof & Trim) but if it DOES NOT contain Meco Or Melco then it hides that values of Siding & Trim I tried the following and alot of other things.

 string s = "MECO";

        if (s.Contains("MECO") == true)
        {
            dataGridView1.Columns[22].Visible = true; // Siding
            dataGridView1.Columns[23].Visible = true; // Roof
            dataGridView1.Columns[24].Visible = true; // Trim
            Console.WriteLine("Word found!");
        }
        else
        {
            Console.WriteLine("Word not found!");
            dataGridView1.Columns[22].Visible = false; // Siding
            dataGridView1.Columns[23].Visible = true; // Roof
            dataGridView1.Columns[24].Visible = false; // Trim
        }

However I have not got anything to work.. can someone please help this is basically for ordering metal for metal barns and non metal barns only showing the roof color.

CodePudding user response:

You say

If the serial contains the word Meco or Melco it will show the other cell values, but if it DOES NOT contain Meco Or Melco then it hides that values

At User Interface level to hide and show cell VALUES you can handle CellPaint event of DataGridView and omit painting of cell content. Here is an Example,

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.RowIndex == dataGridView1.NewRowIndex)
    {
        return;
    }
    var s = dataGridView1[serialDataGridViewTextBoxColumn.Index, e.RowIndex].Value as string;
    if (s.Contains("MECO") || s.Contains("MELCO"))
    {
        var flag1 = e.ColumnIndex == sidingDataGridViewTextBoxColumn.Index;
        var flag2 = e.ColumnIndex == trimDataGridViewTextBoxColumn.Index;
        if (flag1 || flag2)
        {
            e.Paint(e.ClipBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
            e.Handled = true;
        }
    }
}

However, to make the interface more comprehensive for the users, you can also draw something else instead of cell's actual content to mean hidden value. What to draw is up to you.


Edit

According to the image file you provided, Serial column is at 1, Siding column is 2, Trim column is 4. So the above code should be

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.RowIndex == dataGridView1.NewRowIndex)
    {
        return;
    }
    var s = dataGridView1[1, e.RowIndex].Value as string;
    if (!(s.Contains("MECO") || s.Contains("MELCO")))
    {
        if (e.ColumnIndex == 2 || e.ColumnIndex == 4)
        {
            e.Paint(e.ClipBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
            e.Handled = true;
        }
    }
}

CodePudding user response:

Thank you all for the help I appreciate it..DotNet Developer appreciate it

  • Related