bulk edit using asp.net

bulk edit using c#.net + .net 3.5 framework

private bool tableCopied = false;

private DataTable originalDataTable;

protected void Page_Load(object sender, EventArgs e)

{

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

if (!tableCopied)

{

originalDataTable = ((

DataRowView)e.Row.DataItem).Row.Table.Copy();

Session[

"originalValuesDataTable"] = originalDataTable;

tableCopied =

true;

}

}

}

protected void Button1_Click(object sender, EventArgs e)

{

originalDataTable = (

DataTable)Session["originalValuesDataTable"];

foreach (GridViewRow r in GridView1.Rows)

if (IsRowModified(r)) { GridView1.UpdateRow(r.RowIndex, false); }

// Rebind the Grid to repopulate the original values table.

tableCopied =

false;

GridView1.DataBind();

}

protected bool IsRowModified(GridViewRow r)

{

int creditcardID;

string cardType;

string cardNumber;

byte expMonth;

short expYear;

DateTime modifiedDate;

creditcardID =

Convert.ToInt32(GridView1.DataKeys[0].Value);

cardType = ((

TextBox)r.FindControl("CardTypeTextBox")).Text;

cardNumber = ((

TextBox)r.FindControl("CardNumberTextBox")).Text;

expMonth =

Convert.ToByte(((TextBox)r.FindControl("ExpMonthTextBox")).Text);

expYear =

Convert.ToInt16(((TextBox)r.FindControl("ExpYearTextBox")).Text);

modifiedDate =

Convert.ToDateTime(((TextBox)r.FindControl("ModifiedDateTextBox")).Text);

DataRow row =

originalDataTable.Select(

String.Format("CreditCardID = {0}", creditcardID))[0];

if (!cardType.Equals(row["CardType"].ToString())) { return true; }

if (!cardNumber.Equals(row["CardNumber"].ToString())) { return true; }

if (!expMonth.Equals(row["ExpMonth"].ToString())) { return true; }

if (!expYear.Equals(row["ExpYear"].ToString())) { return true; }

if (!modifiedDate.Equals(row["ModifiedDate"].ToString())) { return true; }

return false;

}

protected void btnGeneratePassword_Click(object sender, EventArgs e)

{

int lenthpass = 8;

Label1.Text = GetRandomPasswordUsingGUID(lenthpass);

}

public string GetRandomPasswordUsingGUID(int length)

{

// Get the GUID

string guidResult = System.Guid.NewGuid().ToString();

// Remove the hyphens

guidResult = guidResult.Replace(

"-", string.Empty);

// Make sure length is valid

if (length <= 0 || length > guidResult.Length)

throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

// Return the first length bytes

return guidResult.Substring(0, length);

}

By Bhasker Reddy Guntuka   Popularity  (1364 Views)