switch (e.Item.ItemType)
        {
            case ListItemType.AlternatingItem:
            case ListItemType.Item:
把你的代码写在这里面
 break;
}

解决方案 »

  1.   

     <asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" 
            Height="90px" Width="1400px" 
        AutoGenerateColumns="False" onrowdeleting="GridView1_RowDeleting" 
            onselectedindexchanging="GridView1_SelectedIndexChanging" 
            onrowupdating="GridView1_RowUpdating" GridLines="Horizontal" 
            onrowdatabound="GridView1_RowDataBound">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="#333333" 
                BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="文件编号">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="select" Text='<%# Bind("pdfNo") %>' ForeColor="Blue" Font-Underline="true"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="pdfName" HeaderText="文件名称" ReadOnly="true" />
                <asp:BoundField DataField="dept" HeaderText="部门" ReadOnly="true" />
                <asp:BoundField DataField="author" HeaderText="编写者" ReadOnly="true" />
                <asp:BoundField DataField="SaveDate" HeaderText ="首次发行日期" ReadOnly="true" />
                <asp:BoundField DataField="updateDate" HeaderText="更新日期" ReadOnly="true" />
                <asp:TemplateField HeaderText="备注">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="update"  Text='<%# Bind("reName") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="link1" runat="server" Text="删除" CommandName="delete" OnClientClick="return confirm('您确定要删除吗?')"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
        </asp:GridView>
      

  2.   

     <asp:GridView ID="GridView1" runat="server" BackColor="White" 
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" 
            Height="90px" Width="1400px" 
        AutoGenerateColumns="False" onrowdeleting="GridView1_RowDeleting" 
            onselectedindexchanging="GridView1_SelectedIndexChanging" 
            onrowupdating="GridView1_RowUpdating" GridLines="Horizontal" 
            onrowdatabound="GridView1_RowDataBound">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="#333333" 
                BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="文件编号">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="select" Text='<%# Bind("pdfNo") %>' ForeColor="Blue" Font-Underline="true"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="pdfName" HeaderText="文件名称" ReadOnly="true" />
                <asp:BoundField DataField="dept" HeaderText="部门" ReadOnly="true" />
                <asp:BoundField DataField="author" HeaderText="编写者" ReadOnly="true" />
                <asp:BoundField DataField="SaveDate" HeaderText ="首次发行日期" ReadOnly="true" />
                <asp:BoundField DataField="updateDate" HeaderText="更新日期" ReadOnly="true" />
                <asp:TemplateField HeaderText="备注">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="update"  Text='<%# Bind("reName") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="操作">
                    <ItemTemplate>
                        <asp:LinkButton ID="link1" runat="server" Text="删除" CommandName="delete" OnClientClick="return confirm('您确定要删除吗?')"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#487575" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#275353" />
        </asp:GridView>
    你前面用的是控件这样是获取不到的,要这样
    ((LinkButton)e.Item.FindControl("LinkButton1")).Text你试一试!
      

  3.   

    你前面用的是控件这样是获取不到的,要这样
    ((LinkButton)e.Item.FindControl("LinkButton1")).Text你试一试!
    rowdatabound 
    里面没有 e.item.Item.FindControl() 方法
      

  4.   

    我刚发现 e.Row.Cells[1].Text;获取的是HeaderText的值,而并不是绑定的数据的值
      

  5.   

    rowdatabound 
    里面没有 e.item.Item.FindControl() 方法
    你太逗了!是e.item.FindControl()不是 e.item.Item.FindControl()string str = e.Row.Cells[0].Text;你不是想获取编号吗?
      

  6.   

    我刚发现 e.Row.Cells[1].Text;获取的是HeaderText的值,而并不是绑定的数据的值switch (e.Item.ItemType)
            {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
    把你的代码写在这里面
     break;
    }
      

  7.   

      protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex != -1)
                { if (e.Row.Cells[2].Text.Replace("&nbsp;", "").Equals("1"))
                    {
                        e.Row.Cells[1].Style.Value = "color:#ff0000";
                        e.Row.Cells[2].Style.Value = "color:#ff0000";
                        e.Row.Cells[3].Style.Value = "color:#ff0000";
                        e.Row.Cells[4].Style.Value = "color:#ff0000";
                        e.Row.Cells[5].Style.Value = "color:#ff0000";
                        e.Row.Cells[6].Style.Value = "color:#ff0000";                   // e.Row.ForeColor = Color.Red;
                    }
    }
      

  8.   

    rowdatabound 
    里面没有e.item.FindControl()
      

  9.   

    rowdatabound 
    里面没有e.item.FindControl()
      

  10.   


    rowdatabound 
    里面没有e.item.FindControl()
      

  11.   

    GridView: e.row.Findcontrol()DataList and Repeate: e.Item.Findcontrol()
      

  12.   

    参考:
    http://www.cnblogs.com/insus/archive/2011/05/28/2060705.html
    http://www.cnblogs.com/insus/archive/2013/05/19/3086475.html
    http://www.cnblogs.com/insus/p/3210157.html
      

  13.   

    我的想法是,从数据库里查出来的那几条数据(指定的某个字段),如果和gridview里的某条数据(指定的某个字段)相同,则让这行背景为其他颜色。给位给我的都是设置死数据的背景。
    现在我gridview里的第一个字段用的是LinkButton绑定的,
    用了insus的方法  LinkButton link = e.row.Findcontrol("LinkButton1")  as LinkButton; link为null。获取不到数据
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DataTable dt = pm.Get30DaysInfo();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
                    string str = link.Text;
                    string str1 = dt.Rows[i][1].ToString();
                    if (str == str1)
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }  
                }
            }
      

  14.   

    你的头模板肯定是没有LinkButton1
    加个判断
      if (e.Row.RowIndex != -1)
      

  15.   

    e.Row.RowIndex  = -1; 头模板里的确是有LinkButton1的,我的代码都贴到上面了,你应该是可以看到的。
      

  16.   

      if (e.Row.RowIndex != -1) 为什么会是false呢,难道是因为我手动绑定的gridview吗?
      

  17.   

    e.Row.RowIndex  = -1; 头模板里的确是有LinkButton1的,我的代码都贴到上面了,你应该是可以看到的。

    <HeaderTemplate></HeaderTemplate>这是头模板
      switch (e.Row.RowType)
                {
                    case DataControlRowType.DataRow:
      DataTable dt = pm.Get30DaysInfo();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
                    string str = link.Text;
                    string str1 = dt.Rows[i][1].ToString();
                    if (str == str1)
                    {
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                    }  
                }
                        break;
    }
      

  18.   

    设置GridView字段内容不同的数据行背景色
    //GridView控件显示数据设置
     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                                        AutoGenerateColumns="False" BackColor="White" 
                BorderColor="#DEDFDE" BorderStyle="None"
                                        BorderWidth="1px" CellPadding="4" DataKeyNames="产品编号" 
                                        ShowFooter="True" Width="624px" 
                OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" 
                onpageindexchanging="GridView1_PageIndexChanging" PageSize="5" 
                ForeColor="Black" GridLines="Vertical">
                                        <FooterStyle BackColor="#CCCC99" />
                                        <Columns>
                                            <asp:BoundField DataField="产品编号" HeaderText="产品编号" InsertVisible="False" ReadOnly="True"
                                                SortExpression="产品编号" />
                                            <asp:BoundField DataField="单价" HeaderText="单价" SortExpression="单价" />
                                            <asp:BoundField DataField="库存量" HeaderText="库存量" SortExpression="库存量" />
                                            <asp:BoundField DataField="已订购量" HeaderText="已订购量" SortExpression="已订购量" />
                                            <asp:TemplateField HeaderText="订货金额" SortExpression="订货金额">
                                                <EditItemTemplate>
                                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label>
                                                </EditItemTemplate>
                                                <FooterTemplate>
                                                    <asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
                                                </FooterTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="订单日期">
                                                <EditItemTemplate>
                                                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("订单日期","{0:d}") %>'></asp:Label>
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("订单日期","{0:d}") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <RowStyle BackColor="#F7F7DE" />
                                        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE" />
                                        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                                        <AlternatingRowStyle BackColor="White" />
                                    </asp:GridView>
    //加载绑定数据显示
     private decimal orderTotal = 0.0m;//创建一个变量来保存订货金额总和
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DbBind();
        }
        public void DbBind()
        {
            string strCon = ConfigurationSettings.AppSettings["ConnectionString"];
            string strsql = "SELECT 产品编号, 单价, 库存量, 已订购量, 订单日期,单价 * 已订购量 AS 订货金额 FROM tb_OrderForm";
            SqlConnection myConn = new SqlConnection(strCon);
            if (myConn.State.Equals(ConnectionState.Closed))
                myConn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strsql, strCon);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            myConn.Close();
        }//codego.net/tags/11/1/
    //根据不同字段设置数据行的背景色
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 确认“库存量”字段的值。
                // 我们通过一个 DataBinder.Eval() 调用从将被绑定到 GridView 数据行的 
                // 数据中取得“库存量”字段的值,传递给 DataBinder.Eval() 的第一个参 
                // 数是将被绑定到 GridView 数据行的数据(也就是 e.Row.DataItem),
                // 传递给 DataBinder.Eval() 的第二个参数则是字段名称。
                decimal stock =
                  Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "库存量"));
                if (stock <= 0)
                {
                    // 如果库存量小于或等于 0,则将该数据行的背景色设置成红色。
                    e.Row.BackColor = Color.Red;
                }
                decimal totalMoney =
                   Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额"));
                if (totalMoney > 0)
                {
                    // 如果订货金额大于 0,则将该数据行的背景色设置成黄色。
                    e.Row.BackColor = Color.Yellow;
                }           
                orderTotal += totalMoney;// 累加订货金额并赋给变量 orderTotal。
            }
        }