Wednesday, April 8, 2009

GridView Edit/Delete and Bind

Html Section:


<asp:GridView ID="grdvBook" runat="server" AutoGenerateColumns="false" OnRowDataBound="grdvBook_RowDataBound"
OnRowEditing="grdvBook_RowEditing" OnRowCancelingEdit="grdvBook_RowCancelingEdit"
OnRowUpdating="grdvBook_RowUpdating" OnRowDeleting="grdvBook_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Author">
<ItemTemplate>
<asp:Label ID="lblBookId" runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblAuthor" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBookId" runat="server" Visible="false"></asp:TextBox>
<asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="125px"></ControlStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="ISBN">
<ItemTemplate>
<asp:Label ID="lblISBN" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtISBN" runat="server"></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="80px"></ControlStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year">
<ItemTemplate>
<asp:Label ID="lblYear" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtYear" runat="server"></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="40px"></ControlStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
</EditItemTemplate>
<ControlStyle Width="125px"></ControlStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edition">
<ItemTemplate>
<asp:Label ID="lblEdition" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdition" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ToolTip="Edit Information" CommandName="Edit"
ImageUrl="~/images/edit_icon.png" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgSave" runat="server" ImageUrl="~/images/AddEvidence.png"
ToolTip="Save" />
<asp:ImageButton ID="imgCancle" runat="server" ImageUrl="~/images/cancel_icon.png"
ToolTip="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDelte" runat="server" ImageUrl="~/images/Cross.png" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



C # Coding Section:
protected void grdvBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblBookId = (Label)e.Row.FindControl("lblBookId");
Label lblAuthor = (Label)e.Row.FindControl("lblAuthor");
Label lblISBN = (Label)e.Row.FindControl("lblISBN");
Label lblYear = (Label)e.Row.FindControl("lblYear");
Label lblTitle = (Label)e.Row.FindControl("lblTitle");
Label lblEdition = (Label)e.Row.FindControl("lblEdition");

ImageButton imgEdit = (ImageButton)e.Row.FindControl("imgEdit");
ImageButton imgDelte = (ImageButton)e.Row.FindControl("imgDelte");

if (lblBookId != null)
lblBookId.Text = ((LINQ.Book)e.Row.DataItem).BookId.ToString();

if (lblAuthor != null)
lblAuthor.Text = ((LINQ.Book)e.Row.DataItem).Author.ToString();

if (lblISBN != null)
lblISBN.Text = ((LINQ.Book)e.Row.DataItem).ISBN.ToString();

if (lblYear != null)
lblYear.Text = ((LINQ.Book)e.Row.DataItem).Year.ToString();

if (lblTitle != null)
lblTitle.Text = ((LINQ.Book)e.Row.DataItem).Title.ToString();

if (lblEdition != null)
lblEdition.Text = ((LINQ.Book)e.Row.DataItem).Edition.ToString();

if (imgEdit != null)
{
imgEdit.CommandArgument = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
imgEdit.CommandName = "Edit";
}

if (imgDelte != null)
{
imgDelte.CommandArgument = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
imgDelte.CommandName = "Delete";
}


TextBox txtBookId = (TextBox)e.Row.FindControl("txtBookId");
TextBox txtAuthor = (TextBox)e.Row.FindControl("txtAuthor");
TextBox txtISBN = (TextBox)e.Row.FindControl("txtISBN");
TextBox txtYear = (TextBox)e.Row.FindControl("txtYear");
TextBox txtTitle = (TextBox)e.Row.FindControl("txtTitle");
TextBox txtEdition = (TextBox)e.Row.FindControl("txtEdition");

ImageButton imgSave = (ImageButton)e.Row.FindControl("imgSave");
ImageButton imgCancle = (ImageButton)e.Row.FindControl("imgCancle");

if (txtBookId != null)
txtBookId.Text = ((LINQ.Book)e.Row.DataItem).BookId.ToString();

if (txtAuthor != null)
txtAuthor.Text = ((LINQ.Book)e.Row.DataItem).Author.ToString();

if (txtISBN != null)
txtISBN.Text = ((LINQ.Book)e.Row.DataItem).ISBN.ToString();

if (txtYear != null)
txtYear.Text = ((LINQ.Book)e.Row.DataItem).Year.ToString();

if (txtTitle != null)
txtTitle.Text = ((LINQ.Book)e.Row.DataItem).Title.ToString();

if (txtEdition != null)
txtEdition.Text = ((LINQ.Book)e.Row.DataItem).Edition.ToString();

if (imgSave != null)
imgSave.CommandName = "Update";

if (imgCancle != null)
imgCancle.CommandName = "Cancel";
}
}

protected void grdvBook_RowEditing(object sender, GridViewEditEventArgs e)
{
grdvBook.EditIndex = e.NewEditIndex;
grdvBook.Columns[5].HeaderText = "Update";
grdvBook.Columns[6].Visible = false;
imgAddBook.Visible = false;
ShowTempBooks();
}

protected void grdvBook_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdvBook.EditIndex = -1;
grdvBook.Columns[5].HeaderText = "Edit";
grdvBook.Columns[6].Visible = true;
if (Convert.ToInt32(((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtBookId")).Text) == 0)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
objBooks.RemoveAt(objBooks.Count - 1);
Session["Books"] = objBooks;
}
imgAddBook.Visible = true;
ShowTempBooks();
}

protected void grdvBook_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool boolError = false;
int bookId = Convert.ToInt32(((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtBookId")).Text);
string author = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtAuthor")).Text;
string ISBN = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtISBN")).Text;
string year = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtYear")).Text;
string title = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtTitle")).Text;
string edition = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtEdition")).Text;
if (author == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.AuthorNameReq, false);
boolError = true;
}
if (ISBN == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.ISBNReq, false);
boolError = true;
}
if (ISBN != string.Empty)
{
try
{
Convert.ToInt32(ISBN);
}
catch (FormatException ex)
{
Utilities.CreateMessageLabel(this, BLL.Constants.ISBNNumeric, false);
boolError = true;
}
}
if (year == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearReq, false);
boolError = true;
}
if (year != string.Empty)
{
try
{
Convert.ToInt32(year);
}
catch (FormatException ex)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearNemeric, false);
boolError = true;
}
if (year.Length > 4)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearLength, false);
boolError = true;
}
}
if (title == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.BookTitleReq, false);
boolError = true;
}
if (edition == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.EditionReq, false);
boolError = true;
}
if (!boolError)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
LINQ.Book objBook = null;
if (bookId > 0)
{
objBook = objBooks.Where(B => B.BookId == bookId).Single();
objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = author;
objBook.ISBN = ISBN;
objBook.Title = title;
objBook.Year = year;
objBook.Edition = edition;
}
else
{
objBook = objBooks.ElementAt(e.RowIndex);
objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = author;
objBook.ISBN = ISBN;
objBook.Title = title;
objBook.Year = year;
objBook.Edition = edition;
}
Session["Books"] = objBooks;
grdvBook.EditIndex = -1;
grdvBook.Columns[5].HeaderText = "Edit";
grdvBook.Columns[6].Visible = true;
imgAddBook.Visible = true;
ShowTempBooks();
}
else
{
grdvBook.EditIndex = e.RowIndex;
}
}

protected void grdvBook_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];

objBooks.RemoveAt(e.RowIndex);
Session["Books"] = objBooks;
ShowTempBooks();
}

protected void imgAddBook_Click(object sender, ImageClickEventArgs e)
{
GenerateBookRow();
}

public void GenerateBookRow()
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
LINQ.Book objBook = new LINQ.Book();

objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = string.Empty;
objBook.ISBN = string.Empty;
objBook.Year = string.Empty;
objBook.Title = string.Empty;
objBook.Edition = string.Empty;
objBooks.Add(objBook);
Session["Books"] = objBooks;
grdvBook.EditIndex = objBooks.Count - 1;
grdvBook.Columns[5].HeaderText = "Update";
grdvBook.Columns[6].Visible = false;
imgAddBook.Visible = false;
ShowTempBooks();
}

No comments:

Post a Comment