Monday, 7 July 2014

How to Bind the Dataset to telerik RadGrid in ASP.NET using C#/VB.NET

Before going to use Telerik ,first  download the Telerik dll’s from Telerik site or any another websites.
ASP.NET:
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Bind the Dataset to telerik RadGrid in ASP.NET using C#/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
        <telerik:RadGrid ID="RadGridDetails" GridLines="None" runat="server" AllowSorting="true"
            PageSize="10" AllowPaging="True" OnNeedDataSource="RadGridDetails_NeedDataSource"
            AutoGenerateColumns="False" Width="750px" Skin="Sunset">
            <MasterTableView DataKeyNames="User_ID" HorizontalAlign="NotSet" AutoGenerateColumns="False">
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="User_ID" SortOrder="Descending" />
                </SortExpressions>
                <Columns>
                    <telerik:GridBoundColumn DataField="User_ID" HeaderStyle-Width="135px" HeaderText="User ID"
                        SortExpression="User_ID" UniqueName="User_ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="UserName" HeaderStyle-Width="80px" HeaderText="UserName"
                        SortExpression="UserName" UniqueName="UserName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Country" HeaderStyle-Width="80px" HeaderText="Country"
                        SortExpression="Country" UniqueName="Country">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Gender" HeaderStyle-Width="80px" HeaderText="Gender"
                        SortExpression="Gender" UniqueName="Gender">
                    </telerik:GridBoundColumn>                     
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

C#:
using System.Data.SqlClient;
using Telerik.Web.UI;
using System.Configuration;
using System.Data;
using System.Collections;

public partial class RadGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void RadGridDetails_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select * from user_details", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        RadGridDetails.DataSource = ds;
    }
    protected void BindGrid()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select * from user_details", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        RadGridDetails.DataSource = ds.Tables[0];
        RadGridDetails.DataBind();
    }

VB.NET:
Imports System.Data.SqlClient
Imports Telerik.Web.UI
Imports System.Configuration
Imports System.Data
Imports System.Collections

Partial Public Class RadGrid
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Protected Sub RadGridDetails_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
        Dim cmd As New SqlCommand("Select * from user_details", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        RadGridDetails.DataSource = ds
    End Sub
    Protected Sub BindGrid()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
        Dim cmd As New SqlCommand("Select * from user_details", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        RadGridDetails.DataSource = ds.Tables(0)
        RadGridDetails.DataBind()
    End Sub
End Class

The output of the above code as shown in the below figure.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.