How to check the status (True/False) in a grid view on rowdatabound event?

11,593

Solution 1

you can try like...

if (((CheckBox)e.Row.Cells[3]).Checked == false)

OR, event better if you do like..

 if(((CheckBox)e.Row.FindControl("YourCheckboxID")).Checked == false)

Edit: since you are using RowDataBound Event, in which at that time row don't have what you are finding. you need to do this like...

DataRow dr = ((DataRowView)e.Item.DataItem).Row;
if (Convert.ToBoolean(dr["Status"]) == false)

Solution 2

Check if you have a CheckBox control in:

e.Rows.Cells[3].Controls

Then you can get its Checked property:

((CheckBox)e.Rows.Cells[3].Controls[0]).Checked
Share:
11,593
NayeemKhan
Author by

NayeemKhan

Updated on June 04, 2022

Comments

  • NayeemKhan
    NayeemKhan about 2 years

    I have a table with three columns (ProdID,ProdName,Status). I m fetching that into a dataSet and binding that to my gridview. I have a very basic and simple rowdatabound event like this :

     protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[2].Text == "False")
                {
                    e.Row.BackColor = System.Drawing.Color.PaleVioletRed;
                }
            }
        }
    

    But when i see my 3rd column (Status), it is converted to a checkbox, may be becz its containing only 'True' or 'False'. Also in my if condition :

    if (e.Row.Cells[3].Text == "False")
    

    the text value is showing this :

    ""

    Can anybody suggest me, how can i compare my status against True or False in my if condition.

  • Adeel
    Adeel over 13 years
    +1, but before comparison, check FindControl returns null or not.
  • NayeemKhan
    NayeemKhan over 13 years
    I have mentioned in the post itself that m not having any checkbox control.Since in my database, the column field type is bit, the status column is displayed in the format of checkbox automatically in my gridview.
  • NayeemKhan
    NayeemKhan over 13 years
    @ Akhtar: have u written code for DataRowBound event handler?