<asp:Label ID="lblMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
<asp:GridView ID="gridSample" runat="server" AutoGenerateColumns="False" ShowFooter="True"
CssClass="grid" OnRowCommand="gridSample_RowCommand"
DataKeyNames="Aid" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowCancelingEdit="gridSample_RowCancelingEdit"
OnRowEditing="gridSample_RowEditing"
OnRowUpdating="gridSample_RowUpdating"
onrowdatabound="gridSample_RowDataBound"
onrowdeleting="gridSample_RowDeleting">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Edit"
CommandArgument=''><img src="../Images/show.png" /></asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
ToolTip="Delete" OnClientClick='return confirm("Are you sure you want to delete this entry?");'
CommandArgument=''><img src="../Images/icon_delete.png" /></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="editGrp" CommandName="Update" ToolTip="Save"
CommandArgument=''><img src="../Images/icon_save.png" /></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="Cancel" ToolTip="Cancel"
CommandArgument=''><img src="../Images/refresh.png" /></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="" ValidationGroup="newGrp" CommandName="InsertNew" ToolTip="Add New Entry"
CommandArgument=''><img src="../Images/icon_new.png" /></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="" CommandName="CancelNew" ToolTip="Cancel"
CommandArgument=''><img src="../Images/refresh.png" /></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Area Name">
<EditItemTemplate>
<asp:TextBox ID="txtAreaName" runat="server" Text='<%# Bind("AreaName") %>' CssClass="" MaxLength="30"></asp:TextBox>
<asp:RequiredFieldValidator ID="valFirstName" runat="server" ControlToValidate="txtAreaName"
Display="Dynamic" ErrorMessage="Area Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAreaName" runat="server" Text='<%# Bind("AreaName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAreaNameNew" runat="server" CssClass="" MaxLength="30"></asp:TextBox>
<asp:RequiredFieldValidator ID="valFirstNameNew" runat="server" ControlToValidate="txtAreaNameNew"
Display="Dynamic" ErrorMessage="Area Name is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category">
<EditItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valCategory" runat="server" ControlToValidate="ddlCategory"
Display="Dynamic" ErrorMessage="Category is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Bind("tbl_AreaGroup.AreaGroupName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCategoryNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valCategoryNew" runat="server" ControlToValidate="ddlCategoryNew"
Display="Dynamic" ErrorMessage="Category is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
lblMessage.Text = "";
}
void BindGrid()
{
using (SmartmaidappEntities context = new SmartmaidappEntities())
{
if (context.tbl_Area.Count() > 0)
{
gridSample.DataSource = context.tbl_Area.ToList();
gridSample.DataBind();
}
else
{
var obj = new List<tbl_Area>();
obj.Add(new tbl_Area());
// Bind the DataTable which contain a blank row to the GridView
gridSample.DataSource = obj;
gridSample.DataBind();
int columnsCount = gridSample.Columns.Count;
gridSample.Rows[0].Cells.Clear();// clear all the cells in the row
gridSample.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
gridSample.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
gridSample.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
gridSample.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
gridSample.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
gridSample.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
}
}
protected void gridSample_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "InsertNew")
{
GridViewRow row = gridSample.FooterRow;
TextBox txtAreaName = row.FindControl("txtAreaNameNew") as TextBox;
DropDownList ddlCategory = row.FindControl("ddlCategoryNew") as DropDownList;
if (txtAreaName != null && ddlCategory != null)
{
using (SmartmaidappEntities context = new SmartmaidappEntities())
{
tbl_Area obj = new tbl_Area();
obj.AreaName = txtAreaName.Text;
obj.AGID = Convert.ToInt32(ddlCategory.SelectedValue);
context.tbl_Area.Add(obj);
context.SaveChanges();
lblMessage.Text = "Added successfully.";
BindGrid();
}
}
}
}
protected void gridSample_RowEditing(object sender, GridViewEditEventArgs e)
{
gridSample.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gridSample_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridSample.EditIndex = -1;
BindGrid();
}
protected void gridSample_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Aid = Convert.ToInt32(gridSample.DataKeys[e.RowIndex].Value);
using (SmartmaidappEntities context = new SmartmaidappEntities())
{
tbl_Area obj = context.tbl_Area.First(x => x.Aid == Aid);
context.tbl_Area.Remove(obj);
context.SaveChanges();
BindGrid();
lblMessage.Text = "Deleted successfully.";
}
}
protected void gridSample_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridSample.Rows[e.RowIndex];
TextBox txtAreaName = row.FindControl("txtAreaName") as TextBox;
DropDownList ddlCategory = row.FindControl("ddlCategory") as DropDownList;
if (txtAreaName != null && ddlCategory != null)
{
using (SmartmaidappEntities context = new SmartmaidappEntities())
{
int Aid = Convert.ToInt32(gridSample.DataKeys[e.RowIndex].Value);
tbl_Area obj = context.tbl_Area.First(x => x.Aid == Aid);
obj.AreaName = txtAreaName.Text;
obj.AGID = Convert.ToInt32(ddlCategory.SelectedValue);
context.SaveChanges();
lblMessage.Text = "Saved successfully.";
gridSample.EditIndex = -1;
BindGrid();
}
}
}
protected void gridSample_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl("ddlCategoryNew") as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl = e.Row.FindControl("ddlCategory") as DropDownList;
}
if (ddl != null)
{
using (SmartmaidappEntities context = new SmartmaidappEntities())
{
ddl.DataSource = context.tbl_AreaGroup.ToList();
ddl.DataTextField = "AreaGroupName";
ddl.DataValueField = "AGID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem(""));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddl.SelectedValue = ((tbl_Area)(e.Row.DataItem)).AGID.ToString();
}
}
}