Hi,

I am create one application in asp.net. I have to use datagrid view in this application. I want to sort Gender. I am use the template field in datagrid view.

<asp:LinkButton ID="LnkBtnGender" runat="server" CommandName="Sort" CommandArgument="Gender">Gender</asp:LinkButton>

plj help me..

Handle the Sorting[/b[ event of GridView.

protected void grdStudentInfo_Sorting(object sender, GridViewSortEventArgs e)
    {
      
     
        string sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {

            GridViewSortDirection = SortDirection.Descending;

            string DESCENDING = "DESC"; 

            SortGridView(sortExpression, DESCENDING);

        }

        else
        {

            GridViewSortDirection = SortDirection.Ascending;

            string ASCENDING = "ASC"; 

            SortGridView(sortExpression, ASCENDING);

        }

       

    }

    private void SortGridView(string sortExpression, string direction)
    {
         ViewState["sortExpression"] = sortExpression + " " + direction;
         FillGrid();

    }

protected void grdStudentInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        { 
            grdStudentInfo.PageIndex = e.NewPageIndex;
            FillGrid();
        }
        catch (Exception) { }

 public void FillGrid()
    {
        try
        {
            DataSet ds = new DataSet();
            ds = BStudentInfo.GetStudentInfo(CourseClassId,"" ,"" , InstituteId,strType);//txtRegNo.Text txtStudentName.Text
            BindData(ds);
        }
        catch (Exception)
        { }
    }
    private void BindData(DataSet ds)
    {
        try
        {
            DataView dv;

            if (ds.Tables[0].Rows.Count > 0)
            {
               if (ViewState["sortExpression"] != null)
                {
                    dv = new DataView(ds.Tables[0]);
                }
                else
                {
                    dv = ds.Tables[0].DefaultView;
                }
               
                grdStudentInfo.DataSourceID = "";
                try
                { 

                    if (strSearchCri != "")
                    {
                        dv.RowFilter = "RegNo like '%" + strSearchCri + "%' or AdmissionDates like '%" + strSearchCri + "%' or StudentName like'%" + strSearchCri + "%' or RollNo like '%" + strSearchCri + "%' or Gender like '%" + strSearchCri + "%' or ReligionName like '%" + strSearchCri + "%' or CasteName like '%" + strSearchCri + "%' or CategoryName like '%" + strSearchCri + "%' or PermanentAdd like '%" + strSearchCri + "%' or LocalAdd like '%" + strSearchCri + "%' or MothersName like '%" + strSearchCri + "%' or PlaceOfBirth like '%" + strSearchCri + "%' or LandLineNo like '%" + strSearchCri + "%' or MobileNo like '%" + strSearchCri + "%' or EmergerencyContactName like '%" + strSearchCri + "%' or EmergencyPhoneNo like '%" + strSearchCri + "%' or FathersCellNo like '%" + strSearchCri + "%' or FathersWorkPhoneNo like '%" + strSearchCri + "%' or FathersEmail like '%" + strSearchCri + "%' or MothersCellNo like '%" + strSearchCri + "%' or MothersWorkPhoneNo like '%" + strSearchCri + "%' or PreviousSchool like '%" + strSearchCri + "%' or ReasonForLeavingPreviousSchool like '%" + strSearchCri + "%' or BloodGroup like '%" + strSearchCri + "%' or StudentEmail like '%" + strSearchCri + "%' or LibraryCategory like '%" + strSearchCri + "%' or ICardNo like '%" + strSearchCri + "%' ";
                        
                        if (strHeaderRegNo != "" || strHeaderAddDate != "" || strHeaderStudentName != "" || strHeaderRollNo != "" || strHeaderGender != "" || strHeaderReligion != "" || strHeaderCaste != "" || strHeaderCategory != "" || strHeaderAddress != "")
                        {
                            dv.RowFilter = "RegNo like '%" + strHeaderRegNo + "%' and AdmissionDates like '%" + strHeaderAddDate + "%' and StudentName like'%" + strHeaderStudentName + "%' and RollNo like '%" + strHeaderRollNo + "%' and Gender like '%" + strHeaderGender + "%' and ReligionName like '%" + strHeaderReligion + "%' and CasteName like '%" + strHeaderCaste + "%' and CategoryName like '%" + strHeaderCategory + "%' and PermanentAdd like '%" + strHeaderAddress + "%'";
                        }
                    }
                    else
                    {
                        if (strHeaderRegNo != "" || strHeaderAddDate != "" || strHeaderStudentName != "" || strHeaderRollNo != "" || strHeaderGender != "" || strHeaderReligion != "" || strHeaderCaste != "" || strHeaderCategory != "" || strHeaderAddress!="")
                        {
                            dv.RowFilter = "RegNo like '%" + strHeaderRegNo + "%' and AdmissionDates like '%" + strHeaderAddDate + "%' and StudentName like'%" + strHeaderStudentName + "%' and RollNo like '%" + strHeaderRollNo + "%' and Gender like '%" + strHeaderGender + "%' and ReligionName like '%" + strHeaderReligion + "%' and CasteName like '%" + strHeaderCaste + "%' and CategoryName like '%" + strHeaderCategory + "%' and PermanentAdd like '%" + strHeaderAddress + "%'";
                        }
                    }
                }
                catch (Exception) { }

                if (ViewState["sortExpression"] != null)
                {                   
                    dv = new DataView(ds.Tables[0]);
                    dv.Sort =(string)ViewState["sortExpression"];
                }
                

                if(dv.Count<=0)
                {
                    EmptyGrid();
                }
                else
                {
                    grdStudentInfo.DataSource = dv;
                    grdStudentInfo.DataBind();
                }

                txtSearchCriteria.Text = strSearchCri;

                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderStudentName")).Text = strHeaderStudentName;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderRegNo")).Text = strHeaderRegNo;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderAddDate")).Text = strHeaderAddDate;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderRollNo")).Text = strHeaderRollNo;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderGender")).Text = strHeaderGender;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderReligion")).Text = strHeaderReligion;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderCaste")).Text = strHeaderCaste;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtHeaderCategory")).Text = strHeaderCategory;
                ((TextBox)grdStudentInfo.HeaderRow.FindControl("txtPermanentAdd")).Text = strHeaderAddress;


               
              

                lblRecordCount.Text = dv.Count.ToString();

                if (dv.Count > 0)
                {
                    DisplaySerialNumber();
                }
               
                try
                {
                    displayTotalRecPerPage();
                }
                catch (Exception)
                {
                }

            }
            else
            {
                EmptyGrid();
            }
        }
        catch (Exception) { }

Hi,
In my web application sort string data correctly.but rollno is integer value they do not sort correctly.plz how to sort integer values in datagrid view in Asp.Net
plj help me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.