当前位置:编程学习 > C#/ASP.NET >>

关于gridview调整列宽的问题

我是新手,最近在使用gridview时遇到了一个问题,困扰了我好几天。
为了让gridview支持滚动和单独刷新数据,利用ajax,将做好的用户控件(GridView放入UpdatePanel,外面再加上Panel 控件,设置 ScrollBar 为 both。 
问题是不管如何设置,都不能控制每一列的宽度,感觉列宽就是根据header的文字宽度定的,换句话说如果字段名长点,那么这个列就宽些,不能用样式或者程序来自定义,这个问题是不是用了Panel控件造成的?

具体代码:其中MT_Name设置了宽度。
自定义控件部分:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FullGridView.ascx.cs"
    Inherits="control_FullGridView" %>
    
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" GridLines="None" AllowPaging="True" PageSize="5" AllowSorting="True" AutoGenerateColumns="False" >
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"/>
    <Columns>
          asp:BoundField DataField="MT_ID" HeaderText="MT_ID"></asp:BoundField>
        <asp:BoundField DataField="MT_Name" HeaderText="MT_Name">
        <ItemStyle Width="100px" />
         <HeaderStyle Width="100px" />
            </asp:BoundField>
        <asp:BoundField DataField="MT_MacType" HeaderText="MT_MacType"></asp:BoundField>
        <asp:BoundField DataField="MT_IPAddr" HeaderText="MT_IPAddr" SortExpression="MT_IPAddr">
        </asp:BoundField>
        <asp:BoundField DataField="MT_Scan_A" HeaderText="MT_Scan_A"></asp:BoundField>
           </Columns>
       </asp:GridView>

显示部分:其中FullGridView就是自定义的gridview控件
<div id="rightbase" runat="server">  <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
         <asp:Panel ID="GridPanel" runat="server" Height="350px" ScrollBars="Both" Width="780px" Wrap="false">
             <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                  <ContentTemplate>
                      <uc1:FullGridView ID="FullGridView1" runat="server" />
                   </ContentTemplate>
              </asp:UpdatePanel>
            </asp:Panel>
 </div> --------------------编程问答-------------------- 也不一定,如果数据的内容过长,也会将列宽撑大.
--------------------编程问答-------------------- 问题是这样不太美观,数据都挤在一起看不清楚,如何能够固定列宽呢? --------------------编程问答-------------------- <talble style="table-layout:fixed"

然后指定td的宽度.
--------------------编程问答--------------------                     <asp:GridView ID="GridView" CssClass="GridViewStyle" runat="server"
                        OnRowDataBound="GridView_RowDataBound" AutoGenerateColumns="False" 
                        onrowcreated="GridView_RowCreated">
                        <FooterStyle CssClass="GridViewFooterStyle" />
                        <HeaderStyle CssClass="GridViewHeaderStyle" />
                        <Columns>
                            <asp:TemplateField HeaderText="序号">
                                <ItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%#Container.DataItemIndex+1+(AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="供应商确认订单">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkAgree" runat="server" Checked ='<%# Eval("确认订单") %>' />
                                </ItemTemplate>
<%--                                此处控制列宽--%>
                               <ItemStyle Width="90px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="订单号">
                                <ItemTemplate>
                                    <asp:Label ID="lblOrderFormID" runat="server" Text='<%# Bind("订单号") %>' Visible="true"></asp:Label>
                                </ItemTemplate>
<%--                                此处控制列宽--%>
                                <ItemStyle Width="160px" />
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="供应商" DataField="供应商"/>
                            <asp:BoundField HeaderText="图号型号" DataField="图号型号" />
                            <asp:BoundField HeaderText="物品名称" />
                            <asp:BoundField HeaderText="规格" />
                            <asp:BoundField HeaderText="订货数量" />
                        </Columns>
                    </asp:GridView>
--------------------编程问答-------------------- 见我上面的控制列宽部分,其中有两列是控制了列宽的,如:供应商确认订单、订单号 --------------------编程问答-------------------- http://blog.csdn.net/michaelgong/article/details/6587271
Lz 看看这个吧。。。有关GirdView操作,设置都有。。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,