Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I only show color.red (rows) in Datagridview. I don't know use databindings in this case

notes: I use CellFormatting in Datagridview for BackColor (red)

foreach (DataGridViewRow row in dgvVV1111.Rows)
{
  int X = Convert.ToInt32(row.Cells["TotalAmount"].Value);                    
  if (X == 0)
  {
    row.DefaultCellStyle.BackColor = Color.Red;
  }
}


Meet Google Drive – One place for all your files[^]

What I have tried:

I try to show only rows (color.red) in datagridview
Posted
Updated 10-Aug-20 20:26pm
v3

iirc a datagridview row has a visible property eg dataGridView.Rows[Index].Visible = [false|true];

so could you do this perhaps ?

foreach (DataGridViewRow row in dgvVV1111.Rows)
{
  int X = Convert.ToInt32(row.Cells["TotalAmount"].Value);                    
  if (X == 0)
  {
    row.DefaultCellStyle.BackColor = Color.Red;
  }
  else
  {
    row.Visible = false;
  }
}
(You might need intellisense to check for the Visible property, this is a dim recollection) .. if it works you could add a button to show all rows by setting all Visible to true

[Edit] I think, depending on what else you need to do, you would be better off with a bindingsource and filter.

Some useful references may be A Detailed Data Binding Tutorial[^], Summary DataGridView[^] and DataGrid with built-in filter functionality[^] [/Edit]
 
Share this answer
 
v3
Comments
BillWoodruff 11-Aug-20 4:05am    
+5
 
Share this answer
 
Thanks you for your helps. I have coded successfully...

if (chkRedLine.Checked == true)
            {
                foreach (DataGridViewRow row in dgvVV1111.Rows)
                {
                    int X = Convert.ToInt32(row.Cells["TotalAmount"].Value);
                    if (X == 0)
                    {
                        row.DefaultCellStyle.BackColor = Color.Red;
                    }
                    else
                    {
                        CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dgvVV1111.DataSource];
                        currencyManager1.SuspendBinding();
                        row.Visible = false;
                        currencyManager1.ResumeBinding();
                    //www.codeproject.com/Questions/657059/Row-associated-with-the-currency-managers-position
                    }                    
                }
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900