Conditional output in cell based on row data in Gridview's RowDataBound event

后端 未结 3 351
后悔当初
后悔当初 2021-01-12 22:45

i have a bit value (Black) i want to display its status in gridview as if its true, the row display \"Yes\", otherwise the row display \"No\", this is my code, but the resul

相关标签:
3条回答
  • 2021-01-12 23:34

    I don't know your datasource, but if you can evaluate it, do something like this:

    <asp:TemplateField HeaderText="Status">
                <ItemStyle CssClass="list" />
                <ItemTemplate>
                        <%# GetBit(Eval("BlackBit"))%>
                </ItemTemplate>
    </asp:TemplateField>
    

    And code-behind:

    private string GetBit(object objBit)
    {
        if (Convert.ToInt32(objBit) == 1)
        {
            return "Yes";
        }
        return "No";
    }
    
    0 讨论(0)
  • 2021-01-12 23:40

    Do you need to iterate through a DataTable dt on each RowDatabound ?

    If you do not need this could you try:

    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    
                    Boolean bitBlack = Convert.ToBoolean(e.Row.Cells[7].Text);
                    if (bitBlack)
                    {
                        e.Row.Cells[7].Text = "Yes";
                    }
                    else
                    {
                        e.Row.Cells[7].Text = "No";
                    }
    
            }
        }
    
    0 讨论(0)
  • 2021-01-12 23:45

    You could always use the rows DataItem to get the underlying DataSource:

    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRow row = ((DataRowView)e.Row.DataItem).Row;
            bool isBlack = row.Field<bool>("Black");
            e.Row.Cells[7].Text = isBlack ? "Yes" : "No";
        }
    }
    
    0 讨论(0)
提交回复
热议问题