开发者

How can I set information on a cell in a datagrid based on its header or column?

I have an ASP.NET 2.0 DataGrid, like so:

<asp:DataGrid 
    runat="server"
    ID="_customerTable"
    CssClass="gridDisplay"
    HeaderStyle-CssClass="headerRow"
    AutoGenerateColumns="False"
>
    开发者_JAVA百科<Columns>
        <asp:TemplateColumn HeaderText="First Name">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Last Name">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "LastName") %>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Address 1">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Address1") %>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Address 2">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Address2") %>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

The problem is, I need to set up a cssclass for cells that meet certain criteria in a column. The best I can seem to figure out is a rather ugly method--either specifically doing e.Item.Cells[0]...[3] for each column, or setting ItemStyle-CssClass="firstName"..."address2", and then updating the cssclass for a cell based on my conditions and whether or not the cell's cssclass matches.

This seems like a really ugly solution, though, so I'd far prefer any advice on a better way--if one exists.


Not sure what your criteria is, but if you had something along these lines:

<asp:DataGrid
    ...
    <Columns>
        <asp:TemplateColumn HeaderText="First Name">
            <ItemTemplate>
                 <asp:Label ID='lbl' 
                     Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' 
                     CssClass= "%conditional_expression%"   />
            </ItemTemplate>
        </asp:TemplateColumn>                
    ...
    </Columns>
</asp:DataGrid>

Assuming you had css classes txtBlue and txtPink, and the criteria is based of a Gender column, then the conditional_expression might be something like:

<% DataBinder.Eval(Container.DataItem, "Gender") == "Male" ? "txtBlue" : "txtPink" %>

Not sure the above is valid syntax, just a suggestion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜