Error hiding dataGridView line

Asked

Viewed 373 times

1

Hello, I need to hide all the lines from dataGridView containing in the column data, the date lower than the current date, ie with the code below I get the following error: Não é possível tornar invisível a linha associada à posição do gerenciador de moeda.

Code:

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;

        foreach (DataGridViewRow row in dgvDados.Rows)
        {
            if (DataViagem <= DataAtual)
            {
                row.Visible = false;
            }
        }
    }
  • Have you tried putting one dgvDados.ClearSelection() before hiding the lines?

3 answers

0

I have found a solution that seems to me the right one to avoid this problem. The error happens because there is a Currencymanager associated with Datasource.

public partial class Form1: Form
{
    // ****************
    CurrencyManager cm;
    // ****************

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (DataTable dt = Modulos.ExecutaQuery("SELECT * FROM PRODUTO"))
        {
            dataGridView1.DataSource = dt;
            // **********
            cm = (CurrencyManager)BindingContext[dataGriView1.DataSource];
            //  *********
        }
    }

    private void OcultaLinha(DataGridViewRow r) 
    {
        // ****************************
        cm.SuspendBinding();
        r.Visible = false;
        cm.ResumeBinding();
        // ****************************
    }
}

0

If I’m not mistaken, the line the cursor points to cannot be hidden, try to ignore the selected line of your datagrid and hide the others.

I have the following code: I put a checkbox on the datagrid at the 0 position of the cell;

This code checks if the checkbox is selected and then hides the line.

foreach (DataGridViewRow row in dtClientesDtgv.Rows)
{
    DataGridViewCheckBoxCell ck = (DataGridViewCheckBoxCell)row.Cells[0];
    //Passa se o checkBox estiver selecionado;
    if (Convert.ToBoolean(ck.Value) == true)
    {
        row.Visible = false;
    }
}

I really believe the problem is hiding Currentrow from datagrid.

  • I tried to dgvDados.CurrentCell = null; but it also doesn’t work

0

I think the command you’re using only works for the column,

dataGridView.Columns[4].Visible = false; //isto funciona
dataGridView.row[4].Visible = false; //isto vai dar erro

In the case of the line, you will have to remove the datagridview line if you do not want it to appear

Browser other questions tagged

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