Hide Datagridview lines by date comparison

Asked

Viewed 78 times

1

Hello! I need to hide certain lines from DataGridView containing in the column data a date less than the current date. I found this code, but do not know how to hide the lines with this comparison, someone can help me?

private void dgvDados_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        this.dgvDados.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic;
        DateTime DataViagem = Convert.ToDateTime(dgvDados.Rows[e.RowIndex].Cells["data"].Value.ToString());
        DateTime DataAtual = DateTime.Now.Date;

        if (DataViagem <= DataAtual)
        {

        }
    }

1 answer

2

// Para evitar erro caso a linha selectionada seja para esconder.    
dgvDados.CurrentCell = null;

foreach (DataGridViewRow row in dgvDados.Rows)
{
    if (row.Cells["data"].Value is DateTime)
    {
        DateTime d = (DateTime)row.Cells["data"].Value;
        if (d <= DateTime.Now)
        {
            row.Visible = false;
        }
    }
}
  • You missed that part: if (row.Cells["data"].Value is DateTime d && d < DateTime.Now): Only assignment, call, increment, decrement, await, and new Object Expressions can be used as a statement. Also: The name’d' does not exist in the Current context

  • 1

    updated the answer, which version of c# you use?

  • Visual Studio C# 2012

  • This error occurred on the line dgvDados.CurrentCell = null;, error: Operation is not valid because it results in a reentrant call to the Setcurrentcelladdresscore function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.