动态列宽

时间:2010-03-15 10:57:23

标签: .net asp.net

protected void GridView1_DataBound(object sender, EventArgs e)
{
    GridView1.Columns[0].ItemStyle.Width = 400;

  <asp:GridView ID="GridView1" runat="server" 
        DataSourceID="ObjectDataSource1"

ObjectDataSource1返回数据表,但我找不到任何宽度属性,所以我猜有GridView端选项,但即使在数据绑定上也没有列......

protected void GridView1_DataBound(object sender, EventArgs e)
{
    if (GridView1.Columns.Count!=0)
        GridView1.Columns[0].ItemStyle.Width = 800;

问题:如何在网格中设置列的宽度

asp:

  <asp:Panel id="Panel1" runat="server" ScrollBars="Auto" style="width:990px; border-style: outset; border-width: 4px;">
  <asp:Label ID="ERROR" runat="server"></asp:Label>
  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
      SelectMethod="GetReport" TypeName="SQF.SQF">
      <SelectParameters>
          <asp:Parameter DefaultValue="2010" Name="Param1" Type="String" />
          <asp:Parameter DefaultValue="1" Name="Param2" Type="Int32" />
          <asp:Parameter DefaultValue="0" Name="Group" Type="Int32" />
          <asp:Parameter DefaultValue="0" Name="DayOfMonth" Type="Int32" />
      </SelectParameters>
  </asp:ObjectDataSource>

  <asp:GridView ID="GridView1" runat="server" 
        DataSourceID="ObjectDataSource1" Width="591px" 
        CellPadding="4" ForeColor="#333333"
        HorizontalAlign="Center" AllowPaging="True"  PageSize="6" 
          onrowdatabound="GridView1_RowDataBound">
        <PagerSettings FirstPageText="&#1055;&#1077;&#1088;&#1074;&#1072;&#1103;" 
            LastPageText="&#1055;&#1086;&#1089;&#1083;&#1077;&#1076;&#1085;&#1103;&#1103;" 
            PageButtonCount="15" position="Bottom" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle CssClass="pagination" HorizontalAlign="Center" 
                VerticalAlign="Middle" 
            Font-Size="14pt" Wrap="True" BackColor="#284775" ForeColor="White"/>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" 
            VerticalAlign="Middle" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
            VerticalAlign="Middle" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" 
            HorizontalAlign="Center" VerticalAlign="Middle" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" 
            Font-Names="Arial" />
        <EditRowStyle BackColor="#999999" HorizontalAlign="Center" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" 
            HorizontalAlign="Center" />
  </asp:GridView>

css:

.wide  {
border:3px solid black;
width:400px;
}
css-content.css (строка 778)
from tr
element.style {
color:#333333;
}
from table#ctl00_ContentPlaceHolder1_GridView1
element.style {
border-collapse:collapse;
color:#333333;
}
from div#ctl00_ContentPlaceHolder1_Panel1
element.style {
border-style:outset;
}
from body
body {
color:#666666;
font-family:Verdana,Arial,Helvetica,sans-serif;
font-size:0.7em;
line-height:1.4em;
}

1 个答案:

答案 0 :(得分:1)

如果你总是想要相同的宽度,我会用CSS做。你可以这样做:

GridView1.Columns[0].ItemCssClass = "wide";

在CSS中:

.wide { width: 400px; }

如果你在项目样式上设置宽度,那么在html中为该列中的每个单元格重复该css id,这样至少可以减少标记。

如果您正在使用自动生成的列,那么实际的列集合是空的,您需要连接到RowDataBound事件,如下所示:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  e.Row.Cells[0].CssClass = "wide";
}

在GridView标记中添加OnRowDataBound="GridView1_RowDataBound"