This is a migrated thread and some comments may be shown as answers.

RadGrid autogenerated column - how size properly

1 Answer 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daryl
Top achievements
Rank 1
Daryl asked on 26 Aug 2014, 09:37 PM


Hello,

 

I have a new situation with radgrids.



The columns are auto generating fine, I actually have two grids on one page.

 

I’m NOT using hardcoded columns in the ASP page.

 

I need to know how to change the widths of the columns in
the C# code behind ideally, if not through javascript.

 

 

Here the ASP code that generates the two columns, which work
fine except for not having control over the width.

 

Also, a lot is commented out.  This code was commented
out due to both incompatibles with our master page and due to redundancies
between the autogenerated code of the two grids.

 

I can switch to bound columns if necessary.  However I
would prefer not to because with this method I can control everything by
programming in Sql Server and then do minimal work in C# / ASP to make the
grid.  However if I must, I’ll accept that as a solution.

 

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Main.Master" AutoEventWireup="true"

   
CodeBehind="Default.aspx.cs" Inherits="SludgeBilling.Pages.Default" %>

<%--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html
xmlns="http://www.w3.org/1999/xhtml">

<head
runat="server">

   
<title></title>

   
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1"
runat="server" />

</head>

<body>

   
<form id="form1" runat="server">

       
<telerik:RadScriptManager ID="RadScriptManager1"
runat="server">

           
<Scripts>

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.Core.js" />

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQuery.js" />

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQueryInclude.js" />

           
</Scripts>

        </telerik:RadScriptManager>

       
<script type="text/javascript">

           
//Put your JavaScript code here.

       
</script> --%>

 

       
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>

<asp:content id="Content2" contentplaceholderid="cphCenter" runat="server">

   
<telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">

       
<script type="text/javascript">

 

           
function
OnClientClose(sender, eventArgs) {

 

 

               
if
(eventArgs.get_argument() == 'true')

                   
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID
%>").ajaxRequest("Reload");

 

               
else

                   
return;

           
}

           
</script>

        
</telerik:RadCodeBlock>

    
<%--  
<telerik:RadAjaxManager ID="RadAjaxManager1"
runat="server">

           
<ClientEvents OnRequestStart="RadAjaxManager1_RequestStart" />

           
<AjaxSettings>

               
<telerik:AjaxSetting AjaxControlID="RadGrid1">

                   
<UpdatedControls>

                       
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />

                   
</UpdatedControls>

               
</telerik:AjaxSetting>

           
</AjaxSettings>

       
</telerik:RadAjaxManager> --%>

       
<div>

    
<%--      
<telerik:RadScriptBlock ID="RadScriptBlock1"
runat="server">

               
<script type="text/javascript">

                   
function gridExport() {

                       
$find("<%= RadGrid1.ClientID
%>").get_masterTableView().exportToExcel();

                   
return false;

               
}

               
function RadAjaxManager1_RequestStart(sender, args) {

                   
if (args.get_eventTarget() == "<%= RadGrid1.ClientID %>")

                       
args.set_enableAjax(false);

               
}

               
</script>

           
</telerik:RadScriptBlock>

            

            <asp:Button
OnClientClick="return gridExport()" ID="Button1"
runat="server" Text="Export To Excel" /> --%>

 

           
<asp:SqlDataSource SelectCommand="select sum([Manifest].[Gallons]) as 'DailyTotal' ,
'<a href=&quot;Pages/Generator/GeneratorDetail.aspx?Id=' +
CONVERT(varchar(36),[Generator].[Id]) + '&quot;><u>' +
[Generator].[Description] + '</u></a>' as Generator ,
[Generator].[SignificantUser] FROM [SludgeBillingSystem].[dbo].[Manifest] inner
join [SludgeBillingSystem].[dbo].[Generator] on [Manifest].[GeneratorId] =
[Generator].[Id] where [Manifest].[DeliveryDate] = cast(convert(varchar(10),
getdate(), 110) as datetime) group by [Generator].[Id],Generator.[Description],
[Generator].[SignificantUser] having sum([Manifest].[Gallons]) >= (SELECT
Convert(Int,REPLACE([AppConfiguration].[Value], ',', '')) as Value FROM
[SludgeBillingSystem].[dbo].[AppConfiguration] where Id =
'9208BD92-2777-400C-8E3E-ED2C791CB13C')" ConnectionString="<%$
ConnectionStrings:SludgeBillingSystem %>" ProviderName="System.Data.SqlClient" ID="DataSource1" runat="server"></asp:SqlDataSource>

 

           
<telerik:RadGrid DataSourceID="DataSource1" ID="RadGrid1" runat="server" Skin="Windows7">

               
<ExportSettings ExportOnlyData="False" FileName="ExportedRadGrid" OpenInNewWindow="False" IgnorePaging="False" />

   
        </telerik:RadGrid>

       
</div>

   
<%--</asp:content>

         
<telerik:RadScriptManager ID="RadScriptManager2"
runat="server">

           
<Scripts>

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.Core.js" />

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQuery.js" />

               
<asp:ScriptReference Assembly="Telerik.Web.UI"
Name="Telerik.Web.UI.Common.jQueryInclude.js" />

           
</Scripts>

        </telerik:RadScriptManager>


        <script
type="text/javascript">

           
//Put your JavaScript code here.

       
</script>

       
<telerik:RadAjaxManager ID="RadAjaxManager2"
runat="server">

           
<ClientEvents OnRequestStart="RadAjaxManager1_RequestStart" />

           
<AjaxSettings>

               
<telerik:AjaxSetting AjaxControlID="RadGrid1">

                   
<UpdatedControls>

           
            <telerik:AjaxUpdatedControl
ControlID="RadGrid1" />

                   
</UpdatedControls>

               
</telerik:AjaxSetting>

           
</AjaxSettings>

       
</telerik:RadAjaxManager> --%>

       
<div>

   
<%--       
<telerik:RadScriptBlock ID="RadScriptBlock2"
runat="server">

               
<script type="text/javascript">

                   
function gridExport() {

                       
$find("<%= RadGrid1.ClientID
%>").get_masterTableView().exportToExcel();

                   
return false;

               
}

               
function RadAjaxManager1_RequestStart(sender, args) {

                   
if (args.get_eventTarget() == "<%= RadGrid1.ClientID %>")

                       
args.set_enableAjax(false);

               
}

               
</script>

           
</telerik:RadScriptBlock>

           
<asp:Button OnClientClick="return gridExport()"
ID="Button2" runat="server" Text="Export To
Excel" /> --%>

 

           
<asp:SqlDataSource SelectCommand="select sum([Manifest].[Gallons]) /
count(distinct([Manifest].[DeliveryDate])) as MonthAvg , '<a
href=&quot;Pages/Generator/GeneratorDetail.aspx?Id=' +
CONVERT(varchar(36),[Generator].[Id]) + '&quot;><u>' +
[Generator].[Description] + '</u></a>' as Generator ,
[Generator].[SignificantUser] FROM [SludgeBillingSystem].[dbo].[Manifest] inner
join [SludgeBillingSystem].[dbo].[Generator] on [Manifest].[GeneratorId] =
[Generator].[Id] where [Manifest].[DeliveryDate] >= DATEADD(mm, DATEDIFF(mm,
0, GETDATE()), 0) and [Manifest].[DeliveryDate] <= DATEADD (dd, -1,
DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0)) group by [Generator].[Id],Generator.[Description],
[Generator].[SignificantUser] having sum([Manifest].[Gallons]) /
count(distinct([Manifest].[DeliveryDate])) >= (SELECT
Convert(Int,REPLACE([AppConfiguration].[Value], ',', '')) as Value FROM
[SludgeBillingSystem].[dbo].[AppConfiguration] where Id =
'9208BD92-2777-400C-8E3E-ED2C791CB13E')" ConnectionString="<%$
ConnectionStrings:SludgeBillingSystem %>" ProviderName="System.Data.SqlClient" ID="DataSource2" runat="server"></asp:SqlDataSource>

 

           
<telerik:RadGrid DataSourceID="DataSource2" ID="RadGrid2" runat="server" Skin="Windows7">

               
<ExportSettings ExportOnlyData="False" FileName="ExportedRadGrid" OpenInNewWindow="False" IgnorePaging="False" />

           
</telerik:RadGrid>

       
</div>

   
</asp:content>

<%--   </form>

</body>

</html>
--%>

 

 

 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Aug 2014, 05:06 AM
Hi Daryl,

You can use the resizeToFit to resizes the column to fit the widest cell's content without wrapping. Another method is as follows:

C#:
void rgrdSample_PreRender(object sender, EventArgs e)
{
    foreach (GridColumn col in rgrdSample.MasterTableView.AutoGeneratedColumns)
    {          
        if (col.ColumnType == "GridNumericColumn")
        {
            col.HeaderStyle.Width = Unit.Pixel(100);
        }
        else if (col.ColumnType == "GridBoundColumn")
        {
            col.HeaderStyle.Width = Unit.Pixel(200);
        }           
    }
    rgrdSample.Rebind();
}

Thanks,
Princy
Tags
Grid
Asked by
Daryl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or