如何在Radgrid列上设置空宽度的最小宽度

时间:2013-04-30 14:31:44

标签: c# javascript asp.net telerik radgrid

我有一个RadGrid,它在PageLoad期间以编程方式添加了列。对于我的网页,总会有至少一个宽度为空的列(HeaderStyle.Width = Unit.Empty),其余列使用特定的像素宽度。所有列都可以调整大小。 (在我的应用程序中,我们正在检索并保存数据库中的这些宽度)网格是可滚动的,因此如果它们超出网格的限制,用户可以滚动而不会调整网格本身的大小。

我遇到的问题是,当用户调整浏览器窗口的大小时,在一定宽度下,空白列最终将减少到0宽度(因为他们正在占用"剩余的& #34; space。是否有任何方法可以将此自动调整大小限制为100px,以确保列可见?

至少有一列必须为null或百分比宽度,否则RadGrid会将所有列默认为百分比宽度,从而使我的特定像素宽度指定无效。

我尝试过的方法是添加一个填充剩余空间的空白列,因为如果宽度为0则无关紧要。但是,这会在调整其他列的大小时导致问题。在这种情况下,空白列应该被消费"直到它为零宽度,然后开始扩展网格大小(或在这种情况下添加滚动条)。相反,它只是被推到一边,保持其宽度。我不知道这种方法是否可行,但我已将其留在代码中,简单地取消注释即可使用它。

这是我的代码:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AA_TEST.aspx.cs" Inherits="Records.AA_TEST" %>


<body id="body" runat="server">
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />   
        <telerik:RadGrid ID="RecordGrid" runat="server" Width="99%" BorderWidth="1px" AutoGenerateColumns="false"
            AllowPaging="true" AllowCustomPaging="true" PageSize="20" AllowSorting="true" AllowFilteringByColumn="false"
            HeaderStyle-Wrap="true" HeaderStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Top"
            ItemStyle-HorizontalAlign="Left" AlternatingItemStyle-HorizontalAlign="Left"
            ItemStyle-VerticalAlign="Top" AlternatingItemStyle-VerticalAlign="Top"
            OnNeedDataSource="RecordGrid_NeedDataSource" OnItemDataBound="RecordGrid_ItemDataBound" >
            <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true" Position="Bottom" />
            <MasterTableView AutoGenerateColumns="false" CommandItemDisplay="None" TableLayout="Fixed"
                EnableHeaderContextMenu="false" AllowCustomSorting="true" AllowMultiColumnSorting="true"
                Width="100%">
            </MasterTableView>
            <ClientSettings AllowColumnsReorder="True" ColumnsReorderMethod="Reorder" ReorderColumnsOnClient="True" ClientMessages-DragToGroupOrReorder="">
                <ClientEvents OnColumnResized="columnResized" />
                <Resizing EnableRealTimeResize="true" AllowColumnResize="true" ClipCellContentOnResize="true" ResizeGridOnColumnResize="true"/>
                <Scrolling AllowScroll="true" SaveScrollPosition="true" UseStaticHeaders="true" />
            </ClientSettings>
            <SortingSettings SortToolTip="" SortedAscToolTip="" SortedDescToolTip="" />
            <GroupingSettings CaseSensitive="false" />
        </telerik:RadGrid>

        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" >
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RecordGrid">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RecordGrid" LoadingPanelID="RadAjaxLoadingPanel1" UpdatePanelRenderMode="Inline" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>

        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">
            //<![CDATA[
            function columnResized(sender, eventArgs) {
                var masterTableView = sender.get_masterTableView();
                var uniqueName = eventArgs.get_gridColumn().get_uniqueName();
                if (!(uniqueName == "DeleteButton")) {
                    var deleteColumn = masterTableView.getColumnByUniqueName("DeleteButton");
                    if (deleteColumn != null) {
                        var deleteColumnIndex = deleteColumn.get_element().cellIndex;
                        deleteColumn.set_resizable(true);
                        masterTableView.resizeColumn(deleteColumnIndex, 30);
                        deleteColumn.set_resizable(false);
                    }
                }
            }
            //]]>
        </script>
        </telerik:RadScriptBlock>  
    </form>
</body>

ASPX.CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;

namespace Records
{
    public partial class AA_TEST : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitRecordGrid();
        }

        protected void InitRecordGrid()
        {
            if (!IsPostBack)
            {
                GridBoundColumn firstNameColumn = new GridBoundColumn();
                firstNameColumn.UniqueName = "FirstName";
                firstNameColumn.DataField = "FirstName";
                firstNameColumn.DataType = typeof(System.String);
                firstNameColumn.HeaderText = "First Name";
                firstNameColumn.Resizable = true;
                firstNameColumn.Reorderable = true;
                firstNameColumn.HeaderStyle.Width = Unit.Pixel(250);
                RecordGrid.Columns.Add(firstNameColumn);

                GridBoundColumn lastNameColumn = new GridBoundColumn();
                lastNameColumn.UniqueName = "LastName";
                lastNameColumn.DataField = "LastName";
                lastNameColumn.DataType = typeof(System.String);
                lastNameColumn.HeaderText = "Last Name";
                lastNameColumn.Resizable = true;
                lastNameColumn.Reorderable = true;
                lastNameColumn.HeaderStyle.Width = Unit.Empty;
                //lastNameColumn.HeaderStyle.Width = Unit.Pixel(250);
                RecordGrid.Columns.Add(lastNameColumn);

                /*
                GridButtonColumn blankColumn = new GridButtonColumn();
                blankColumn.UniqueName = "blank";
                blankColumn.Resizable = false;
                blankColumn.Reorderable = false;
                blankColumn.HeaderStyle.Width = Unit.Empty;
                RecordGrid.Columns.Add(blankColumn);
                */

                GridButtonColumn deleteColumn = new GridButtonColumn();
                deleteColumn.UniqueName = "DeleteButton";
                deleteColumn.HeaderStyle.Width = Unit.Pixel(30);
                deleteColumn.ButtonType = GridButtonColumnType.ImageButton;
                deleteColumn.ImageUrl = "/images/Delete.gif";
                deleteColumn.ItemStyle.CssClass = "delete";
                deleteColumn.CommandName = "Delete";
                deleteColumn.Resizable = false;
                deleteColumn.Reorderable = false;
                RecordGrid.Columns.Add(deleteColumn);
            }
        }

        protected void RecordGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            List<NameRow> nameRows = new List<NameRow>();

            // Filler data
            nameRows.Add(new NameRow("Tyrion", "Lannister"));
            nameRows.Add(new NameRow("Jaime", "Lannister"));
            nameRows.Add(new NameRow("Daenerys", "Targeryen"));
            nameRows.Add(new NameRow("Jon", "Snow"));
            nameRows.Add(new NameRow("Robb", "Stark"));
            nameRows.Add(new NameRow("Benjamin", "Stark"));
            nameRows.Add(new NameRow("Khal", "Drogo"));

            RecordGrid.DataSource = nameRows;
        }

        protected void RecordGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                NameRow user = item.DataItem as NameRow;

                item["FirstName"].Text = user.FirstName;
                item["LastName"].Text = user.LastName;
            }
        }

        public class NameRow
        {
            public string FirstName = "";
            public string LastName = "";

            public NameRow(string first, string last)
            {
                FirstName = first;
                LastName = last;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  GridColumn gridCol = RadGrid1.MasterTableView.GetColumn("columnname");
  gridCol.HeaderStyle.Width = Unit.Pixel(100);   
}