Thursday, April 16, 2009

Paging,RowDataBound of a gridview with in a gridview.

Html Code:



<asp:GridView ID="grdvCommonCore" runat="server" AutoGenerateColumns="False"
OnRowDataBound="grdvCommonCore_RowDataBound"

Width="100%" AllowPaging="True" PageSize="5">
<AlternatingRowStyle CssClass="GridAltRowStyle" />
<Columns>
<asp:TemplateField HeaderText="Course Name,AU Value and Department">
<ItemTemplate>
<asp:GridView ID="grdvCommonCoreCourses" runat="server"
OnRowDataBound="grdvCommonCoreCourses_RowDataBound"

AllowPaging="true" PageSize="10" AutoGenerateColumns="False"
Width="100%" OnPageIndexChanging="grdvCommonCoreCourses_PageIndexChanging"

OnRowCommand="grdvCommonCoreCourses_RowCommand">
<HeaderStyle CssClass="GridHeaderStyle" />
<RowStyle CssClass="GridRowStyle" />
<AlternatingRowStyle CssClass="GridAltRowStyle" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDelete" runat="server"
ImageUrl="~/images/Cross.png" /></ItemTemplate>

</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="lblCommonCoreCoursesId" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course Name">
<ItemTemplate>
<asp:Label ID="lblCommonCoreCourses" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AU Value">
<ItemTemplate>
<asp:Label ID="lblCommonCoreAUvalue" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department Name">
<ItemTemplate>
<asp:Label ID="lblCommonCoreDepeartment" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Compulsory Courses">
<ItemTemplate>
<asp:Label ID="lblCommonCoreCoursesNo" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total AU Value">
<ItemTemplate>
<asp:Label ID="lblCommonCoreAUValue" runat="server"></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Course Found</EmptyDataTemplate>
<HeaderStyle CssClass="GridHeaderStyle" />
<RowStyle CssClass="GridRowStyle" />
</asp:GridView>

C# Coding Section:
public void ShowProgram()
{
IEnumerable<LINQ.ProgramRequirementMap> objCommonCoreProgramRequirementMap = BLL.ProgramRequirementMap.GetProgramRequirementByProgramId
(Convert.ToInt32(hdnProgramId.Value), Constants.ProgramConstrainType.CommonCore.ToString());
if (objCommonCoreProgramRequirementMap.Count() > 0)
grdvCommonCore.DataSource = objCommonCoreProgramRequirementMap;
else
grdvCommonCore.DataSource = null;
grdvCommonCore.DataBind();
}

protected void grdvCommonCore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grdvCommonCoreCourses = (GridView)e.Row.FindControl("grdvCommonCoreCourses");
Label lblCommonCoreCoursesNo = (Label)e.Row.FindControl("lblCommonCoreCoursesNo");
Label lblCommonCoreAUValue = (Label)e.Row.FindControl("lblCommonCoreAUValue");

List<LINQ.CourseProgramRequirementMap> objCommonCoreProgramRequirementMap = BLL.CourseProgramRequirementMap.GetCoursesAccordingRequirement
((Convert.ToInt32(hdnProgramId.Value)), Constants.ProgramConstrainType.CommonCore.ToString());

if (grdvCommonCoreCourses != null)
{
if (objCommonCoreProgramRequirementMap.Count() > 0)
{
hdvCommonCompulsoryNo.Value = objCommonCoreProgramRequirementMap.Count().ToString();
ShowCommonCourses(grdvCommonCoreCourses);
}
}
if (lblCommonCoreCoursesNo != null)
lblCommonCoreCoursesNo.Text = objCommonCoreProgramRequirementMap.Count() + " Of " + objCommonCoreProgramRequirementMap.Count();

if (lblCommonCoreAUValue != null)
lblCommonCoreAUValue.Text = doubleCommonCoreAUValue.ToString();
}
}

public void ShowCommonCourses(GridView grdvCommonCoreCourses)
{
List<LINQ.CourseProgramRequirementMap> objCommonCoreProgramRequirementMap = BLL.CourseProgramRequirementMap.GetCoursesAccordingRequirement
((Convert.ToInt32(hdnProgramId.Value)), Constants.ProgramConstrainType.CommonCore.ToString());

if (objCommonCoreProgramRequirementMap.Count() > 0)
{
grdvCommonCoreCourses.DataSource = objCommonCoreProgramRequirementMap;
}
else
{
grdvCommonCoreCourses.DataSource = null;
}
doubleCommonCoreAUValue = 0.0;
grdvCommonCoreCourses.DataBind();
}

protected void grdvCommonCoreCourses_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton imgDelete = (ImageButton)e.Row.FindControl("imgDelete");
Label lblCommonCoreCoursesId = (Label)e.Row.FindControl("lblCommonCoreCoursesId");
Label lblCommonCoreCourses = (Label)e.Row.FindControl("lblCommonCoreCourses");
Label lblCommonCoreAUvalue = (Label)e.Row.FindControl("lblCommonCoreAUvalue");
Label lblCommonCoreDepeartment = (Label)e.Row.FindControl("lblCommonCoreDepeartment");

if (imgDelete != null)
{
imgDelete.CommandName = "DeleteCourse";
imgDelete.CommandArgument = ((LINQ.CourseProgramRequirementMap)e.Row.DataItem).CourseId.ToString();
imgDelete.OnClientClick = "return confirm('Are you sure you want to delete the Course from program?It will effect all the programs.');";
}

if (lblCommonCoreCoursesId != null)
lblCommonCoreCoursesId.Text = ((LINQ.CourseProgramRequirementMap)e.Row.DataItem).CourseId.ToString();


if (lblCommonCoreCourses != null)
lblCommonCoreCourses.Text = ((LINQ.CourseProgramRequirementMap)e.Row.DataItem).Course.Abbreviation;

if (lblCommonCoreAUvalue != null)
{
lblCommonCoreAUvalue.Text = BLL.CourseCategoryMap.getAuValue(Convert.ToInt32(lblCommonCoreCoursesId.Text));
doubleCommonCoreAUValue = doubleCommonCoreAUValue + Convert.ToDouble(lblCommonCoreAUvalue.Text.Trim());
}

if (lblCommonCoreDepeartment != null)
lblCommonCoreDepeartment.Text = ((LINQ.CourseProgramRequirementMap)e.Row.DataItem).Course.CourseAbbreviation.Department.DepartmentName;
}
}

protected void grdvCommonCoreCourses_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView grdvCommonCoreCourses = null;
foreach (GridViewRow row in grdvCommonCore.Rows)
{
grdvCommonCoreCourses = (GridView)(row.Cells[0].FindControl("grdvCommonCoreCourses"));
}
grdvCommonCoreCourses.PageIndex = e.NewPageIndex;
ShowCommonCourses(grdvCommonCoreCourses);
tabProgramInfo.ActiveTabIndex = 1;
}

protected void grdvCommonCoreCourses_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("DeleteCourse"))
{
BLL.ProgramRequirement.Delete(Convert.ToInt32(e.CommandArgument.ToString()), Convert.ToInt32(hdnProgramId.Value),
BLL.Constants.ProgramConstrainType.CommonCore.ToString());
ShowProgram();
}
}

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();
}