开发者

Dynamic column width

开发者 https://www.devze.com 2022-12-22 19:09 出处:网络
protected void GridView1_DataBound(object sender, EventArgs e) { GridView1.Columns[0].ItemStyle.Width = 400;
protected void GridView1_DataBound(object sender, EventArgs e)
{
    GridView1.Columns[0].ItemStyle.Width = 400;

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

ObjectDataSource1 returns data table , but I can't find any width property there , so I guess there is GridView side option but even on data bound there is like no columns ...

protected void GridView1_DataBound(object sender, EventArgs e)
{
    if (GridView1.Columns.Count!=0)
       开发者_如何学Go GridView1.Columns[0].ItemStyle.Width = 800;

Question : how to set width for column in my grid

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;
}


If you always want the same width, I'd do it with CSS. You can do it like this:

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

And in CSS:

.wide { width: 400px; }

If you set the width on the item style, that css id repeated in the html for every cell in that column, this way at least it's less markup.

If you're using auto generated columns then the actual columns collection is empty and you'll need to hook up to the RowDataBound event like this:

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

In your GridView markup add OnRowDataBound="GridView1_RowDataBound"

0

精彩评论

暂无评论...
验证码 换一张
取 消