Paging and Sorting in GridView without using Datasource control

Paging and Sorting in Gridview can be enabled in an ASP.NET by handling the PageIndexChanging and Sorting events of the Gridview

ASP.NET

 

In the web page add the gridview from the toolbox

<asp:GridView
ID=”gridView”
OnPageIndexChanging=”gridView_PageIndexChanging” 

OnSorting=”gridView_Sorting”
runat=”server” />

 

Code Behind

 

I have a function ConvertSortDirection to display the direction of sort.

private string ConvertSortDirection(SortDirection sortDireciton)
{
   string newSortDirection = String.Empty;

   switch (sortDirection)
   {
      case
SortDirection.Ascending:
         newSortDirection = “ASC”;
         break;

      case
SortDirection.Descending:
         newSortDirection = “DESC”;
         break;
   }

   return newSortDirection
}

 

PageIndexChanging

 

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   gridView.PageIndex = e.NewPageIndex;
   gridView.DataBind();
}

 

Sorting

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
   DataTable dataTable = gridView.DataSource as
DataTable;

   if (dataTable != null)
   {
      DataView dataView = new
DataView(dataTable);
      dataView.Sort = e.SortExpression + ” “ + ConvertSortDirectionToSql(e.SortDirection);

      gridView.DataSource = dataView;
      gridView.DataBind();
   }
}

 

Happy coding J

Export GridView To Excel

Today it is common trying to export from Gridview in ASP.NET to excel right. Please find below the piece of code that can help you export Gridview details to excel.

C#

protected
void Button1_Click(object sender, EventArgs e)

    {

        Response.AddHeader(“content-disposition”, “attachment;filename=FileName.xls”);

        Response.Charset = String.Empty;

        Response.ContentType = “application/vnd.xls”;

        System.IO.StringWriter sw = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new
HtmlTextWriter(sw);

        GridView1.RenderControl(hw);

        Response.Write(sw.ToString());

        Response.End();

    }

 
 

Introduction of the cookies in ASP.NET

Introduction

Cookies provide a useful means in Web applications to store user-specific information. For example, when a user visits your site, you can use cookies to store user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier.

 
 

There are three types of classes which allows to work with cookies.

1- HttpCookie  : This cookie property allows to create and Manupulate individual Http cookies in asp.net
2- HttpRequest  : This property allows to access cookies from client machine
3- HttpResponse  : This property allows to save and create cookie in client machine.

HttpResponse and HttpRequest

 
 

How To create cookies in asp.net ?


Response property is used to create cookies.

So here is simple cookie using Response.

 
 

1.Response.Cookies(“Name”).Value = “Jason Bourne”;

 
 

This line is saving the cookie where the cookie name is”Name” and the value is “Jason Bourne” in client machine.

 
 

How to set the time of expiration of cookie

 
 

1.Response.Cookies(“Name”).Expires = DateTime.Now.AddDays(1);

 
 

This line is using to set the expire date of this cookie which is 1 day.

 
 

How to retrieve values from cookies

 
 

1.string
name = this.context.Request.Cookies(“Name”).Value;

 
 

This line of code is retrieving the value from the cookie and stroing the string variable name and the value of cookie is “Jason Bourne” which we were saved before.

If this cookie dosen’t have any value then the string will return empty.

 
 

HttpCookie

HttpCookie is also a very useful class in creating and retrieving the values from cookies.

 
 

1.HttpCookie cook = new
HttpCookie(“Name”);

2.cook.Value      = “Jason Bourne”;

3.cook.Expires    = DateTime.Now.AddDays(1);

4.Response.Cookies.Add(cook);


This line is using to set the expire date of this cookie which is 1 day.


Conclusion

We have learned how to create cookies, Retrieve the values from Cookies, How to set the expire time of cookies etc.

Thank You

 
 

Baimey

ASP.NET Gridview: Making a GridColumn noneditable in Edit mode

Introduction

 

I came across a very common requirement in my project and thought that will have to find share with all.

This
article explains how to make a column, say Primary Key, in a gridview noneditable.

Create a Website

Create a new website File > New > Website

  

Design (.aspx)

Select gridview from the toolbox

Enable edit and add the required bound and template field according to the requirement

 

<asp:GridView
ID=”GridView2″
runat=”server”
AutoGenerateColumns=”False”
>


<Columns>


<asp:CommandField
ShowEditButton=”True”
/>


<asp:BoundField
DataField=”MyProperty”
HeaderText=”Int”
/>


<asp:BoundField
DataField=”MyProperty1″
HeaderText=”String”
/>


</Columns>


</asp:GridView>

 

 

Include three events for the gridview:

OnRowDataBound=”GridView2_RowDataBound”

OnRowEditing=”GridView2_RowEditing”

 

Code Behind (.cs)

Do the databinding from code behind

 

Being a sample here I have bonded a list of objects to the GridView (Can be done according to the requirement).

private
void DataBindGrid()

{


List<Class1> lst = new
List<Class1>();


for (int i = 0; i < 10; i++)

{

lst.Add(new
Class1()

{

MyProperty = i,

MyProperty1 = “String” + i

});

}

GridView2.DataSource = lst;

GridView2.DataBind();

}

 

 

 

 

 

//Add the class objects to the list with two properties

 

 

 

 

//Binding to gridview

 

RowBound Event

 

To make the column 1 readonly on editing add this

Code Behind

protected
void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{

BoundField bound = GridView2.Columns[1] as
BoundField;

      bound.InsertVisible = false;

      bound.ReadOnly = true;

 
 

 }

OR

 

ASPX

 

<asp:BoundField
DataField=”MyProperty”
HeaderText=”Int”
InsertVisible=”False”
ReadOnly=”True”
/>

 

 

Editing Event in codebehind

Along with editing we’ll have to handle

onrowcancelingedit=”GridView2_RowCancelingEdit”


onrowupdating=”GridView2_RowUpdating”
as well.

But here we’ll see about the editing event

 

 

protected
void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView2.EditIndex = -1;

DataBindGrid();

 

 

}

 

Conclusion

Here we have seen how to make a column noneditab;e in edit mode of a Gridview.

Happy Coding J

 

Thanks

Baimey

 

 

 

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!