动态设置Gridview中列的宽度

时间:2014-08-23 13:01:31

标签: c# gridview datagridview

我正在尝试动态设置gridview列的宽度,但是当我应用以下代码时,我看到浏览器没有变化。

.aspx代码是:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_OnRowDataBound">
                        <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="File">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />


                    </asp:GridView>

.cs代码是:

            protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        //SET WIDTH OF THE COLUMNS
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Width = 100; //DATE
            e.Row.Cells[1].Width = 100; //CUSTOMER
            e.Row.Cells[2].Width = 100; //DESTINATION
        }
    }

任何人都可以注意到我可能出错的地方吗?

1 个答案:

答案 0 :(得分:0)

请检查..我认为您可能需要采取以下方式

  

e.Row.Cells [0] .Width = Unit.Pixel(500);     e.Row.Cells [1] .Width = Unit.Pixel(500);