ronak chittora

ronak chittora

  • NA
  • 31
  • 20.8k

ASP.net GridView

Jan 4 2014 6:29 AM
can anyone help me out.............i want to sort in this code how to do it..... 
 
 
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.IO;
using System.Collections;
using System.Data;
public partial class categorymaster : System.Web.UI.Page
{
DATA_ENGINE de = new DATA_ENGINE();
bool r,val;
MySqlCommand cmd = new MySqlCommand();
MySqlConnection conn = new MySqlConnection();
DataSet ds = new DataSet();
String str;
char flag;
string search;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
setflag();
fun();
//Button5.Visible = true;
}
/*if (Convert.ToInt16(DropDownList1.SelectedItem.Value) == 1)
{
Button6.Visible = false;
}*/
}
protected void setflag()
{
if (DropDownList1.SelectedValue == "2")
{
flag = 'N';
}
else if (DropDownList1.SelectedValue == "1")
{
flag = 'Y';
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.ToString() == "")
{
Label2.Text = "can't be blank";
return;
}
String cmdStr = "insert into category(category_name) values('" + TextBox1.Text.ToString() + "')";
r = de.ExecuteCommand(cmd, cmdStr, conn);
TextBox1.Text = " ";
Label2.Visible = true;
Label2.Text = "Inserted Successfully";
fun();
}
protected void fun()
{
String cmdstr = "select * from category where category_isact='" + flag + "'";
ds = de.GetDataSet(cmdstr, "temp", conn);
GridView1.DataSource = ds;
// GridView1.DataSource = DataTable;
GridView1.DataBind();
}
protected void delete(int id)
{
string strdel = "update category set category_isact='T' WHERE category_id=" + id ;
r = de.ExecuteCommand(cmd, strdel, conn);
if (r)
{
fun();
Label4.Text = "Deleted Successfully!!! ";
}
else
{
Label4.Text = "Not Deleted !!! ";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = " ";
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox2.Text.ToString() != "" || TextBox2.Text.ToString()=="")
{
String cmdstr = "select * from category where category_name like('%" + TextBox2.Text.ToString().Trim().Replace("'","'") + "%')";
ds = de.GetDataSet(cmdstr, "temp", conn);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
Label4.Text = "no record found"; GridView1.DataSource = null;
GridView1.DataBind();
}
}
}
protected void Button4_Click(object sender, EventArgs e)
{
fun();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int strRowNo;
setflag();
if (e.CommandName.ToString() == "Cancel")
{
GridView1.EditIndex = -1;
fun();
}
if (e.CommandName.ToString() == "Update")
{
strRowNo = Convert.ToInt32(e.CommandArgument);
Label lbl = (Label)GridView1.Rows[strRowNo].FindControl("label6");
TextBox txt = (TextBox)GridView1.Rows[strRowNo].FindControl("txtname1");
if (txt.Text != "")
{
updatecategory(lbl.Text, txt.Text);
}
else
{
Label4.Text = "Field Can Not be Blank";
}
}
}
protected void updatecategory(string id, string name)
{
string cmdstr = "UPDATE category SET category_name='" + name + "' WHERE category_id=" + id;
r = de.ExecuteCommand(cmd, cmdstr, conn);
if (r)
{
fun();
Label4.Text = "Updated Successfully";
}
else
{
Label4.Text = "Not Updated";
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
fun();
Label4.Text = "";
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
fun();
}
protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
fun();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
fun();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox2.Text = "";
//Label4.Text = "";
setflag();
if (Convert.ToInt16(DropDownList1.SelectedItem.Value) == 1)
{
Button7.Text = "DeActive";
fun();
}
else if (Convert.ToInt16(DropDownList1.SelectedItem.Value) == 2)
{
Button7.Text = "Active";
fun();
}
}
protected void LnkDelete(object sender, EventArgs e)
{
string id = ((Label)((Control)((Control)((Control)sender).Parent).Parent).FindControl("lbldelid")).Text;
delete(Convert.ToInt32(id));
}
protected void Button7_Click(object sender, EventArgs e)
{
setflag();
if (Button7.Text== "Active")
{
if (conn.State == ConnectionState.Closed) de.OpenConnection(conn);
string gvIDs = "";
bool chk = false;
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("CheckBox1");
if (deleteChkBxItem.Checked)
{
chk = true;
gvIDs += ((Label)gv.FindControl("label5")).Text.ToString() + ",";
}
}
if (gvIDs.Length > 0)
{
if (conn.State == ConnectionState.Closed) de.OpenConnection(conn);
string str1 = "Update category set category_isact='" + "Y" + "' where category_id IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
r = de.ExecuteCommand(cmd, str1, conn);
if (r)
{
fun();
Label4.Text = "Activate Successfully";
}
else
{
Label4.Text = "Not Activate";
}
}
}
else
if (Button7.Text == "DeActive")
{
//Button7.Text = "Active";
if (conn.State == ConnectionState.Closed) de.OpenConnection(conn);
string gvIDs = "";
bool chkBox = false;
foreach (GridViewRow gv in GridView1.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("CheckBox1");
if (deleteChkBxItem.Checked)
{
chkBox = true;
gvIDs += ((Label)gv.FindControl("label5")).Text.ToString() + ",";
}
}
if (gvIDs.Length > 0)
{
if (conn.State == ConnectionState.Closed) de.OpenConnection(conn);
string str1 = "Update category set category_isact='" + "N" + "' where category_id IN (" + gvIDs.Substring(0, gvIDs.LastIndexOf(",")) + ")";
r = de.ExecuteCommand(cmd, str1, conn);
if (r)
{
Label4.Text = "De-Activate Successfully";
fun();
}
else
{
Label4.Text = "Not De-Activate!!";
}
}
}
}
}

Answers (1)