using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace MyCodeSnippet
{
public partial class GV1 : System.Web.UI.Page
{
#region connection string
private int UserId;
private string qry = "select * from UserLogin";
private int UId;
private string Password;
//static string txtpass;
static SqlConnection conn = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
#endregion
#region GetViewState/SetViewState
private void GetViewState()
{
this.UserId = Convert.ToInt32(ViewState["UserId"]);
}
private void SetViewState()
{
ViewState["UserId"] = this.UserId;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.SetData();
//this.UserId = Convert.ToInt32(dvUser.Rows[0].Cells[1].Text);
//SetViewState();
}
}
private void SetData()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM UserLogin", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
grdview.DataSource = ds;
grdview.DataBind();
conn.Close();
}
finally
{
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.UserId = Convert.ToInt32(grdview.DataKeys[e.RowIndex].Value);
GridViewRow row =grdview.Rows[e.RowIndex];
TextBox txtUserId = (TextBox)row.FindControl("txtUserId");
TextBox txtPassword = (TextBox)row.FindControl("txtPassword");
if (txtUserId == null) { return; }
if (txtPassword == null) { return; }
SqlCommand cmd = new SqlCommand(
"update UserLogin set UserId=" + Convert.ToInt32(txtUserId.Text) + ",Password=" + "'" + txtPassword.Text + "'" + " where UserId=" + this.UserId,
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
grdview.EditIndex = -1;
this.SetData();
}
finally
{
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.UserId = Convert.ToInt32(grdview.DataKeys[e.RowIndex].Value);
SqlCommand cmd = new SqlCommand("DELETE FROM UserLogin WHERE UserId =" + this.UserId,
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
this.SetData();
}
finally
{
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdview.PageIndex = e.NewPageIndex;
this.SetData();
}
protected void DropDownListRowsPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr=grdview.BottomPagerRow;
DropDownList ddl = (DropDownList)(gvr.Cells[0].FindControl("DropDownListRowsPerPage"));
grdview.PageSize=int.Parse(ddl.SelectedValue);
//grdview.PageIndex = (ddl.SelectedIndex);
this.SetData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
{
grdview.EditIndex = e.NewEditIndex;
this.SetData();
}
protected void grdview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdview.EditIndex = -1;
this.SetData();
}
protected void grdview_DataBound(object sender, EventArgs e)
{
InitialiseGridViewPagerRow(grdview.TopPagerRow);
InitialiseGridViewPagerRow(grdview.BottomPagerRow);
}
#region Pagination
private void InitialiseGridViewPagerRow(GridViewRow GridViewRow)
{
if (GridViewRow != null)
{
// Check the page index so that we can :
// 1. Disable the 'First' and 'Previous' paging image buttons if paging index is at 0
// 2. Disable the 'Last' and 'Next' paging image buttons if paging index is at the end
// 3. Enable all image buttons if the conditions of 1 and 2 are not satisfied
if (grdview.PageIndex == 0)
{
// Disable 'First' and 'Previous' Paging image buttons
ImageButton firstPageImageButton = GridViewRow.FindControl(
"FirstPageImageButton") as ImageButton;
ImageButton previousPageImageButton = GridViewRow.FindControl(
"PreviousPageImageButton") as ImageButton;
if (firstPageImageButton != null && previousPageImageButton != null)
{
firstPageImageButton.Enabled = false;
previousPageImageButton.Enabled = false;
}
}
else if ((grdview.PageIndex + 1) == grdview.PageCount)
{
// Disable 'Last' and 'Next' Paging image buttons
ImageButton lastPageImageButton = GridViewRow.FindControl(
"LastPageImageButton") as ImageButton;
ImageButton nextPageImageButton = GridViewRow.FindControl(
"NextPageImageButton") as ImageButton;
if (lastPageImageButton != null && nextPageImageButton != null)
{
lastPageImageButton.Enabled = false;
nextPageImageButton.Enabled = false;
}
}
else
{
// Enable the Paging image buttons
ImageButton firstPageImageButton = GridViewRow.FindControl(
"FirstPageImageButton") as ImageButton;
ImageButton previousPageImageButton = GridViewRow.FindControl(
"PreviousPageImageButton") as ImageButton;
ImageButton lastPageImageButton = GridViewRow.FindControl(
"LastPageImageButton") as ImageButton;
ImageButton nextPageImageButton = GridViewRow.FindControl(
"NextPageImageButton") as ImageButton;
if (firstPageImageButton != null && lastPageImageButton != null &&
previousPageImageButton != null && nextPageImageButton != null)
{
firstPageImageButton.Enabled = true;
lastPageImageButton.Enabled = true;
nextPageImageButton.Enabled = true;
previousPageImageButton.Enabled = true;
}
}
// Get the DropDownList found as part of the Pager Row.
// One can then initialise the DropDownList to contain
// the appropriate page settings. Eg. Page Number and
// number of Pages
DropDownList pageNumberDropDownList = GridViewRow.FindControl(
"PageNumberDropDownList") as DropDownList;
Label pageCountLabel = GridViewRow.FindControl(
"PageCountLabel") as Label;
if (pageNumberDropDownList != null && pageCountLabel != null)
{
for (int i = 0; i < grdview.PageCount; i++)
{
int page = i + 1;
ListItem li = new ListItem(page.ToString(), i.ToString());
pageNumberDropDownList.Items.Add(li);
}
pageNumberDropDownList.SelectedIndex = grdview.PageIndex;
pageCountLabel.Text = grdview.PageCount.ToString();
}
}
}
///
/// Handle the SelectedIndexChanged event for the Page Number
/// DropDownList. This allows one to select a page index via a
/// DropDownList.
///
protected void PageNumberDropDownList_OnSelectedIndexChanged(
object sender, EventArgs e)
{
DropDownList pageNumberDropDownList = sender as DropDownList;
if (pageNumberDropDownList != null)
{
if (grdview.Rows.Count > 0)
{
if (pageNumberDropDownList.SelectedIndex < grdview.PageCount ||
pageNumberDropDownList.SelectedIndex >= 0)
{
grdview.PageIndex = pageNumberDropDownList.SelectedIndex;
this.SetData();
}
}
}
}
#endregion
protected void grdview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
//handle insert here
TextBox newUserId = grdview.FooterRow.FindControl("txtNewUserId") as TextBox;
TextBox newpass = grdview.FooterRow.FindControl("txtnewpass") as TextBox;
SqlCommand cmd = new SqlCommand(
"insert into UserLogin values(" + Convert.ToInt32(newUserId.Text) + "," + "'" + newpass.Text + "'" + ")",
conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
grdview.EditIndex = -1;
this.SetData();
}
finally
{
}
}
}
#region sorting
protected void grdview_Sorting(object sender, GridViewSortEventArgs e)
{
string Sort=e.SortExpression;
SortData(Sort);
//GridViewSortExpression = e.SortExpression;
//int pageIndex = gridView.PageIndex;
//gridView.DataSource = SortData(gridView.DataSource as DataTable, false);
//gridView.DataBind();
//gridView.PageIndex = pageIndex;
}
void SortData(string SortExpression)
{
if (ViewState["SortOrder"] == null)
{
ViewState["SortOrder"] = " ASC";
}
else if (ViewState["SortOrder"].ToString() == " ASC")
{
ViewState["SortOrder"] = " DESC";
}
else
{
ViewState["SortOrder"] = " ASC";
}
//string qry = "select * from UserLogin";
qry = qry + " ORDER BY " + SortExpression.ToString() + " " + ViewState["SortOrder"];
SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);
grdview.DataSource = ds;
grdview.DataBind();
conn.Close();
}
finally
{
}
}
#endregion
}
}
No comments:
Post a Comment