custom Sorting in a gridView with out using the sorting events

Bijub
Posted by Bijub under ASP.NET category on | Points: 40 | Views : 2632
step1:
// in the aspx page
sorting ascending and descending with sorting image displayed in each column which indicates sorting direction...

<asp:GridView ID="grdManupulate" runat="server"  AutoGenerateColumns="false" DataKeyNames="Id"   Width="800" AlternatingRowStyle-BackColor="AliceBlue" AllowSorting = "false" OnRowCreated="grdManupulate_RowCreated" RowStyle-BackColor = "#ffffff" >
<Columns>
<asp:TemplateField HeaderText = "Name" SortExpression = "Name">
<ItemTemplate>
<asp:HiddenField ID="hid" runat="server" Value = '<%# Bind("Id") %>' />
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' Width="100" ></asp:Label>
</ItemTemplate>
<ItemStyle Width="170px" Height="10px" HorizontalAlign ="Left" VerticalAlign = "Top" />
<HeaderStyle ForeColor="#0099FF" HorizontalAlign="Center" />
<HeaderTemplate>
<asp:LinkButton ID="lnkbName" runat="server" OnClick="lnkbName_Click" ForeColor = "#0099FF">Name</asp:LinkButton>
</HeaderTemplate>
</asp:TemplateField>

</Columns>
<AlternatingRowStyle BackColor="AliceBlue" />
<RowStyle BackColor="White" />

</asp:GridView>


step 2 :
// in the code behind file

// global level declaration
Connection connectionDb = new Connection(); // generate an object of class, connection
DataSet dsRetrieve = new DataSet();
DataSet ds = new DataSet();
DataView dvSort = new DataView();
int gridColumnIndex;

//page load
protected void Page_Load(object sender, EventArgs e)
{ // execute for the first page load
if (!IsPostBack)
{
gridColumnIndex = 5;
ViewState["gridColumnIndex"] = gridColumnIndex;
DataView();// bind sorted data values to the grid view
}
}

// this event is called while binding data in to a row
protected void grdManupulate_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (String.Empty != (string)ViewState["sortColumn"])
{
AddSortImage(e.Row); // bind the image in to the header of gridview based on sorting mode
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFFFE1';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='aliceblue';");
}
else
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFFFE1';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';");
}
}
}

// bind the image in to the header of gridview based on the sorting mode
public void AddSortImage(GridViewRow headerRow)
{
int colIndex = (int)ViewState["gridColumnIndex"];
Image sortImage = new Image();
if (5 == colIndex)
{
return;
}
else
{ // Create the sorting image based on the sort direction.
if ("ASC" == ((string)ViewState["sortDirection"]))
{
sortImage.ImageUrl = "../Images/imagesCA2SMGZG.jpg";
sortImage.AlternateText = "Ascending Order";
sortImage.Height = 20;
sortImage.Width = 20;
}
else
{
sortImage.ImageUrl = "../Images/imagesCAMH0HNC.jpg";
sortImage.AlternateText = "Descending Order";
sortImage.Height = 20;
sortImage.Width = 20;
}
// Add the image to the appropriate header cell.
headerRow.Cells[colIndex].Controls.Add(sortImage);
}
}

// click event of the linkbutton inside the header template in a gridview
protected void lnkbName_Click(object sender, EventArgs e)
{
// grid view is sorted based on the name column and its sort direction
gridColumnIndex = 0;
ViewState["gridColumnIndex"] = gridColumnIndex;
if("Ascending" == ((string)ViewState["SortDirectionName"]))
{
ViewState["sortColumn"] = "Name";
ViewState["sortDirection"] = "";
ViewState["sortDirection"] = "ASC";
ViewState["SortDirectionName"] = null;
ViewState["SortDirectionName"] = "Descending";
}
else
{
ViewState["sortColumn"] = "Name";
ViewState["sortDirection"] = "DESC";
ViewState["SortDirectionName"] = null;
ViewState["SortDirectionName"] = "Ascending";
}
DataView(); //bind sorted data values to the grid view
}

// sort the dataview and bind it to the grid view
public void DataView()
{
ds = connectionDb.Retrieve(); // retrieve the records
//bind sorted data values to the grid view
string str = (string)ViewState["sortDirection"];
if (ViewState["sortColumn"] != null)
{
dvSort = new DataView(ds.Tables[0]);
dvSort.Sort = (string)ViewState["sortColumn"] + " " + (string)ViewState["sortDirection"];
}
else
{
dvSort = ds.Tables[0].DefaultView;
}
grdManupulate.DataSource = dvSort;
grdManupulate.DataBind();
}

Comments or Responses

Login to post response