protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
public partial class GridViewDemo : System.Web.UI.Page
{
SqlConnection conn;
SqlDataAdapter adapter;
DataSet ds;
string str="EmpID";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
FillGridView(str);
}
}
protected void FillGridView(string str)
{
string cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select * from tblEmps order by "+str, conn);
ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
finally
{
ds.Dispose();
conn.Dispose();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
str = e.SortExpression;
FillGridView(str);
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex= e.NewPageIndex;
FillGridView(str);
}
}
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmployeeID" />
<asp:BoundField DataField="DepID" HeaderText="DepartmentID" />
<asp:BoundField DataField="Name" HeaderText="Employee Name" />
</Columns>
protected void FillGridView()
{
cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select * from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
}
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
public partial class GridViewDemo : System.Web.UI.Page
{
SqlConnection conn;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;
string cs;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
FillGridView();
}
}
protected void FillGridView()
{
cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select * from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
finally
{
ds.Dispose();
conn.Dispose();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox txtName = (TextBox)row.Cells[2].Controls[0];
TextBox txtGender = (TextBox)row.Cells[3].Controls[0];
TextBox txtSalary = (TextBox)row.Cells[4].Controls[0];
TextBox txtAddress = (TextBox)row.Cells[5].Controls[0];
TextBox txtDepartmentID = (TextBox)row.Cells[6].Controls[0];
string updateQuery = "update tblEmps set name='" + txtName.Text + "',gender='" + txtGender.Text + "',Salary=" + txtSalary.Text + ",Address='" + txtAddress.Text + "',DepID=" + txtDepartmentID.Text + " where EmpID=" + userid;
cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
conn = new SqlConnection(cs);
cmd = new SqlCommand(updateQuery,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
FillGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGridView();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string deleteQuery = "delete from tblEmps where empID="+userid;
cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
conn = new SqlConnection(cs);
cmd = new SqlCommand(deleteQuery, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
FillGridView();
}
}
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gridrow in GridView1.Rows)
{
CheckBox chkDelete = (CheckBox)gridrow.FindControl("CheckBox1");
if (chkDelete.Checked)
{
int empid = Convert.ToInt32(gridrow.Cells[1].Text);
DeleteRecord(empid);
}
}
FillGridView();
}
protected void DeleteRecord(int ID)
{
string deleteQuery = "delete from tblEmps where EmpID="+ID;
cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
conn = new SqlConnection(cs);
cmd = new SqlCommand(deleteQuery,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
Double salaryTotal = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Double sal = (Double)DataBinder.Eval(e.Row.DataItem, "Salary");
salaryTotal += sal;
if (sal >= 50000)
e.Row.BackColor = System.Drawing.Color.LightGreen;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}