2011-01-25 90 views
0

我有一個gridview與某些框以綠色突出顯示。這些盒子應該填滿整個盒子,但我似乎無法在邊緣周圍垃圾該1px邊框。我使用的是IE7,但FF也是。Gridview列顏色不填充整個框

GridViewSS

呈現的HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title> 

</title><link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" /><link href="App_Themes/Contoso/Style.css" type="text/css" rel="stylesheet" /></head> 
<body> 
    <form name="form1" method="post" action="GridViewColoring.aspx" id="form1"> 
<div> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODIyMTgxMzQxZBgBBQh0ZXN0R3JpZA88KwAMAQgCAWR6Qz5BuXEoalr4HjsTfYqqKPrdwd2ICIXpeNacwdi46w==" /> 
</div> 

    <div> 
    <div> 
    <table class="cssTable" cellspacing="0" rules="all" border="1" id="testGrid" style="border-collapse:collapse;"> 
     <tr> 
      <th scope="col">Description</th><th scope="col">Serial#</th> 
     </tr><tr style="background-color:Yellow;"> 
      <td class="NoMargin NoPadding" style="font-size:Smaller;"> 
         <span id="testGrid_ctl02_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span> 
        </td><td style="font-size:Smaller;"> 
         <span id="testGrid_ctl02_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;height:100%;width:100%;">0</span> 
        </td> 
     </tr><tr style="background-color:Yellow;"> 
      <td class="NoMargin NoPadding" style="font-size:Smaller;"> 
         <span id="testGrid_ctl03_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span> 
        </td><td style="font-size:Smaller;"> 
         <span id="testGrid_ctl03_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;background-color:#CCFFCC;height:100%;width:100%;">1000</span> 
        </td> 
     </tr><tr style="background-color:Yellow;"> 
      <td class="NoMargin NoPadding" style="font-size:Smaller;"> 
         <span id="testGrid_ctl04_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span> 
        </td><td style="font-size:Smaller;"> 
         <span id="testGrid_ctl04_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;background-color:#CCFFCC;height:100%;width:100%;">2000</span> 
        </td> 
     </tr> 
    </table> 
</div> 
    </div> 
    </form> 
</body> 
</html> 

測試用例
CSS

body { 
} 
.NoMargin 
{ 
    margin:0 0 0 0; 
} 
.NoPadding 
{ 
    padding:0 0 0 0; 
} 
.BgColor 
{ 
    background-color:Aqua; 
} 
.MaxHeightAndWidth 
{ 
    height:100%; 
    width:100%; 
} 
.NoBorder 
{ 
    border:0px; 
} 

ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewColoring.aspx.cs" Inherits="WebApplication1.GridViewColoring" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <asp:GridView id="testGrid" runat="server" CssClass="cssTable" 
      AutoGenerateColumns="False" 
      OnRowDataBound="SetStatusColors" > 
      <Columns> 
       <asp:TemplateField HeaderText="Description" SortExpression="description" ItemStyle-CssClass="NoMargin NoPadding"> 
        <ItemTemplate> 
         <asp:Label ID="descriptionLbl" runat="server" Text='<%# Bind("description") %>'></asp:Label> 
        </ItemTemplate> 
        <ItemStyle Font-Size="Smaller" /> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Serial#" SortExpression="serial"> 
        <ItemTemplate> 
         <asp:Label ID="serialNumberLbl" runat="server" Text='<%# Bind("serial") %>' CssClass="NoMargin NoPadding MaxHeightAndWidth NoBorder" Height="100%" Width="100%"></asp:Label> 
        </ItemTemplate> 
        <ItemStyle Font-Size="Smaller" /> 
       </asp:TemplateField>     
      </Columns> 
      </asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 

C#後端

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

namespace WebApplication1 
{ 
    public partial class GridViewColoring : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      testGrid.DataSource = MakeTable(); 
      testGrid.DataBind(); 
     } 
     protected void SetStatusColors(object sender, GridViewRowEventArgs e) 
     { 
      for (int i = 0; i < testGrid.Rows.Count; i++) 
      { 
       string serialNumber = ((Label)testGrid.Rows[i].FindControl("serialNumberLbl")).Text; 
       if (serialNumber != "0") 
       { 
        //GREEN HIGHLIGHTS 
        ((Label)testGrid.Rows[i].FindControl("serialNumberLbl")).BackColor = System.Drawing.Color.FromArgb(204, 255, 204); 
       } 
       testGrid.Rows[i].BackColor = System.Drawing.Color.Yellow; 
      } 
     } 
     //mock db 
     private DataSet MakeTable() 
     { 
      var table = new DataTable("ParentTable"); 
      DataColumn column; 
      DataRow row; 

      // Create new DataColumn, set DataType, 
      // ColumnName and add to DataTable.  
      column = new DataColumn(); 
      column.DataType = System.Type.GetType("System.Int32"); 
      column.ColumnName = "serial"; 
      column.ReadOnly = true; 

      // Add the Column to the DataColumnCollection. 
      table.Columns.Add(column); 

      //// Create second column. 
      column = new DataColumn(); 
      column.DataType = System.Type.GetType("System.String"); 
      column.ColumnName = "description"; 
      column.AutoIncrement = false; 
      column.Caption = "Description"; 
      column.ReadOnly = false; 
      column.Unique = false; 
      // Add the column to the table. 
      table.Columns.Add(column); 

      // Instantiate the DataSet variable. 
      var dataSet = new DataSet(); 
      // Add the new DataTable to the DataSet. 
      dataSet.Tables.Add(table); 

      // Create three new DataRow objects and add 
      // them to the DataTable 
      for (int i = 0; i <= 2; i++) 
      { 
       row = table.NewRow(); 
       row["serial"] = i * 1000; 
       row["description"] = "Some desc " + DateTime.Now; 
       table.Rows.Add(row); 
      } 
      return dataSet; 
     } 
    } 
} 

更新
改變了序列號模板的itemstyle,它解決了這一問題。我不知道爲什麼,但感謝你的建議,我是能夠減少的問題上下足試試:

<ItemStyle Font-Size="Smaller" CssClass="NoMargin NoPadding" /> 
+0

你可以發佈截圖 – Pabuc 2011-01-25 14:57:10

+0

@Pabuc你去我的朋友 – 2011-01-25 15:04:34

回答

1

嘗試設置邊界崩潰:崩潰;在cssTable的CSS類。

好吧,我能夠得到它的工作。我從第一個模板項目中取出了css類,並創建了以下css。

table.cssTable 
{ 
    border-collapse: collapse; 
} 

table.cssTable tr td 
{ 
    background: Yellow; 
    font-size:Smaller; 
    margin: 0; 
    padding: 0; 
} 

順便說一句,你應該能夠擺脫ItemStyle與字體大小以及這個CSS。

1

我創建了一個小提琴,但實在看不出我怕問題 - http://jsfiddle.net/5rBYb/1/

你可能會想嘗試,雖然這增加你的CSS,這將設置邊框單個像素(我懷疑是什麼導致你的問題)。如果你想隱藏邊框它

table, th, td { border: 1px solid #000; } 

只是更改爲border: 0