简体   繁体   中英

row visibility datagridview c#

I'm stuck with this code. I want to hide some rows so I wrote this line first and its work fine

    foreach (DataGridViewRow row in dataGridView1.Rows)
      {
        if ((string)row.Cells[11].Value != (string)row.Cells[12].Value)
          {
             row.DefaultCellStyle.BackColor = Color.Red;
          }
        else
          {
            row.DefaultCellStyle.BackColor = Color.Green;
          }
      }

I want now to hide row instead of green color so I changed

row.DefaultCellStyle.BackColor = Color.Green;

with

row.Visible = false;

but i get an error with that.

You should cast the row to DataGridViewBand as documented here

DataGridViewBand band = row;
band.Visible = false;

I'm trying this method and this time row become invisible as i wanted but after that an error happen

                    CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                dataGridView1.CurrentCell = null;
                row.Visible = false;
                dataGridView1.Refresh();

error:

Could not make invisible the new line not validated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM