Showing posts with label RadGrid. Show all posts
Showing posts with label RadGrid. Show all posts

Tuesday, February 18, 2014

How to Remove Week Numbers from the GridDateTimeColumn/ RadDatePicker in Telerik Grid.

Hi all,

You may have used the date pickers from Telerik controls, either in RadGrid’s GridDateTimeColumn or in RadDatePicker. In both of these scenarios, by default the Telerik control will add a week column from the side of the calendar. Following is the calendar with week columns.




But for some applications we don’t need this column and we will want to remove it. So following is the way that you can achieve that.

The first approach is to remove the week column from the GridDateTimeColumn in RadGrid.

<telerik:GridDateTimeColumn UniqueName="Column2" Visible="true" 
DataFormatString="{0:MM/dd/yyyy}">
                <ItemStyle Wrap="true" />
                <HeaderStyle VerticalAlign="Top" />
</telerik:GridDateTimeColumn>


protected void GridFourColumns_ItemDataBound(object sender, GridItemEventArgs e)
        {           
           RadDatePicker date2 = editItem["Column2"].Controls[0] as RadDatePicker;
           if (date2 != null)
           {
              date2.SharedCalendar.ShowRowHeaders = false;
           }               
           
        }

The following approach is to remove the week column from the RadDatePicker.

 <telerik:RadDatePicker ID="dtpStartDate" runat="server"
      <Calendar ShowRowHeaders="false"></Calendar> 
 </telerik:RadDatePicker> 

Following is the final output without week columns.



Hope this will be helpful.
Thanks J

Thursday, November 14, 2013

Common Issues with Rad Grid : telerik Unable to get property 'documentElement' of undefined or null reference

Hi all,

When developing ASP.Net applications using Telerik Controls, you may have come across this error saying "telerik Unable to get property 'documentElement' of undefined or null reference" which will ask you to debug using the javascript debugger. 

I came across this error when working with Telerik Controls in IE 10. When the browser mode is changed to IE 9, the issue fixed.  But this is not good for your Client.

If you are using a Telerik version which is outdated you will face this problem. One thing you can do is to upgrade your Telerik version.

Next you can fix this issue by adding the following lines in your web page. 

<head runat="server">
    <!-- Mimic Internet Explorer 9 -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />
</head>

Another fix that is discussed in various posts in web is to edit the web.config file as follows. (This fix didn’t work for me and I have also seen others who have not been able to fix by editing the web.config file.) Anyway following is the way you can do that.

<system.webServer>

    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=9"/>
      </customHeaders>
    </httpProtocol>
   
</system.webServer>



Start Developing ASP.Net Applications Using Telerik Controls

Hi all,

Today I’m going to show you how to setup the environment to work with Telerik controls in ASP.Net web applications. In a previous tutorial I showed you how to develop a simple grid using Telerik RadGrid. When developing the grid, we have to add the references and edit the web.config file to avoid the errors that come on our way.

First you have to add the reference to “Telerik.Web.UI.dll”.





















Next the web.config should be edited as following. (Note: I have done this to support a basic rad grid and this may change according to your application)
 <configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
   
    <httpHandlers>
      <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
    </httpHandlers>
    <pages>
      <controls>
        <add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI"/>
      </controls>
    </pages>
   
  </system.web>

  <system.webServer>
    <handlers>
      <add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource"/>
    </handlers>
  </system.webServer>

  <location path="Telerik.Web.UI.WebResource.axd">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
 
</configuration>

Finally you have to add the  “Telerik.Web.UI.dll” to the bin folder of your project.

Now you can start developing ASP.Net applications using Telerik.


Hope this helps J

Friday, November 8, 2013

Common Issues with Rad Grid : Input string was not in a correct format.

If you get this error "Error: Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format." when u click on the edit button of your radgrid data item, what you have to do is first check whether the data-format-strings are correct in your grid. I got this error because I have incorrectly configured the data-format-string for the given object's fields.

Hope this helps, thanks :-)

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 :-)





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.....