1.
How to give serial Number
<asp:TemplateField HeaderText="Sno.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
2. aspx page-just create command button
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" ForeColor="#b2b3b5" runat="server"
CommandName="edit" CommandArgument='<%# Eval("contactid") %>'
CausesValidation="false" >Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" ForeColor="#b2b3b5" runat="server"
CommandName="delete" CommandArgument='<%# Eval("contactid") %>'
CausesValidation="false" >Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
3. aspx.cs page:
protected void grdContactList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you
want to delete?')");
}
}
//edit And delete record
protected void grdContactList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("edit"))
{
hfldContactID.Value = e.CommandArgument.ToString();
appContactList cl = new appContactList();
cl.ContactID = long.Parse(hfldContactID.Value);
cl.fetchContactDetails();
txtFName.Text = cl.FirstName;
txtLName.Text = cl.LastName;
txtEmail.Text = cl.Email;
txtContactNo.Text = cl.ContactNo;
btnAddNew.Text = "Update";
}
if (e.CommandName.Equals("delete"))
{
appContactList cl = new appContactList();
cl.ContactID = long.Parse(e.CommandArgument.ToString());
int raff=cl.deleteContact();
Response.Redirect("~/user/contact");
}
}
Another way to do using rowedit command etc.
1. .aspx page
<asp:TemplateField HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox id="txtremarks" runat="server" Text='<%#Eval("remarks")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRemark" runat="server" Text='<%#Eval("remarks")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField EditText="Add Remark" HeaderText="Add Remark"
ShowEditButton="True" />
2. .aspx.cs
protected void GVPayment_RowEditing(object sender, GridViewEditEventArgs e)
{
GVPayment.EditIndex = e.NewEditIndex;
}
protected void GVPayment_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string
pno=((HiddenField)GVPayment.Rows[e.RowIndex].FindControl("paymentno")).Value;
string uid =
((Label)GVPayment.Rows[e.RowIndex].FindControl("lbluid")).Text;
string remark =
((TextBox)GVPayment.Rows[e.RowIndex].FindControl("txtremarks")).Text;
using (SqlConnection oCon = new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connecti
onString"].ConnectionString))
{
string strInsertQuery = "update nsWeeklyPayment set
remarks=@remarks where uid=@uid and paymentno=@pno";
try
{
SqlCommand oCmd = new SqlCommand(strInsertQuery, oCon);
oCmd.Parameters.AddWithValue("@uid", uid);
oCmd.Parameters.AddWithValue("@remarks",remark);
oCmd.Parameters.AddWithValue("@pno", pno);
oCon.Open();
int chk = oCmd.ExecuteNonQuery();
}
catch (Exception)
{
//throw;
}
finally
{
oCon.Close();
}
}
}
catch (Exception)
{
//throw;
}
GVPayment.EditIndex = -1;
bindgride();
}
protected void GVPayment_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GVPayment.EditIndex = -1;
bindgride();
}
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkedit" runat="server"
CausesValidation="False" CommandName="Edit" CommandArgument='<%# Eval("usrid") %>'
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>