Gridview's Column hide, page Index,Sorting the recods in C# Coding.


Here Explained about GridView's usage.
* How to Hide GridView's Column
* How to set GridView's Page indexing
* How to set sorting the records in Gridview


How to Hide GridView's Column



GridView gridView = (GridView)sender;
GridViewRowCollection growArr = (GridViewRowCollection)gridView.Rows;

foreach (GridViewRow row in growArr)
{
GridView2.HeaderRow.Cells[1].Visible = false;
row.Cells[1].Visible = false;
}

How to use Page indexing in Gridview


GridView1.Visible = false;
Ds = new DataSet();
Ds=DB.GridViewFetch();
GridView2.PageIndex = e.NewPageIndex;
PanelView.Visible =
GridView2.Visible = true;
GridView2.DataSource = Ds.Tables[0];
GridView2.DataBind();


How to sorting the records in Gridview


Declare in public or as you like..
private static String tmpSort = " DESC";

GridView1.Visible = false;
Ds = new DataSet();
Ds = DB.GridViewFetch();
DataView dataView = new DataView();
dataView = Ds.Tables[0].DefaultView;
dataView.Sort = e.SortExpression + (string)((e.SortDirection == SortDirection.Ascending) ? tmpSort : tmpSort);
tmpSort = (tmpSort == " ASC") ? " DESC" : " ASC";
PanelView.Visible =
GridView2.Visible = true;
GridView2.DataSource = dataView;
GridView2.DataBind();

DB connection class Name:DBconnection.cs


Sample Code:



using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public class DBconnection
{
string str;
SqlConnection sqlconn;
SqlCommand cmd;
SqlDataAdapter sqlada;
DataSet ds;

public void connectingstring()
{
string str;
str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
sqlconn = new SqlConnection(str);
sqlconn.Open();
}

public void fetchquery(string str)
{
connectingstring();
sqlada = new SqlDataAdapter(str,sqlconn);
ds = new DataSet();
sqlada.Fill(ds,"xxxx");
}
}
public void connectingstring()
{
string str;
str = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
sqlconn = new SqlConnection(str);
sqlconn.Open();
}

public DataSet GridViewFetch()
{
try
{
DataSet UDS = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand DTcmd = new SqlCommand();
SqlConnection Dtcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
Dtcon.Open();
DTcmd.CommandType = CommandType.Text;
DTcmd.CommandText = "select Employee_Id,Ename,Marital_status,Blood_groub,Address1 from Emp_Table ";
DTcmd.Connection = Dtcon;
da.SelectCommand = DTcmd;
da.Fill(UDS);
Dtcon.Close();
return UDS;
}
catch (Exception ex)
{
throw ex;
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: