Read the previous code snippet to edit/update in a GridView here. I have disabled AutoGenerateColumns and declared the columns explicitly.
In the below code snippet, I have made the delete button to appear as first column.
ASPX
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#010101" BorderStyle="Groove" BorderWidth="1px" CellPadding="4" OnRowDeleting="gvUsers_RowDeleting">
<Columns>
<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
<asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" />
<asp:BoundField DataField="DateOfBirth" HeaderText="DOB" ReadOnly="True" />
<asp:BoundField DataField="WebAddress" HeaderText="WebSite" ReadOnly="True" />
<asp:BoundField DataField="Blog" HeaderText="Blog" ReadOnly="True" />
<asp:BoundField DataField="Address1" HeaderText="Address1" ReadOnly="True" />
<asp:BoundField DataField="Address2" HeaderText="Address2" ReadOnly="True" />
<asp:BoundField DataField="City" HeaderText="City" ReadOnly="True" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" />
<asp:BoundField DataField="Country" HeaderText="Country" ReadOnly="True" />
<asp:BoundField DataField="PIN" HeaderText="PIN" ReadOnly="True"
</Columns>
</asp:GridView>
CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUsers();
}
}
public void BindUsers()
{
DataTable dt = userDAO.GetUsersForVIEWEDITDEL();
lblNoOfUsers.Text = "Total Number of Users: " + dt.Rows.Count;
gvUsers.DataSource = dt;
gvUsers.DataBind();
}
protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string Email = gvUsers.Rows[e.RowIndex].Cells[1].Text;
userDAO.DeleteUser(Email);
BindUsers();
}
|