marky
marky

Reputation: 5068

How to hide a DataGridVew row

In a C# WinForms project, I'm iterating through a DataGridView's DataSource as a DataTable and I'm doing a check on the source database and determining if a value in one of the columns is valid. If it is a valid value I want to hide it so I only end up with the DGV showing rows with invalid values.

Here's psuedo-code of what I have so far.

private void btnValidate_Click(object sender, EventArgs e)
    {
    DataTable dt = ((DataTable)dgvMyDataGridView.DataSource);

    int intRowIndex;

    for (intRowIndex = 0; intRowIndex <= dt.Rows.Count - 1; intRowIndex++)
    {
        DataRow drValidationRow = dt.Rows[intRowIndex];

        string strNewValue = drValidationRow[5].ToString();
        // Get the current row's db record ID as a string for the db query
        int intCurrDgvRowId = int.Parse(dgvMyDataGridView[0, intRowIndex].Value.ToString());

        // Determine if we need to show or hide the DGV's row
        bool bNewValueIsValid = <db check for valid strNewValue>

        if (bNewValueIsValid)
        {
            /*
             *  Hide the DGV row corresponding to the DT row ID
             */
        }
    }
}

I tried what seems most logical to me:

dgvmyDataGridView.Rows[intRowIndex].Visible = false;

But when I run that I get this error:

System.InvalidOperationException: 'Row associated with the currency manager's position cannot be made invisible.'

I can't just do something like drValidationRow.Visible = false, as there's no Visible property on that, I'm guessing because the row is from the DataTable not the DGV.

How do I accomplish this?

Upvotes: 0

Views: 303

Answers (1)

JSL
JSL

Reputation: 83

you dont need a counter. You can just refresh dgv if Rows.Count = 0 ?

Upvotes: 1

Related Questions