tags:

views:

27

answers:

1
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)
        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;
}
+1  A: 

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"

Nick Craver
GridView1.Columns[0].ItemCss... // there is no such parameter and even here is no columns :(
nCdy
@nCdy - If you're dynamically generating columns, there won't be any columns, and you need to bind to RowDataBound, I'll update with an example
Nick Craver
that will works slow :( isn't it ? ... doesn't works :(
nCdy
@nCdy - It's not really that slow...when you consider what runs when a page hits the server, this is a very, very, **very** small portion of the code running.
Nick Craver
but it doesn't works )
nCdy
@nCdy - Explain more than "doesn't work", it has no effect, it errors? If it has no effect, did you add the css class? Look at the markup to see that the class attribute is being added.
Nick Craver
css class added and I can see it on target column with firefox addon and I even can change it there and it makes no effect ...
nCdy
@nCdy - did you add `.wide { width: 400px; }` to your css? Your gridview is working, just need to make that css class mean something.
Nick Craver
I have already added it. And I tested it on firefox. And I found it included in my target column but it makes no sense :/
nCdy
@nCdy - Not sure what CSS rules you have interfering, try `.wide { width: 400px; border: solid 1px red; }` and see if it shows up.
Nick Craver
I can change color ... it works ... but I can't change width I add grid code to the question.
nCdy
Solved . The solution was : min-width
nCdy