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

sorting and paging for radgrid

11 Answers 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Caro
Top achievements
Rank 1
Caro asked on 27 Jun 2012, 07:07 AM
Hi :)

I have a radgrid and I put "allowpaging" and "allowsorting" to true, but it doesn't work :(
Here is my code :

<telerik:RadGrid runat="server" ID="List" 
  AllowPaging="true"
  AllowSorting="True"
  AllowFilteringByColumn="True"
  ShowStatusBar="true"
  AutoGenerateColumns="false"
  OnEditCommand="List_OnEditCommand"
  OnItemCommand="List_Command"
  OnItemDataBound="List_ItemDataBound"
  Visible="false">
   
  <ClientSettings EnablePostBackOnRowClick="true">
     <Selecting AllowRowSelect="True"/>
  </ClientSettings>
 
  <PagerStyle Mode="NumericPages" Position="Bottom"></PagerStyle>
  <GroupingSettings CaseSensitive="false" />
     
  <MasterTableView  ShowHeadersWhenNoRecords="true" DataKeyNames="Id"  ClientDataKeyNames="Id">
 
  <Columns>
       <telerik:GridBoundColumn DataField="Id" UniqueName="Id" HeaderText="Id"
       AutoPostBackOnFilter="true" SortExpression="Id" Visible="false">
       </telerik:GridBoundColumn>
 
       <telerik:GridTemplateColumn DataField="Name" UniqueName="Name" HeaderText="Name"
       DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Name" >
       <ItemTemplate> <%# Eval("Name")%> <img src="/Images/bc_<%# Eval("name")%>.png" alt="" /></ItemTemplate>
       </telerik:GridTemplateColumn>
        
       <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="Firstname"
       DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Firstname" >
       </telerik:GridBoundColumn>
 
      <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="Edit">
      </telerik:GridEditCommandColumn>
  </Columns>
  </MasterTableView>   
</telerik:RadGrid>



protected void button1_Click(object sender, EventArgs e)
        {           
            SqlConnection conn = new SqlConnection();
            SqlCommand comand = new SqlCommand("research", conn);
            comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
            comand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
 
            try
            {
                conn.Open();
                SqlDataReader r = comand.ExecuteReader();
                dt.Load(r);
        
                List.DataBind();
                List.DataSource = dt;
                List.DataBind();
                List.Visible = true;
 
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                    conn.Close();
            }
        }


protected void button2_Click(object sender, EventArgs e)
        {
            foreach (search cs in u.search)
            {
               u.save(u.id, u.ac, u.s);
            }
 
            SqlConnection conn = new SqlConnection();
            SqlCommand comand = new SqlCommand("proc_s", conn);
            comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
            comand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
 
            try
            {
                conn.Open();
                SqlDataReader r = comand.ExecuteReader();
                dt.Load(r);
            
                List.DataSource = dt;
                List.DataBind();
                List.Visible = true;
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                    conn.Close();
            }
        }


Thx

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Jun 2012, 08:34 AM
Hi Caro,

Simple data-binding can be used in simple scenarios,which does not do complex operations. In order to implement these, you should use advanced data-binding through its NeedDataSource event.
Check the following help documentation
Advanced Data-binding (using NeedDataSource event)

Thanks,
Shinu
0
Caro
Top achievements
Rank 1
answered on 27 Jun 2012, 09:01 AM
Hi Shinu

Thanks for your answer, but how to use "Need data source" if I have 2 or 3 buttons and datasource for 1 grid?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Jun 2012, 10:21 AM
Hi Caro,
   <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid2_NeedDataSource"
AllowSorting="true">
            <MasterTableView DataKeyNames="ID">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="Name" DataField="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
        </telerik:RadGrid>
 
 
    <asp:Button ID="Button1" runat="server" Text="Button1"
            onclick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Button2"
            onclick="Button2_Click" />
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       if (!string.IsNullOrEmpty(dataSourceType))
       {
           if (dataSourceType == "1")
           {
               dynamic data = new[] {
               new { ID = 1, Name ="Name1"},
               new { ID = 1, Name = "Name1"},
               new { ID = 1, Name = "Name1"}
           };
               RadGrid2.DataSource = data;
           }
           else if (dataSourceType == "2")
           {
               dynamic data = new[] {
               new { ID = 2, Name ="Name2"},
               new { ID = 2, Name = "Name2"},
               new { ID = 2, Name = "Name2"}
           };
               RadGrid2.DataSource = data;
 
           }
       }
   }
 
 protected void Button1_Click(object sender, EventArgs e)
   {
       dataSourceType = "1";
       RadGrid2.Rebind();
   }
   protected void Button2_Click(object sender, EventArgs e)
   {
 
       dataSourceType = "2";
       RadGrid2.Rebind();
   }
 
 
   public string dataSourceType
   {
       get
       {
           if (ViewState["dataSourceType"] != null)
               return (string)ViewState["dataSourceType"];
           else
               return null;
       }
       set
       {
           ViewState["dataSourceType"] = value;
       }
   }




Thanks,
Jayesh Goyani
0
Caro
Top achievements
Rank 1
answered on 27 Jun 2012, 10:40 AM
Hi Jayesh Goyani,

I'm sorry, I don't really understand and I try to make the same thing, but it doesn't work
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Jun 2012, 12:59 PM
Hello Caro,

Please provide your code.

Thanks,
Jayesh Goyani
0
Caro
Top achievements
Rank 1
answered on 27 Jun 2012, 01:14 PM
My code is on the first post.
What I have to add to it?
I don't understand your code, could you please explain me?

Thanks
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Jun 2012, 01:37 PM
Hello Caro,

<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
       <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />
       <telerik:RadGrid runat="server" ID="List" AllowPaging="true" AllowSorting="True"
           AllowFilteringByColumn="True" ShowStatusBar="true" AutoGenerateColumns="false"
           OnNeedDataSource="List_NeedDataSource" Visible="false">
           <ClientSettings EnablePostBackOnRowClick="true">
               <Selecting AllowRowSelect="True" />
           </ClientSettings>
           <PagerStyle Mode="NumericPages" Position="Bottom"></PagerStyle>
           <GroupingSettings CaseSensitive="false" />
           <MasterTableView ShowHeadersWhenNoRecords="true" DataKeyNames="Id" ClientDataKeyNames="Id">
               <Columns>
                   <telerik:GridBoundColumn DataField="Id" UniqueName="Id" HeaderText="Id" AutoPostBackOnFilter="true"
                       SortExpression="Id" Visible="false">
                   </telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn DataField="Name" UniqueName="Name" HeaderText="Name"
                       DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Name">
                       <ItemTemplate>
                           <%# Eval("Name")%>
                           <img src="/Images/bc_<%# Eval("name")%>.png" alt="" /></ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="Firstname"
                       DataType="System.String" AutoPostBackOnFilter="true" SortExpression="Firstname">
                   </telerik:GridBoundColumn>
                   <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="Edit">
                   </telerik:GridEditCommandColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
protected void Button1_Click(object sender, EventArgs e)
   {
       List.Visible = true;
       dataSourceType = "1";
       List.Rebind();
   }
   protected void Button2_Click(object sender, EventArgs e)
   {
       List.Visible = true;
       dataSourceType = "2";
       List.Rebind();
   }
   protected void List_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       if (!string.IsNullOrEmpty(dataSourceType))
       {
           if (dataSourceType == "1")
           {  
               //SqlConnection conn = new SqlConnection();
               //SqlCommand comand = new SqlCommand("research", conn);
               //comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
               //comand.CommandType = CommandType.StoredProcedure;
               //DataTable dt = new DataTable();
 
               //try
               //{
               //    conn.Open();
               //    SqlDataReader r = comand.ExecuteReader();
               //    dt.Load(r);
 
                
               //    List.DataSource = dt;
 
                
 
               //}
               //catch (SqlException ex)
               //{
               //}
               //finally
               //{
               //    conn.Close();
               //}
 
               // test Code -- Remove Below code
               #region
               dynamic data = new[] {
               new { ID = 1, Name ="Name1"},
               new { ID = 1, Name = "Name1"},
               new { ID = 1, Name = "Name1"}
           };
               List.DataSource = data;
               #endregion
           }
           else if (dataSourceType == "2")
           {
               //foreach (search cs in u.search)
               //{
               //    u.save(u.id, u.ac, u.s);
               //}
 
               //SqlConnection conn = new SqlConnection();
               //SqlCommand comand = new SqlCommand("proc_s", conn);
               //comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
               //comand.CommandType = CommandType.StoredProcedure;
               //DataTable dt = new DataTable();
 
               //try
               //{
               //    conn.Open();
               //    SqlDataReader r = comand.ExecuteReader();
               //    dt.Load(r);
 
               //    List.DataSource = dt;
                
                
               //}
               //catch (SqlException ex)
               //{
               //}
               //finally
               //{
               //    conn.Close();
               //}
 
 
               // test Code -- Remove Below code
               #region
               dynamic data = new[] {
               new { ID = 2, Name ="Name2"},
               new { ID = 2, Name = "Name2"},
               new { ID = 2, Name = "Name2"}
           };
               List.DataSource = data;
               #endregion
 
           }
       }
 
   }


Thanks,
Jayesh Goyani
0
Caro
Top achievements
Rank 1
answered on 28 Jun 2012, 06:24 AM
Hi Jayesh,

Thanks .... but I always don't understand ....

This code : where is it on your code?
How I get the data from database?
What is the "Name1" "Name2"?

foreach (search cs in u.search)
 {
          u.save(u.id, u.ac, u.s);
}
  
SqlConnection conn = new SqlConnection();
SqlCommand comand = new SqlCommand("proc_s", conn);
comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
comand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
  
      try
      {
            conn.Open();
            SqlDataReader r = comand.ExecuteReader();
            dt.Load(r);
  
            List.DataSource = dt;
                 
     }          
 catch (SqlException ex)
 {
 }
 finally
 {
       conn.Close();
  }
0
Shinu
Top achievements
Rank 2
answered on 28 Jun 2012, 06:50 AM
Hi Caro,

You can bind the RadGrid with two button click in one NeedDataSource event as follows.

C#:
string Bind = string.Empty; //setting a variable to distinguish the button
protected void button1_Click(object sender, EventArgs e)
{
    Bind = "research"; // 'Bind' value is set to 'research' for the fist button
    List.Visible = true;
    List.Rebind();
}
protected void button2_Click(object sender, EventArgs e)
{
    Bind = "proc_s"; // 'Bind' value is set to 'research' for the fist button
    List.Visible = true;
    List.Rebind();
}
protected void List_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
   if(Bind=="research") //Checking if clicked button is first.
   {
      SqlConnection conn = new SqlConnection();
      SqlCommand comand = new SqlCommand("research", conn);
      comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
      comand.CommandType = CommandType.StoredProcedure;
      DataTable dt = new DataTable();
      ry
      {
        conn.Open();
        SqlDataReader r = comand.ExecuteReader();
        dt.Load(r);
        List.DataSource = dt;          
      }
      catch (SqlException ex)
      {
      }
      finally
      {
        conn.Close();
      }
   }
   // for the second button click
   else
   {
      foreach (search cs in u.search)
      {
         u.save(u.id, u.ac, u.s);
      }
      SqlConnection conn = new SqlConnection();
      SqlCommand comand = new SqlCommand("proc_s", conn);
      comand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = u_id;
      comand.CommandType = CommandType.StoredProcedure;
      DataTable dt = new DataTable();
      try
      {
         conn.Open();
         SqlDataReader r = comand.ExecuteReader();
         dt.Load(r);
         List.DataSource = dt;
      }
      catch (SqlException ex)
      {
      }
      finally
      {
        conn.Close();
      }
   }
}

Thanks,
Shinu.
0
Caro
Top achievements
Rank 1
answered on 28 Jun 2012, 07:53 AM
Hi Shinu,

Thank you very much,  it works :)


0
Ganesh Pote
Top achievements
Rank 1
answered on 17 Sep 2012, 11:52 AM
Hello,

I am having same kind of problem with Safari browsers. In Safari browsers, grid paging doesn't work. It simply reloads the page without paging effect. I already set AutoPaging property to true but no luck.

Please help me to understand why telerik grid paging not working in Safari browsers.

Thank you!
Ganesh
Tags
Grid
Asked by
Caro
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Caro
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Ganesh Pote
Top achievements
Rank 1
Share this question
or