简体   繁体   English

如何在rowdatabound事件的网格视图中检查状态(True / False)?

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

I have a table with three columns (ProdID,ProdName,Status). 我有一个包含三列的表(ProdID,ProdName,Status)。 I m fetching that into a dataSet and binding that to my gridview. 我将其提取到dataSet并将其绑定到我的gridview。 I have a very basic and simple rowdatabound event like this : 我有一个非常基本和简单的rowdatabound事件,如下所示:

 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'. 但是当我看到我的第3列(状态)时,它被转换为复选框,可能因为它只包含'True'或'False'。 Also in my if condition : 也在我的if条件中:

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. 任何人都可以建议我,如何在我的if条件下将我的状态与True或False进行比较。

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. 编辑:因为您正在使用RowDataBound事件,其中当时行没有您找到的内容。 you need to do this like... 你需要这样做......

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

Check if you have a CheckBox control in: 检查您是否有CheckBox控件:

e.Rows.Cells[3].Controls

Then you can get its Checked property: 然后你可以得到它的Checked属性:

((CheckBox)e.Rows.Cells[3].Controls[0]).Checked

如果Status列是一堆复选框,则测试复选框的Checked属性是true还是false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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