Step 1: Write below code in .aspx page
<asp:GridView ID="gvCustDetails"
runat="server"
DataKeyNames="empId"
AutoGenerateColumns="False"
Width="80%"
HorizontalAlign="Center"
OnRowDataBound="gvCustDetails_RowDataBound"
OnRowDeleting="gvCustDetails_RowDeleting"
OnRowEditing="gvCustDetails_RowEditing"
AllowPaging="True"
OnPageIndexChanging="gvCustDetails_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="Sr." >
<ItemTemplate>
<%# ((GridViewRow)Container).RowIndex + 1%>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("empName")
%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblSal" runat="server" Text='<%#Eval("salary")
%>'>asp:Label>
ItemTemplate>
asp:TemplateField>
<asp:ButtonField CausesValidation="True" CommandName="Delete" Text="Delete" />
<asp:ButtonField CausesValidation="True" CommandName="Edit" Text="Edit" />
Columns>
asp:GridView>
Step 2: Write below code in .aspx.cs page
Step3: On page load call BindGridView() function generate
Grid View
Step4: On row data bound event
protected void gvCustDetails_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
//check the
condition with respect to salary and change text color
Label
lblSal = (Label)e.Row.FindControl("lblSal");
switch
(lblSal.Text)
{
case
"35000":
lblSal.ForeColor =
System.Drawing.Color.Green;
break;
case
"45000":
lblSal.ForeColor =
System.Drawing.Color.Blue;
break;
case
"75000":
lblSal.ForeColor =
System.Drawing.Color.Orange;
break;
}
//change
alternate color on mouse over and out
if
(e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#D74584';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#CCCCCC';");
}
else
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFDDE1';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#EEDEF0';");
}
}
}
Step5: On row deleting event
protected void gvCustDetails_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
objDAL.ExecuteNonQuery("DELETE FROM EmployeeMaster WHERE EmpId = "
+ empid + "");
BindGridView();
}
Step6: On row editing event
protected void gvCustDetails_RowEditing(object sender, GridViewEditEventArgs
e)
{
DataTable
dtgetData = objDAL.getDataTable("SELECT * FROM
EmployeeMaster WHERE EmpId = " + empid + "");
if
(dtgetData.Rows.Count > 0)
{
//Set
data to control
//txtEmpName.Text
= dtgetData.Rows[0]["empName"].ToString();
//txtEmpSal.Text
= dtgetData.Rows[0]["empSal"].ToString();
}
}
Step7: on page index changing event
protected void gvCustDetails_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
gvCustDetails.PageIndex =
e.NewPageIndex;
BindGridView();
}
Comments