vinayak ghanti

vinayak ghanti

  • NA
  • 95
  • 11.7k

Sorting Gridview at binding different datasource

Jul 27 2016 1:16 AM
Hi guys,
 
here is the problem.
 
i have one web user control mediagrid.ascx and i have  3 data source as dataset  which am binding in page load.
 
i have one webform gridviewpage.aspx where am calling this grid in three place's am successfully binding the data and showing in gridview with respect to their datasource
 
problem is i need to do sorting now but am unable to do it its giving error 
 
here is my code: of mediagrid.ascx
 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
else {
}
}
public mediarid()
{
//Create Student List
_students = new List<Student>();
_student = new Student();
_student.RollNumber = 11;
_student.Class = "5Th";
_student.Div = "A";
_student.Name = "Raju";
_students.Add(_student);
_student = new Student();
_student.RollNumber = 13;
_student.Class = "6Th";
_student.Div = "A1";
_student.Name = "Chanki";
_students.Add(_student);
_student = new Student();
_student.RollNumber = 21;
_student.Class = "5Th";
_student.Div = "A3";
_student.Name = "Amit";
_students.Add(_student);
_student = new Student();
_student.RollNumber = 61;
_student.Class = "9Th";
_student.Div = "A4";
_student.Name = "Suresh";
_students.Add(_student);
dataTable = new DataTable("Student");
//Add column
// Add columns.
for (int i = 0; i < 4; i++)
{
dataTable.Columns.Add();
}
// Add rows.
foreach (var item in _students)
{
string[] obj = new string[4];
obj[0] = item.Name;
obj[1] = item.Class;
obj[2] = item.Div;
obj[3] = item.RollNumber.ToString();
dataTable.Rows.Add(obj);
}
}
public void BindData()
{
ds = new DataSet();
ds1 = new DataSet();
DataView DV = new DataView();
if(MediaType=="A1")
{
SqlCommand cmdgrid = new SqlCommand("select * from reg", con);
con.Open();
SqlDataAdapter adacust = new SqlDataAdapter(cmdgrid);
adacust.Fill(ds);
ViewState["dataset"] = ds;
GridView1.DataSource = ds;
ViewState["A1Data"] =(DataSet)GridView1.DataSource;
con.Close();
}
else if (MediaType == "B1")
{
SqlCommand cmdgrid1 = new SqlCommand("select * from Booked", con);
con.Open();
SqlDataAdapter adacust1 = new SqlDataAdapter(cmdgrid1);
adacust1.Fill(ds1);
GridView1.DataSource = ds1;
ViewState["B1Data"] = (DataSet)GridView1.DataSource;
}
GridView1.DataBind();
}
public void SetSortDirections(string Direction)
{
if (Direction == "ASC")
{
_sortDirection = "DESC";
}
else
{
_sortDirection="ASC";
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet ds2 = new DataSet();
DataSet ds3 = new DataSet();
SetSortDirections(SortDirections);
ds2 = (DataSet)ViewState["B1Data"];
if (ds2.Tables[0].Rows.Count != 0)
{
ds2.Tables[0].DefaultView.Sort = e.SortExpression + " " + _sortDirection;
GridView1.DataSource = ds2;
//ds.Clear();
}
ds3 = (DataSet)ViewState["A1Data"];
if (ds3.Tables[0].Rows.Count > 0)
{
ds3.Tables[0].DefaultView.Sort = e.SortExpression + " " + _sortDirection;
GridView1.DataSource = ds3;
}
GridView1.DataBind();
}
 
 
 

Answers (2)