Hi alhamd,
Refer below code.
SQL
CREATE TABLE [dbo].[Customers_EditTime](
[CustomerId] [INT] NOT NULL,
[Name] [NVARCHAR](20) NULL,
[Country] [NVARCHAR](30) NULL,
[ModifiedDate] [DATETIME] NULL
) ON [PRIMARY]
GO
HTML
<div class="table-responsive">
<asp:GridView ID="GridView1" runat="server" AllowPaging="false" CssClass="table table-striped table-bordered table-hover"
Font-Bold="true" Font-Names="Jameel Noori Nastaleeq" Font-Size="14" AutoGenerateColumns="false"
RowStyle-Wrap="true" HeaderStyle-Wrap="false" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White"
RowStyle-ForeColor="#3A3A3A" OnRowDataBound="OnRowDataBound" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Choose Ans">
<EditItemTemplate>
<asp:DropDownList ID="ddlRemarks" class="form-control" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lbl_Code" runat="server" Text='<%# Bind("CustomerId") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="آپشن ڈی">
<ItemTemplate>
<asp:Label ID="lbl_QuestionD" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="آپشن سی">
<ItemTemplate>
<asp:Label ID="lbl_QuestionC" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
C#
Namespaces
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
C#
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
DateTime modifiedDate = dr["ModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr[3]) : DateTime.MinValue;
LinkButton lnkEdit = e.Row.Cells[0].Controls[0] as LinkButton;
lnkEdit.Enabled = (DateTime.Now - modifiedDate).Hours >= 1;
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label Code = (Label)GridView1.Rows[e.RowIndex].FindControl("lbl_Code");
DropDownList ddlRemarks = GridView1.Rows[e.RowIndex].FindControl("ddlRemarks") as DropDownList;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Customers_EditTime set ModifiedDate=GETDATE() WHERE CustomerId=@No", con))
{
con.Open();
cmd.Parameters.AddWithValue("@No", Code.Text);
cmd.ExecuteNonQuery();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Answer Updated');", true);
}
protected void BindGrid()
{
string name = "Will Smith";
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country, ModifiedDate FROM Customers_EditTime where Name=@Name", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.Visible = false;
Response.Write("No Record Found");
return;
}
}
}
}
}
}
Screenshot