当前位置:编程学习 > C#/ASP.NET >>

GridView如何控制绑定数据的显示颜色?

<asp:GridView ID="gvDepartment" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvDepartment_RowDataBound"
                                Height="15px" OnRowCommand="gvDepartment_RowCommand" OnSorting="gvDepartment_Sorting"
                                OnRowCreated="gvDepartment_RowCreated" AllowSorting="True">
                                <AlternatingRowStyle CssClass="td2" Width="800px" Height="18px" />
                                <RowStyle CssClass="td21" Height="18px" />
                                <Columns>
                                    <asp:TemplateField HeaderText='<%$ Resources:Resource,reportName%>' SortExpression="REPORTNAME">
                                        <ItemTemplate>
                                             <asp:LinkButton ID="lbtnReportName" runat="server" CommandName="select" Text='<%# Eval("REPORTNAME") %>'
                                                CommandArgument='<%# Eval("REPORTID") %>' TabIndex="7"></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Width="230px" />
                                        <HeaderStyle Height="18px" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText='<%$ Resources:Resource,memberName%>' SortExpression="MEMBERNAME">
                                        <ItemTemplate>
                                             <asp:LinkButton ID="lbtnName" runat="server" CommandName="selectName" Text='<%# Eval("MEMBERNAME") %>'
                                                CommandArgument='<%#Eval("MEMBERID") %>' TabIndex="8"></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle Width="100px" HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="KOUFUSTATENAME" HeaderText='<%$ Resources:Resource,SWR1100sendState%>'
                                        SortExpression="KOUFUSTATENAME">
                                        <ItemStyle Width="100px" HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="TEAMNAME" HeaderText='<%$ Resources:Resource,SWR1100team%>'
                                        SortExpression="TEAMNAME">
                                        <ItemStyle Width="270px" HorizontalAlign="Center" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="SUBMITDATE" HeaderText='<%$ Resources:Resource,SWR1100sendDate%>'
                                        SortExpression="SUBMITDATE" DataFormatString="{0:d}" HtmlEncode="False" meta:resourcekey="beginDate">
                                        <ItemStyle HorizontalAlign="Center" Width="100px" />
                                    </asp:BoundField>
                                </Columns>
                                <HeaderStyle BackColor="Silver" HorizontalAlign="Center" />
                            </asp:GridView>








<asp:GridView ID="gvMine" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvMine_RowDataBound"
                                OnRowDeleting="gvMine_RowDeleting" OnRowEditing="gvMine_RowEditing" OnRowCommand="gvMine_RowCommand"
                                DataKeyNames="REPORTID" OnSorting="gvMine_Sorting" OnRowCreated="gvMine_RowCreated"
                                AllowSorting="True">
                                <AlternatingRowStyle CssClass="td2" Height="18px" />
                                <RowStyle CssClass="td21" Height="18px" />
                                <Columns>
                                    <asp:TemplateField HeaderText='<%$ Resources:Resource,reportName%>' SortExpression="REPORTNAME">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lbtnReportName" runat="server" CommandArgument='<%#Eval("REPORTID")%>'
                                                CommandName="select" Text='<%# Eval("REPORTNAME") %>' TabIndex="7"></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Width="227px" />
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="KOUFUSTATENAME" HeaderText='<%$ Resources:Resource,SWR1100sendState%>'
                                        SortExpression="KOUFUSTATENAME">
                                        <ItemStyle HorizontalAlign="Center" Width="200px" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="REPORTSTATENAME" HeaderText='<%$ Resources:Resource,SWR1100reportStauts%>'
                                        SortExpression="REPORTSTATENAME">
                                        <ItemStyle HorizontalAlign="Center" Width="200px" />
                                    </asp:BoundField>
                                    <asp:TemplateField HeaderText='<%$ Resources:Resource,pageModify%>'>
                                        <ItemTemplate>
                                            <asp:Button ID="btnEdit" runat="server" Text='<%$ Resources:Resource,modify%>' CommandName="Edit"
                                                CssClass="buttonStyle" TabIndex="9" meta:resourcekey="btnModify" UseSubmitBehavior=false/>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Width="90" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText='<%$ Resources:Resource,pageDelete%>' meta:resourcekey="delete">
                                        <ItemTemplate>
                                            <asp:Button ID="btnDelete" runat="server" Text='<%$ Resources:Resource,delete%>'
                                                meta:resourcekey="btnDelete" CommandName="Delete" CssClass="buttonStyle" TabIndex="10"/>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Width="90" />
                                    </asp:TemplateField>
                                </Columns>
                                <HeaderStyle BackColor="Silver" BorderStyle="None" HorizontalAlign="Center" />
                            </asp:GridView>


这是一个画面上的两个一览表,它们的id分别为“gvDepartment”和“gvMine”。当部长身份登陆时,页面初始化时显示id为“gvDepartment”的一览表,当社员身份登陆时显示
id为“gvMine”的一览表。但目前有一个问题需要请教大家,id为“gvDepartment”的一览表初始话时其绑定的数据会有颜色区分,而id为“gvMine”的一览表初始话时却没有。
 <asp:BoundField DataField="KOUFUSTATENAME" HeaderText='<%$ Resources:Resource,SWR1100sendState%>'SortExpression="KOUFUSTATENAME">
 <ItemStyle HorizontalAlign="Center" Width="200px" />
 </asp:BoundField>
麻烦大家了。
--------------------编程问答-------------------- --------------------编程问答-------------------- protected void gvMine_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            // ログ出力
            Common.putTraceLog("Start", "I", loginID, "gvMine_RowDataBound");
            // データ行の場合
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 交付状態の値=[延長]の場合
                if (e.Row.Cells[1].Text.Trim().Equals("延期"))
                {
                    // 文字色は赤くなる。
                    e.Row.Cells[1].ForeColor = Color.Red;
                }
                // 交付状態の値=[正常]の場合
                if (e.Row.Cells[1].Text.Trim().Equals("正常"))
                {
                    // 文字色は緑になる。
                    e.Row.Cells[1].ForeColor = Color.Green;
                }
                // 交付状態の値=[未提出]の場合
                if (e.Row.Cells[1].Text.Trim().Equals("未提出"))
                {
                    // 文字色は黄色になる。
                    e.Row.Cells[1].ForeColor = Color.Blue;
                }

                //******2009/06/03******付伝景******削除開始******  
                //// 提出済みの場合
                //if (e.Row.Cells[2].Text.Trim().Equals("提出済み"))
                //{
                //    // 修正ボタンは使用不可になる。
                //    ((Button)e.Row.FindControl("btnEdit")).Enabled = false;
                //}
                //******2009/06/03******付伝景******削除完了******

            }
            // ログ出力
            Common.putTraceLog("End", "I", loginID, "gvMine_RowDataBound");
        }
知道它对应的处理方法了。呵呵。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,