简体   繁体   中英

Fill DataGridView row according to a string value of a cell

Im trying to fill a row according to a string text, better explained:

Cell number 7 has 2 values ("1 - Pago" or "2- Pendente"), if its the 1 option i want that row painted in Green, else i want it red.

I tried several codes, but i cant get it, i have this so far:

foreach (DataGridViewRow row in DataGridView1.Rows) 
 if (Convert.ToInt32(row.Cells[7].Value) == "1- Pago)) 
 {
     row.DefaultCellStyle.BackColor = Color.Red; 
 }

Pretty sure, error is on 2 line, but im new and i still couldnt find a solution, i tried changed Value to Text, still errors.

Thank you.

You have 2 major compile errors:

1) You tried to equal Int and String

2) Your string at right side of equation doesnt have proper "" symbol at the end.

Try this:

 foreach (DataGridViewRow row in vendorsDataGridView.Rows) 
      if (row.Cells[7].Value.ToString() == "1 - Pago") 
           row.DefaultCellStyle.BackColor = Color.Red; 

You are trying to convert a text into a Integer number. Try changing the second row to

if (row.Cells[7].Value.ToString() == "1 - Pago")

Also, pay attention to spaces inside your string, if you miss one while writing the option, this will not work.

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