Search

just show me the code

Thursday, October 30, 2008

bind a gridview column to a method

bind a attribute of the ItemTemplate to a method

<asp:TemplateField HeaderText="Deleted Ind" SortExpression="DeletedInd"> 
    <ItemTemplate>
        <asp:CheckBox ID="cbDeletedInd" runat="server" 
            Checked='<%# this.GetNullableBool(Eval("DeletedInd"))  %>' 
            Enabled="false" />
    </ItemTemplate>
</asp:TemplateField>


put the method in your codebehind (this could be a bind utility method)

protected bool GetNullableBool(object o)
{
    bool bReturn = false;
    Nullable<bool> b = o as Nullable<bool>;
    if (b != null)
    {
        if (b == true)
        {
            bReturn = true;
        }
    }
    return bReturn;
}


this method returns false if the bool is false or null
otherwise it returns true

now we use the RowDataBound to format that row


protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
    CheckBox c = (CheckBox)e.Row.FindControl("cbDeletedInd");
    if (c != null && c.Checked)
    {
        e.Row.ForeColor = System.Drawing.Color.LightGray;
    } 
}


results: (the deleted rows are now grey)


No comments:

Post a Comment

Contributors