ASP.NET GridView-无法在数据绑定期间设置行的颜色?

ASP.NET GridView-无法在数据绑定期间设置行的颜色?,asp.net,gridview,Asp.net,Gridview,这简直快把我逼疯了!这是我用Datagrid做了100多年的事情。我现在使用的是Gridview,我无法理解这一点 我有这个网格: <asp:GridView AutoGenerateColumns="false" runat="server" ID="gvSelect" CssClass="GridViewStyle" GridLines="None" ShowHeader="False" PageSize="20" AllowPaging="True">

这简直快把我逼疯了!这是我用Datagrid做了100多年的事情。我现在使用的是Gridview,我无法理解这一点

我有这个网格:

<asp:GridView AutoGenerateColumns="false" runat="server" ID="gvSelect" CssClass="GridViewStyle"
        GridLines="None" ShowHeader="False" PageSize="20" AllowPaging="True">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" ID="lbldas" Text="blahblah"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
而且它从不设置行的背景色。。我成功地使用了:

gridrow.Cells(0).BackColor = Drawing.Color.Blue
但是整排都是这样吗?不!这让我快发疯了。。有人能为我找到解决办法吗

为了好玩,我把这个放在同一页上:

<asp:DataGrid AutoGenerateColumns="false" runat="server" ID="dgSelect" GridLines="None"
        ShowHeader="False" PageSize="20" AllowPaging="True">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:Label runat="server" ID="lbldas" Text="blahblah"></asp:Label>
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>
而且它的工作原理与预期一样。。那么我对Gridview有什么错呢

**更新************************

我想我应该发布生成的HTML,以表明任何样式都不会影响这一点

以下是gridview html:

<div class="AspNet-GridView" id="gvSelect"> 
<table cellpadding="0" cellspacing="0" summary=""> 
    <tbody> 
        <tr> 
            <td> 
                <span id="gvSelect_ctl02_lbldas">blahblah</span> 
            </td> 
        </tr> 
    </tbody> 
</table> 
 </div>

布拉布拉赫
下面是生成的Datagrid HTML:

<table cellspacing="0" border="0" id="dgSelect" style="border-collapse:collapse;"> 
<tr onMouseOver="this.style.backgroundColor='lightgrey'"> 
    <td> 
        <span id="dgSelect_ctl03_lbldas">blahblah</span> 
            </td> 
</tr>
 </table> 

布拉布拉赫
看。。主要区别在于标签。它永远不会在gridview中设置。。我不知道为什么。。我已经查过了。。然后代码运行..:我是这样做的:

Protected Sub gvwCompounds_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.CssClass = "rowstyle"
    End If
End Sub
在example.css中:

.rowstyle
{
    background-color:#e5e5e5;
}

这实际上应该在RowCreatedEvent期间完成。刚刚测试了下面的代码,它在鼠标悬停时高亮显示/取消高亮显示一行,效果非常好

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ccaaaa';")
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';")
    End If
End Sub
编辑:附加html输出(注意:在VB和C中都可以使用,并且创建了行-输出相同)


圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰圣约翰
加利福尼亚
俄亥俄州
田纳西州
42TXTEXAS
弗吉尼亚州
华盛顿
49怀俄明州
爱荷华州
24蒙密苏里州
南达科他州
犹他州
佛蒙特州
弗吉尼亚州西部47号
威斯康星州
54阿卡拉斯卡
编辑:这是我得到的HTML方面。我保持简单。在HTML端,您可能有一个干扰的css配置

<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="ST_CD" HeaderText="ST_CD" SortExpression="ST_CD" />
            <asp:BoundField DataField="ST_CD_ALPHA" HeaderText="ST_CD_ALPHA" 
                SortExpression="ST_CD_ALPHA" />
            <asp:BoundField DataField="ST_DESC" HeaderText="ST_DESC" 
                SortExpression="ST_DESC" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:WebTestConnectionString %>" 

        SelectCommand="SELECT [ST_CD], [ST_CD_ALPHA], [ST_DESC] FROM [STATE_VALUES] WHERE ([ST_CD] LIKE '%' + @ST_CD + '%')">
        <SelectParameters>
            <asp:Parameter DefaultValue="4" Name="ST_CD" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>

此问题发生在IE6中。我通过设置gridview行中所有单元格的CssClass名称解决了这个问题。代码如下:

private void grdvw_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       e.Row.Attributes.Add("onmouseover", "rowHighlight(this,'lightOn');"); 
       e.Row.Attributes.Add("onmouseout", "rowHighlight(this,'');");
    }
}

function rowHighlight(obj, nameOfTheClass) 
{
   cells = obj.getElementsByTagName("td");
   for (var i = 0; i < cells.length; i++) 
   {
       cells[i].className = nameOfTheClass;
   }
}
private void grdvw_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
e、 Add(“onmouseover”、“rowHighlight(this,'lightOn');”;
e、 添加(“onmouseout”、“rowHighlight(this)、;”;
}
}
函数行高亮显示(obj,类名称)
{
cells=obj.getElementsByTagName(“td”);
对于(变量i=0;i
您是否在GridView中使用CSSFriendly控件适配器?它们可能没有呈现您添加的属性。

我不明白您为什么要两次检查它是否是数据行。我甚至试过你的e.row.cssclass,当我查看呈现页面的源代码时,它仍然是:不是预期的:(谢谢,这是个错误。我修复了它。是的..即使如此..它在DataGrid中的工作方式很奇怪,但在GridView中却不是..而且它们都在同一页上。这让我很难过..@Dan您在RowDataBound事件处理程序中的参数正确吗?即GridViewRowEventArgsDavid,是的。我编辑了代码来显示它。也许这是一个bug w通过这个gridview控件和VB。我只在网络上找到了C#中的代码。我知道它不是行数据绑定,因为我可以设置单元格的颜色。并用鼠标在上面突出显示单元格。但是整行都不起作用。它从来没有向上添加任何内容。我从顶部尝试了你的代码,它工作正常。你检查过有没有问题吗在单元格上设置bg颜色的规则,这些规则将覆盖行上设置的任何bg颜色?我从页面中删除了所有样式。由于datagrid工作正常,因此没有任何意义。是否有可能我的datagrid版本不好?是否有visual studio 2008的service pack或修补程序我不知道?我尝试了。并且它不起作用..但它适用于我的datagrid..这太令人沮丧了..肯定还有其他一些底层问题S@Dan:您使用的是什么框架?我在VS 2008中使用了完全相同的代码,它工作得非常出色。如果您使用的是2.0/3.0,我可以在其中进行测试。我使用的是3.5..我检查了我的visual studio及其9.0版。30729.1那么..它有service pack 1…你能在运行页面后发布网格的结果HTML吗?@Dan:使用一些美国州的表格发布结果。我想知道为什么你的和我的有这么大的不同..我的div是,而你的只是,ID在表格名中..我的怎么了?
<div>
  <table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
    <tr>
      <th scope="col">ST_CD</th><th scope="col">ST_CD_ALPHA</th><th scope="col">ST_DESC</th>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>04</td><td>CA</td><td>CALIFORNIA                    </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>34</td><td>OH</td><td>OHIO                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>41</td><td>TN</td><td>TENNESSEE                     </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>42</td><td>TX</td><td>TEXAS                         </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>45</td><td>VA</td><td>VIRGINIA                      </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>46</td><td>WA</td><td>WASHINGTON                    </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>49</td><td>WY</td><td>WYOMING                       </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>14</td><td>IA</td><td>IOWA                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>24</td><td>MO</td><td>MISSOURI                      </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>40</td><td>SD</td><td>SOUTH DAKOTA                  </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>43</td><td>UT</td><td>UTAH                          </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>44</td><td>VT</td><td>VERMONT                       </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>47</td><td>WV</td><td>WEST VIRGINIA                 </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>48</td><td>WI</td><td>WISCONSIN                     </td>
    </tr><tr onmouseover="this.style.backgroundColor='#ccaaaa';" onmouseout="this.style.backgroundColor='#ffffff';">
      <td>54</td><td>AK</td><td>ALASKA                        </td>
    </tr>
  </table>
</div>
<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="ST_CD" HeaderText="ST_CD" SortExpression="ST_CD" />
            <asp:BoundField DataField="ST_CD_ALPHA" HeaderText="ST_CD_ALPHA" 
                SortExpression="ST_CD_ALPHA" />
            <asp:BoundField DataField="ST_DESC" HeaderText="ST_DESC" 
                SortExpression="ST_DESC" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:WebTestConnectionString %>" 

        SelectCommand="SELECT [ST_CD], [ST_CD_ALPHA], [ST_DESC] FROM [STATE_VALUES] WHERE ([ST_CD] LIKE '%' + @ST_CD + '%')">
        <SelectParameters>
            <asp:Parameter DefaultValue="4" Name="ST_CD" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>
private void grdvw_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       e.Row.Attributes.Add("onmouseover", "rowHighlight(this,'lightOn');"); 
       e.Row.Attributes.Add("onmouseout", "rowHighlight(this,'');");
    }
}

function rowHighlight(obj, nameOfTheClass) 
{
   cells = obj.getElementsByTagName("td");
   for (var i = 0; i < cells.length; i++) 
   {
       cells[i].className = nameOfTheClass;
   }
}