Create a table
CREATE TABLE Employee_Leaves
(
EmpId int,
Name varchar(50),
LeaveStatus int
)
1 (Pending), 2 (Solved), 3 (Escalate)
INSERT INTO Employee_Leaves (EmpId,Name,LeaveStatus) VALUES (1,'Mustafa',3)
INSERT INTO Employee_Leaves (EmpId,Name,LeaveStatus) VALUES (2,'Ramkrishna',2)
INSERT INTO Employee_Leaves (EmpId,Name,LeaveStatus) VALUES (3,'Shailesh',1)
<asp:GridView ID="gvEmployeeLeaves" runat="server" CellPadding="0" CellSpacing="0"
AutoGenerateColumns="false" Width="300px" OnRowDataBound = "OnRowDataBound">
<Columns>
<asp:BoundField DataField="EmpId" HeaderStyle-Width="100px" HeaderText="Employee ID" />
<asp:BoundField DataField="Name" HeaderStyle-Width="100px" HeaderText="Name" />
<asp:TemplateField HeaderText="Leave Status" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:DropDownList ID="ddlLeaveStatus" runat="server">
<asp:ListItem Text="Pending" Value="1"></asp:ListItem>
<asp:ListItem Text="Solved" Value="2"></asp:ListItem>
<asp:ListItem Text="Escalate" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblLeaveStatus" Visible = "false" runat="server" Text='<%# Eval("LeaveStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:Button ID="btnUpdate" runat="server" OnClick="OnUpdateClick" Text="Update" CommandArgument='<%#Eval("EmpId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetData();
}
}
protected void OnUpdateClick(object sender, EventArgs e)
{
int empId = int.Parse((sender as Button).CommandArgument);
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
DropDownList ddlLeaveStatus = row.FindControl("ddlLeaveStatus") as DropDownList;
int leaveStatus = int.Parse(ddlLeaveStatus.SelectedItem.Value);
Update(leaveStatus, empId);
}
private void GetData()
{
string connectionString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
SqlConnection sqlconnection = new SqlConnection();
sqlconnection.ConnectionString = connectionString;
sqlconnection.Open();
SqlCommand sqlCommand = new SqlCommand("SELECT EmpId,Name,LeaveStatus FROM Employee_Leaves", sqlconnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
gvEmployeeLeaves.DataSource = sqlDataReader;
gvEmployeeLeaves.DataBind();
sqlconnection.Close();
}
private void Update(int leaveStatus, int empId)
{
string connectionString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
SqlConnection sqlconnection = new SqlConnection();
sqlconnection.ConnectionString = connectionString;
sqlconnection.Open();
SqlCommand sqlCommand = new SqlCommand("UPDATE Employee_Leaves SET LeaveStatus=@L WHERE EmpId=" + empId + " ", sqlconnection);
sqlCommand.Parameters.Add(new SqlParameter("@L", leaveStatus));
sqlCommand.ExecuteNonQuery();
sqlconnection.Close();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlLeaveStatus = (e.Row.FindControl("ddlLeaveStatus") as DropDownList);
ddlLeaveStatus.Items.FindByValue((e.Row.FindControl("lblLeaveStatus") as Label).Text).Selected = true; ;
}
}