Wednesday, October 30, 2013

How to Truncate a String in a Telerik Grid (RadGrid) Column

Hi all, this small blog post is to show how you can truncate a string in a telerik grid when the string is too long to be shown inside the column. Truncation should be done at the ItemDataBound event of telerik grid.

Following is the column template :

<telerik:GridBoundColumn UniqueName="Column1" Visible="true" ShowSortIcon="true">
<ItemStyle Wrap="true" />                   

</telerik:GridBoundColumn>

The ItemDataBound Column will look like this. 

protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{

if (e.Item is GridDataItem)
{
GridDataItem dataBoundItem = e.Item as GridDataItem;
if (dataBoundItem["Column1"].Text.Length > 10)
       {
        dataBoundItem["Column1"].Text = dataBoundItem["Column1"].Text.Substring(0, 10) + "...";
       }
}

}



Hope this helps J
Thank You J

Monday, October 28, 2013

How to Develop a Simple RadGrid Easily

Hi all, in this tutorial I am going to show you how to develop a very simple grid using Telerik. 

First create the structure of the RadGrid using the markups.

<telerik:radscriptmanager id="RadScriptManager1" runat="server"EnablePageMethods="true"/>
<asp:Panel ID="PnlGrid" runat="server">
        <telerik:radgrid runat="server"id="Grid"autogeneratecolumns="false"allowpaging="true" enableviewstate="true"
OnItemCreated ="Grid_ItemCreated"
OnNeedDataSource ="Grid_NeedDataSource">
        <MasterTableViewCommandItemDisplay="None" InsertItemPageIndexAction="ShowItemOnCurrentPage" EditMode="InPlace">
            <Columns>
                <telerik:GridBoundColumn UniqueName="Column1"Visible="true">
                    <ItemStyleWrap="true"/>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Column2"Visible="true">
                    <ItemStyleWrap="true"/>
                </telerik:GridBoundColumn>
            </Columns>
           
            <NoRecordsTemplate>
                <asp:Label ID="lblNoRecords"runat="server"/>
            </NoRecordsTemplate>
        </MasterTableView>
        <PagerStyle Mode="NextPrevAndNumeric"/>
        <ClientSettings EnablePostBackOnRowClick="false">
            <Selecting AllowRowSelect="true"/>
        </ClientSettings>
        <SelectedItemStyleBorderStyle="None"BorderWidth="0px"/>
    </telerik:radgrid>
  
</asp:Panel>

Between the ‘Columns’ tags, you can specify the columns you need using telerik:GridBoundColumn. Here I have used telerik:GridBoundColumn but we can use different other column types. As this is going to be a simple grid, I have only included the telerik:GridBoundColumn.
In this grid, I have specified some events. Let me explain them very briefly.
OnNeedDataSource : When this event fires, the grid will be bound to a given data source. It can be either a SQL data source or a list of objects.
OnItemCreated : I thought of showing you this method, because this method can be really helpful, when you are trying to dynamically change the items in the grid. Inside this method, you can do so many functions, for setting grid headers, data types for columns, data format strings, button names etc.
Then I have included the <NoRecordsTemplate> ,which will used for displaying the grid, when there are no records to be shown in the data source.

Then I have added pager style for the grid. This is a really cool feature in Telerik Grid, because it creates an amazing pagination for the grid.

In the <ClientSettings> allowed the row selection and avoided autopostback on row click. You can do server side functions by enabling this autopostback.

Then I have given a simple style for the selected row.
OK, now we are good with the structure of the RadGrid.  Then let’s see how we can bind the data to the grid and display it. For this example I have used a sample Student list to populate the grid.

Here’s the student class.

public class Student
    {
        public int StudentID { get; set; }
        public stringStudentName { get; set; }

        public Student()
        {
        }

        public Student(intid, string name)
        {
            this.StudentID = id;
            this.StudentName = name;
        }
    }

The code behind of the web page will look like this.

protected void Page_Load(object sender, EventArgse)
{
}

protected voidGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
this.SetDataFieldsForGridColumns();
       this.Grid.DataSource = this.GetStudentsList();
}

protected voidGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item isGridHeaderItem)
{
              GridHeaderItemheader = (GridHeaderItem)e.Item;
              header["Column1"].Text = "Student ID";
              header["Column2"].Text = "Student Name";
}

       if (e.Item is GridNoRecordsItem)
       {
              GridNoRecordsItemnoRecordsItem = e.Item as GridNoRecordsItem;
System.Web.UI.WebControls.LabellblNoRecords = (System.Web.UI.WebControls.Label)noRecordsItem.FindControl("lblNoRecords");
                lblNoRecords.Text = "Sorry, No Records are available for viewing";
       }
 }
private voidSetDataFieldsForGridColumns()
{
GridBoundColumn column1 = this.Grid.MasterTableView.GetColumnSafe("Column1") as GridBoundColumn;
GridBoundColumn column2 = this.Grid.MasterTableView.GetColumnSafe("Column2") as GridBoundColumn;

column1.DataField = "StudentID";
column2.DataField = "StudentName";
}

private List<Student> GetStudentsList()
{
List<Student> students = new List<Student>();
for (inti = 0; i < 20; i++)
       {
       students.Add(new Student(i, string.Format("Std{0}",i)));
       }

       return students;
}


Final Output for the grid will look like this.



In this tutorial, I have explained how to develop a simple static radgrid with populated with a list of data.  


Thanks. J

Monday, October 21, 2013

Telerik RadGrid : Add Sorting to RadGrids EASILY !!!

Hi all, This tutorial will explain how to add sorting for a RadGrid easily. Once you have added the grid to your page, you'll have to add the following codes according to your grid.

First in the aspx or ascx page or controller,

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
    <script type="text/javascript">
   
        function SortColumn(ColumnName)
        {
         var masterTable = $find("<%= Grid1.ClientID %>").get_masterTableView();
          masterTable.sort(ColumnName);
        }
   
    </script>

</telerik:RadCodeBlock>

Then in the code behind, add the following code. 

protected void Grid1_PreRender(object sender, EventArgse)
        {           

            foreach (GridColumncol in Grid1.MasterTableView.RenderColumns)
            {
                foreach (GridHeaderItemheader in Grid1.MasterTableView.GetItems(GridItemType.Header))
                {
                    header[col.UniqueName].Attributes.Add("OnClick", "return SortColumn('" + col.UniqueName + "');");
                }
            }
        }



Hope this will help someone... Thanks :-)





Sunday, October 20, 2013

How to Remove an Assembly from GAC using gacutil in Windows Server 2008 r2 (Without using .net sdk)

Hi all, I happened to remove some unwanted dlls from the GAC in a production server. In this server I didn't have windows SDK installed and therefore I had to use some other method to use gacutil.

The first thing I did was to copy the gacutil and it's configuration file from my local machine. It's located in C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\.  Copy the gacutil Application and gacutil.exe CONFIG file from there and paste it to a folder in your Server. 

When you want to install  an assembly to GAC, copy the above files to your assembly location and open the cmd as an Administrator. Then run the command "gacutil -i <assembly_name>" to install the assembly.

To uninstall an assembly from GAC you have to use "gacutil -u" command. As gacutil is now in a sepereate folder in your server, redirect to that location from the cmd and use the above command. Here you have to use the fully qualified name of the assembly in the GAC. Following is an example. 

gacutil -u "hello,Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789ABCDEF"

When you uninstall the assembly, you'll get an error message including this line. 

"Unable to uninstall: assembly is required by one or more applications"

To solve this follow the instructions as follow. 


  1. cd %systemroot%\assembly\
  2. cd gac_msil (Fom here you may have more that one "gac" directory, so you will have to search within each to find your component.  For me, it is within the "gac_MSIL" directory.)
  3. cd <assembly DLL name>
  4. cd <assembly version number>__<public key token>                                                            
    1. For example: 1.0.23.456__10dbd0fba6984148
  5. erase *.*
  6. Say "y" to are you sure.
  7. cd ..
  8. rd <assembly version number>__<public key token>
  9. cd ..
  10. rd <assembly DLL name>

Tuesday, October 8, 2013

RadGrid Tutorials - How to Change the Header Text of a RadGrid Column, When Gird is in EditMode

When you want to change the header text of a particular column in a Telerik Grid (RadGrd) you can do that easily from the code behind using the ItemCreated event in the grid.

protected void Grid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
            //Set Grid Column Headers
            if (e.Item is GridHeaderItem)
            {
                GridHeaderItem header = (GridHeaderItem)e.Item;
                header["ColumnUniquName"].Text = "Header Titile"; 
            }
}


There may be some requirements from your client, "This column name should be changed when you are inserting an item to the grid".... So how do you do that... simple as this. 

protected void Grid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
            //Set Grid Column Headers
            if (e.Item is GridHeaderItem)
            {
                GridHeaderItem header = (GridHeaderItem)e.Item;
              if (this.Grid.MasterTableView.IsItemInserted)
              {
                 header["ColumnUniquName"].Text = "Header Titile Changed";
              }
              else
              {
                  header["ColumnUniquName"].Text = "Header Titile";
              } 
             }
}

Hope this helps.....