1

我有以下一段代码。我没有在我的网格视图中定义任何边界字段。我正在使用我的 aspx.cs 文件中的 sql 查询来检索数据。是否可以调整每列0、1、2的宽度?有什么方法可以调查吗?我已经尝试了很多方法,但仍然无法正常工作。请帮忙!

<asp:GridView ID="surgicalGridView" runat="server"
    CaptionAlign="Top" HorizontalAlign="Justify" 
    DataKeyNames="id" onselectedindexchanged="surgicalGridView_SelectedIndexChanged"
    ToolTip="Excel File Download Tool" CellPadding="4" ForeColor="#333333" 
    GridLines="None" Width="854px">

     <RowStyle BackColor="#E3EAEB" />
       <Columns>
       <asp:CommandField ShowSelectButton="True" SelectText="Download" 
               ControlStyle-ForeColor="Blue">
<ControlStyle ForeColor="Blue"></ControlStyle>
           </asp:CommandField>
       </Columns>
     <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
     <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
     <HeaderStyle   BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
     <EditRowStyle BackColor="#7C6F57" />
     <AlternatingRowStyle BackColor="White" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>
    <br />
4

3 回答 3

7

您可以OnRowDataBoundgridview.

protected void surgicalGridView_RowDataBound(object o, GridViewRowEventArgs e)
{           
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         e.Row.Cells[0].Width = new Unit("200px");
         e.Row.Cells[1].Width = new Unit("400px");
         // and so on
    } 
}

将此添加到您的Gridview标记中

<asp:GridView ...............................
        onrowdatabound="surgicalGridView_RowDataBound">    // just add this event and execute the above code
 </asp:GridView>
于 2013-03-24T14:14:39.327 回答
0

由于网格呈现为table trdt因此您可以将 css 类用于网格。
在那里您可以设置列的宽度。
在您可以使用的类td中,例如td+tdtd+td+td

根据帖子
.NET Gridview 主题示例

查看这些链接。

http://icant.co.uk/csstablegallery/index.php?css=69#r69

http://mattberseth2.com/demo/有很多带有代码下载的gridview 自定义。

寻呼

使用滑块分页

使用排序图标排序

更多主题

http://mattberseth2.com/demo/Default.aspx?Name=A+YUI+DataTable+Styled+GridView&Filter=All http://mattberseth.com/blog/2007/11/5_gridview_themes_based_on_goo.html

替代文字
(来源:mattberseth.com

于 2013-03-24T14:50:13.870 回答
0

我的解决方案如下。我有一个包含 2 个已定义列的网格,其余列是动态绑定的。我不知道为什么用 (e.Row.Cells[0].Width = new Unit("200px");) 设置列不起作用,但我找到了替代方法。此外,我的网格启用了排序,因此链接按钮代码。

const int FirstControl = 0;
const int GriDefinedFieldsCount = 2;

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Header)
     {
         int col = 0;
         foreach (DataColumn dc in SiteManager.Reports.ReportData.Columns)
         {
             if (dc.ColumnName == "Notes")
             {
                 LinkButton lnk = (e.Row.Cells[col + GriDefinedFieldsCount].Controls[FirstControl] as LinkButton);
                 lnk.Width = Unit.Pixel(300);
             }
             col += 1;
         }
     }
于 2015-02-06T16:33:42.780 回答