0

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?

marky
  • 4,878
  • 17
  • 59
  • 103
  • AFAIK you can use ```SuspendBinding```, set ```Visible``` to false and use ```ResumeBinding``` aftewards. Not sure, though. – devsmn Apr 27 '20 at 13:39
  • 2
    You can add a `bool` (hidden) Column to your DataTable. Set the Value of this Column to true/false when it passes/fails validation, then just use `dt.DefaultView.RowFilter = "[MyBoolColumn] = false"`. Invert when needed, set `.RowFilter = string.Empty` to remove the filter. When a filter is active, changing the value of the bool Column will update the view immediately (no need to do anything else). – Jimi Apr 27 '20 at 13:41
  • Shawn is right, have a look on : [Unable To set row visible false of a datagridview](https://stackoverflow.com/a/18942430/12906488) – JSL Apr 27 '20 at 13:56
  • @Shawn, I checked out the link that JSL provided and that worked, but I ran into a situation where I ended up with no rows displaying because all values were valid, which hid all rows in the DGV. My thought was to add a counter and compare the number of rows hidden to the number of rows in the DGV and just refreshing the DGV if all rows are valid. Is there a better way to handle that situation? – marky Apr 27 '20 at 14:17
  • @marky I don't really think I know what you're trying to achieve. If all values are valid, there will be nothing left to display. Correct? – devsmn Apr 27 '20 at 15:08
  • Playing with visibility of rows is definitely wrong solution. You need to use filtering feature of data table, like explained by Jimi. – Reza Aghaei Apr 28 '20 at 07:52

1 Answers1

1

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

JSL
  • 83
  • 7