簡體   English   中英

如何從數據庫驅動的數據在C#和asp.net中設置GridView的字段寬度

[英]How do i set the field widths of a GridView in C# and asp.net from database driven data

我有來自sql數據庫的數據。 這被轉移到GridView中。

問題在於標頭都具有可變寬度並且看起來確實很奇怪。 我嘗試從“行和標題”設置的“屬性視圖”更改GridView的屬性。 但是,這沒有什么區別。

我假設因為數據是從數據庫驅動的,所以忽略了寬度。

有辦法解決這個問題嗎?

謝謝

aspx:

<asp:GridView ID="gvLatestUpdate" runat="server" CellPadding="4" GridLines="None" BorderStyle="Double" CellSpacing="1">
<RowStyle VerticalAlign="Top"/>
<FooterStyle />
<PagerStyle />
<SelectedRowStyle />
<HeaderStyle BorderWidth="10px"/>
<EditRowStyle/>
<AlternatingRowStyle/>
</asp:GridView>

code behind:

SqlDataReader sdr = Business.DesignStandard.getDSGridInfo(Convert.ToInt32(ddlDateRange.SelectedValue), Convert.ToInt32(UserID), headerName, DirectionOfSort);

gvLatestUpdate.DataSource = sdr;
gvLatestUpdate.DataBind();

如果使用已知的列數填充gridview,則可以嘗試以下方法:

在gridview標記中,為您要從數據庫中檢索的每一列創建綁定字段,並將綁定項的HeaderStyle-Width屬性設置為所需的寬度

<asp:GridView ID="gvEmployees" runat="server" DataKeyNames="EmployeeId" DataSourceID="ObjectDataSource1"
    AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="true" Visible="false" />
        <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="150px" />
        <asp:BoundField DataField="Surname" HeaderText="Surname" HeaderStyle-Width="150px" />
        <asp:BoundField DataField="Age" HeaderText="Age" HeaderStyle-Width="150px" />
        <asp:BoundField DataField="IdNumber" HeaderText="Id number" HeaderStyle-Width="150px" />
        <asp:CheckBoxField DataField="Married" HeaderText="Maritial Status" HeaderStyle-Width="80px" />
    </Columns>
</asp:GridView>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM