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

Formating cells of autogenerated column in radgrid

4 Answers 39 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Purojit
Top achievements
Rank 1
Purojit asked on 14 Jun 2013, 11:11 AM
Hi,
We have a rad grid with auto generated column. In some of the column that are having numeric data in the auto generated columns should be right aligned. We have tried from our end by following your code from the forum. But not been able to achieve desired result.Hence attaching herewith the grid screen shot and .aspx, aspx.cs codes for reference.

.aspx

<%@ Page Language="C#" MasterPageFile="~/Presentationlayer/MasterAdmin.master" AutoEventWireup="true" CodeFile="T_Other_AgentwiseCaseStatement.aspx.cs" Inherits="Presentationlayer_TAgentwiseCaseStatement" Title="Agentwise Case Statement" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Src="MS_Control/MultipleSelection.ascx" TagName="MultipleSelection" TagPrefix="uc1" %>   


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <div>
     
<script type="text/javascript">
    function pageLoad() {
        var grid = $find('<%=RadGrid1.ClientID %>');
        var columns = grid.get_masterTableView().get_columns();
        for (var i = 0; i < columns.length; i++) {
            columns[i].resizeToFit();
        }
    }
</script>
</div>
     <div>
     
     
             <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="True"  
                AllowSorting="true" AllowPaging="True" PageSize="50" Skin="Sunset"
                ShowFooter="True" AllowFilteringByColumn="true" ShowGroupPanel="true"
                 onneeddatasource="RadGrid1_NeedDataSource" OnColumnCreated ="RadGrid1_ColumnCreated" >  <%--OnColumnCreated ="RadGrid1_ColumnCreated --%>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Sunset"></HeaderContextMenu>

                <ExportSettings HideStructureColumns="true" IgnorePaging="true" OpenInNewWindow="true" ExportOnlyData="true" />
                <MasterTableView CommandItemDisplay="Top" ShowGroupFooter="true" GroupLoadMode="Client" TableLayout="Fixed" Width="100%">
                <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true"
                    ShowExportToCsvButton="true" ShowExportToPdfButton="true" ShowAddNewRecordButton="false" />
                                   
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>

<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>

<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
                                   
                </MasterTableView>
                <ClientSettings Selecting-AllowRowSelect="true" AllowKeyboardNavigation="true" EnablePostBackOnRowClick="true" EnableRowHoverStyle="true" AllowGroupExpandCollapse="true" ReorderColumnsOnClient="True" AllowDragToGroup="true" AllowColumnsReorder="True">
<Selecting AllowRowSelect="True"></Selecting>
<Scrolling AllowScroll="false" UseStaticHeaders="true" />
                    <Resizing AllowColumnResize="true" EnableRealTimeResize="true" AllowResizeToFit="true" />
                </ClientSettings>
                <FilterMenu EnableImageSprites="False"></FilterMenu>
            </telerik:RadGrid>  
        </div>
       <div>
        <telerik:RadDock ID="RadDock1" runat="server" Width="380px" Left="10" Top="125"
             Title="Search Companion"  DefaultCommands="ExpandCollapse">
            <ContentTemplate>
                <table>
                    
                    <tr>
                    <td>
                    <table width="90%">
                       <tr>
                        <td> From</td>
                        <td>
                            <telerik:RadDatePicker ID="rdpFrom" runat="server" Width="100"  Culture="(Default)">
                                <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x" Skin="Outlook"></Calendar>

                                <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>

                                <DateInput DisplayDateFormat="dd/MM/yyyy"    AutoPostBack="False"></DateInput>
                            </telerik:RadDatePicker>
                        </td>
                        <td> To</td>
                        <td>
                            <telerik:RadDatePicker ID="rdpTo" runat="server" Width="100"  Culture="(Default)">
                                <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" ViewSelectorText="x" Skin="Outlook"></Calendar>

                                    <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>

                                    <DateInput DisplayDateFormat="dd/MM/yyyy"    AutoPostBack="False"></DateInput>
                            </telerik:RadDatePicker>
                        </td>
                    </tr>
                    </table>
                         
                        </td>
                    </tr>
                     <tr>
                                <td style="z-index:2">
                            <asp:CheckBox ID="CheckBox1" runat="server" Text="All/Selective Agent" Checked="true"
                                AutoPostBack="true" oncheckedchanged="CheckBox1_CheckedChanged" />
                            <br />
                            <uc1:MultipleSelection ID="chkAgent" runat="server" width="400px" />
                        </td></tr>
                  
                    <tr>
                     <td colspan="1">
                    <table width="50%">
                       <tr>
                        
                                <td>
                                    Print Company Name,Address &amp; Report Heading :
                                </td>
                                <td>
                                    <telerik:RadComboBox ID="ddlincldagentcase" runat="server" Width="60PX">
                                        <Items>
                                            <telerik:RadComboBoxItem Text="Yes" Value="Y" />
                                            <telerik:RadComboBoxItem Selected="true" Text="No" Value="N" />
                                        </Items>
                                    </telerik:RadComboBox>
                                </td>
                                </tr>
                                </table>
                                </td>  
                            
                    </tr>
                    </tr>
                    <caption>
                        <br />
                        <br />
                        <tr>
                            <td>
                                <table width="75%">
                                    <tr>
                                        <td align="left">
                                            <telerik:RadButton ID="rbtRpt" runat="server" onclick="rbtRpt_Click"
                                                Text="Print">
                                            </telerik:RadButton>
                                        </td>
                                        <td align="right">
                                            <telerik:RadButton ID="rbtShow" runat="server" onclick="rbtShow_Click"
                                                Text="Preview">
                                            </telerik:RadButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </caption>
                    </tr>
                </table>
            </ContentTemplate>
        </telerik:RadDock>
          <%--<asp:SqlDataSource ID="SqlDataSource2" ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT name as DOCTORNAME FROM doctor"
    runat="server"></asp:SqlDataSource>--%>
    </div>
</asp:Content>


aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using Telerik.Web.UI;


public partial class Presentationlayer_TAgentwiseCaseStatement : System.Web.UI.Page
{
    string conn;

    Binddata btd;

    Misc1 misc;
    string strfltr1;

    public void conns()
    {

        conn = WebConfigurationManager.ConnectionStrings[Convert.ToString(Session["connstring"])].ConnectionString;
    }
    public void reset()
    {

        misc = new Misc1();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Session["connstring"]) == "" || Convert.ToString(Session["connstring"]) == null)
        {
            Response.Redirect("Userauthentication.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                btd = new Binddata();
                ViewState["crdate"] = btd.getsrdate();
                ViewState["fnstartdate"] = Convert.ToString(Session["fnstartdate"]);
                ViewState["mode"] = "0";
                string opdt = "01/04/" + Convert.ToString(ViewState["fnstartdate"]);
                rdpFrom.SelectedDate = DateTime.Parse(ViewState["fnstartdate"].ToString(), CultureInfo.CreateSpecificCulture("en-CA")); ;
                chkAgent.Visible = false;

                rdpTo.SelectedDate = DateTime.Parse(ViewState["crdate"].ToString(), CultureInfo.CreateSpecificCulture("en-CA")); ;
                //ViewState["ch"] = "1";
                //bindgrid();
                //RadGrid1.DataBind();
                //RadDock1.Collapsed = true;
            }
        }
    }
    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if (Convert.ToString(ViewState["mode"]) == "1")
        {
            bindgrid();
        }

        

    }

    private void bindgrid()
    {
        conns();
        SqlConnection con = new SqlConnection(conn);
        SqlCommand com;
        try
        {
            //ViewState["cmpid"] = "1";
            con.Open();
            com = new SqlCommand("Usp_AgentwiseCasestmt", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@ComCode", "");
            com.Parameters.AddWithValue("@FrmDt", DateTime.Parse(rdpFrom.SelectedDate.Value.ToString("yyyy/MM/dd"), CultureInfo.CreateSpecificCulture("en-CA")));
            com.Parameters.AddWithValue("@ToDt", DateTime.Parse(rdpTo.SelectedDate.Value.ToString("yyyy/MM/dd"), CultureInfo.CreateSpecificCulture("en-CA")));
            // com.Parameters.AddWithValue("@ch", Int32.Parse(Convert.ToString(ViewState["ch"])));
            com.Parameters.AddWithValue("@strfltr", Convert.ToString(ViewState["strfltr"]));
            // com.Parameters.AddWithValue("@Pending", ddlonlypending.SelectedValue);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);
            RadGrid1.MasterTableView.Caption = "AgentWise CaseStatement   " + rdpFrom.SelectedDate.Value.ToString("dd/MM/yyyy") + " To" + rdpTo.SelectedDate.Value.ToString("dd/MM/yyyy");
            //int x = com.ExecuteNonQuery();
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        catch (Exception ex)
        {

        }
        finally
        {
            con.Close();

        }

    }
    protected void RadGrid1_PreRender(object sender, System.EventArgs e)
    {
        /


    }
    protected void rbtShow_Click(object sender, EventArgs e)
    {
        ViewState["mode"] = "1";
        if (chkAgent.sValue != "")
        {
            strfltr1 = " and AGENT.CODE IN(" + chkAgent.sValue + ") ";
        }
        else
        {
            strfltr1 = "";
        }
        ViewState["strfltr"] = strfltr1;
        bindgrid();
        RadGrid1.DataBind();
        RadDock1.Collapsed = true;
    }
    protected void rbtRpt_Click(object sender, EventArgs e)
    {

        Response.Write("<script>window.open('T_Other_AgentwiseCaseStatement_rpt.aspx?cmp=" + Convert.ToString(ViewState["cmpid"]) + "&dtfrom=" + rdpFrom.SelectedDate.Value.ToString("dd/MM/yyyy") + "&dtto=" + rdpTo.SelectedDate.Value.ToString("dd/MM/yyyy") + "&strfltr=" + Convert.ToString(ViewState["strfilter"]) + "', '','left=0,height=900,width=1200,top=0,resizable=no,scrollbars=yes,toolbar=no,status=no,target=_blank,location=no');</script>");

    }
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.ColumnType== "GridBoundColumn")
        {
            if (e.Column.UniqueName != "patient" && e.Column.UniqueName != "Agent" && e.Column.UniqueName != "casedt" && e.Column.UniqueName != "CaseNo")
            {
                e.Column.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                e.Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
            }
            if (e.Column.DataType == typeof(String))
            {
                e.Column.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                e.Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

            }
           
            
              
           
        }
       

    }
   
 
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked == true)
        {
            chkAgent.Visible = false;
            
        }
        else
        {
            chkAgent.Visible = true;
            bindagent();
            

        }
    }
    public void bindagent()
    {
        conns();
        SqlConnection con = new SqlConnection(conn);
        SqlCommand com;
        con.Open();
        com = new SqlCommand("Select CODE, NAME from AGENT Order by Name", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        da.Fill(dt);
        chkAgent.CreateCheckBox(dt, "name", "code");
    }
}


Thanks & Regards
Purojit

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 14 Jun 2013, 12:08 PM
Hi,

Please put the following code in the Pre_Render event ,hope this helps you.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       foreach (GridColumn col in RadGrid1.Columns)
       {
            if (col.UniqueName != "patient" && col.UniqueName != "Agent" && col.UniqueName != "casedt" && col.UniqueName != "CaseNo")
          {
             col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
             col.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
         }
         else  if (col.DataType == typeof(String))
         {
             col.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
             col.Column.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
        }     
   }

Thanks,
Shinu

0
Purojit
Top achievements
Rank 1
answered on 15 Jun 2013, 07:29 AM
Hi Shinu,

I have used your code in prerender event...but the result remains the same..

Thanks
Purojit
0
Princy
Top achievements
Rank 1
answered on 17 Jun 2013, 05:22 AM
Hi,
I have tried an example, Please check if this works,put the code into ColumnCreated event.

C#:
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
   {
       foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
       {
           if (col.DataType != typeof(String))        
           {
               col.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
               col.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
           }
       }       
   }
Thanks,
Princy
0
Purojit
Top achievements
Rank 1
answered on 17 Jun 2013, 06:42 AM
Hi Princy,


Thanks a lot , its working..

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