简体   繁体   English

在rowdatabound事件内设置Gridview的列宽。

[英]Set column width of Gridview inside rowdatabound event.

I have the following piece of code. 我有以下代码。 I did not define any boundfields in my gridview. 我没有在gridview中定义任何边界域。 I am retrieving data using sql queries in my aspx.cs file instead. 我正在使用aspx.cs文件中的sql查询来检索数据。 Is it possible to adjust the width of each column 0, 1, 2? 是否可以调整每列0、1、2的宽度? Are there any ways that I can look into? 有什么我可以研究的方法吗? I have tried a lot of ways but it is still not working. 我已经尝试了很多方法,但是仍然无法正常工作。 Please help! 请帮忙!

<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 />

You can do that on the OnRowDataBound event of the gridview . 您可以在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
    } 
}

Add this to your Gridview Markup 将此添加到您的Gridview标记

<asp:GridView ...............................
        onrowdatabound="surgicalGridView_RowDataBound">    // just add this event and execute the above code
 </asp:GridView>

As grid is rendered as table tr and dt so you can use the css class for the grid. 由于网格以table trdt形式呈现,因此您可以将css类用于网格。
There you can set the width of your columns. 在那里您可以设置列的宽度。
In classes your can use like td , td+td , td+td+td etc 在课堂上,您可以使用tdtd+tdtd+td+td

According to the post 根据帖子
.NET Gridview themes examples .NET Gridview主题示例

Check out these links. 查看这些链接。

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

http://mattberseth2.com/demo/ has lot of gridview customizations with code download. http://mattberseth2.com/demo/具有许多通过代码下载进行的gridview定制。

Paging 分页

Paging With Slider 用滑块分页

Sorting with sort icons 用排序图标排序

Some more themes 其他一些主题

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

替代文字
(source: mattberseth.com ) (来源: mattberseth.com

My solution is below. 我的解决方案如下。 I have a grid that has 2 defined columns and the rest are bound dynamically. 我有一个网格,其中有2个已定义的列,其余的是动态绑定的。 I don't know why setting the column with (e.Row.Cells[0].Width = new Unit("200px");) didn't work, but I found an alternative. 我不知道为什么用(e.Row.Cells [0] .Width = new Unit(“ 200px”);)设置列不起作用,但是我找到了替代方法。 Also, my grid has sorting enabled, therefore the linkbutton code. 另外,我的网格已启用排序,因此启用了linkbutton代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM