【问题标题】:Set column width of Gridview inside rowdatabound event.在 rowdatabound 事件中设置 Gridview 的列宽。
【发布时间】:2013-03-14 00:36:44
【问题描述】:

我有以下代码。我没有在我的网格视图中定义任何边界字段。我正在使用我的 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 />

【问题讨论】:

    标签: c# asp.net sql gridview


    【解决方案1】:

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

    根据帖子
    .NET Gridview themes examples

    查看这些链接。

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

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

    Paging

    Paging With Slider

    Sorting with sort icons

    更多主题

    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

    【讨论】:

      【解决方案2】:

      我的解决方案如下。我有一个包含 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;
               }
           }
      

      【讨论】:

        【解决方案3】:

        您可以在gridviewOnRowDataBound 事件上执行此操作。

        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>
        

        【讨论】:

        • 每次页面加载都要调用这个方法来改变?我该如何准确执行此方法?谢谢!
        • 它不是一个方法,它是一个gridview 事件,不,你不必在 page_load 或任何地方写它。查看我对答案的更新
        • 很抱歉我刚开始使用gridview!那我到底应该怎么用呢!
        • 好吧....gridview 有一个名为onrowdatabound 的事件,ypu 可以在其中初始化gridview 属性..所以就像我在我的答案中更新了.. 如图所示直接添加该事件复制粘贴该代码..它会为你工作......:)......你可以设置任何你想要的width
        猜你喜欢
        • 2011-05-24
        • 2014-08-30
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-13
        • 1970-01-01
        相关资源
        最近更新 更多