In aspx, i have:
<asp:GridView ID="gvDetails" AutoGenerateColumns="False" AllowPaging="True" PageSize="4"
DataKeyNames="ID" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnPageIndexChanging="gvDetails_PageIndexChanging"
OnRowCancelingEdit="gvDetails_RowCancelingEdit" OnRowEditing="gvDetails_RowEditing"
OnRowUpdating="gvDetails_RowUpdating" OnRowDataBound="gvDetails_RowDataBound"
OnRowDeleting="gvDetails_RowDeleting">
<Columns>
<asp:CommandField ButtonType="Link" HeaderText="Action" ShowEditButton="true" ShowDeleteButton="true"
ShowCancelButton="true" />
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBoxEditName" runat="server" Text='<%# Bind("Name") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Bind("Name") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("Name") %>' />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
In CS:
{
SqlCommand cmd, upd;
SqlConnection con;
SqlDataAdapter da;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
if (Request.Cookies["Username"] == null || Request.Cookies["Password"] == null)
{
Response.Redirect("../../Registration/Login.aspx");
}
else
{
lblUser.Text = Request.Cookies["Username"].Value.ToString();
lblUser.ForeColor = System.Drawing.Color.White;
Session["Username"] = Request.Cookies["Username"].Value.ToString();
}
if (!IsPostBack)
{
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
public DataTable bindresults()
{
cmd = new SqlCommand("select * from Registration", con);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
return dt;
}
protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDetails.PageIndex = e.NewPageIndex;
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
string Name = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditName")).Text;
string Gender = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditGender")).Text;
string Occupation = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditOccupation")).Text;
string Address = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[4].FindControl("TextBoxEditAddress")).Text;
upd = new SqlCommand("update Registration set Name = '" + Name + "', Gender = '" + Gender + "', Occupation = '" + Occupation + "', Address = '" + Address + "' where ID = '" + ID + "'", con);
upd.ExecuteNonQuery();
gvDetails.EditIndex = -1;
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
cmd = new SqlCommand("delete from Registration where ID = " + ID + "", con);
cmd.ExecuteNonQuery();
bindresults();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!DataBinder.Eval(e.Row.DataItem, "Username").Equals(Session["Username"].ToString()))
{
LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[0];
LinkButton btn1 = (LinkButton)e.Row.Cells[0].Controls[2];
if (btn != null && btn1 != null)
{
btn.Visible = false;
btn1.Visible = false;
}
}
}
}
}
It is working fine, but its really lengthy. I was wondering if someone could provide a less code. I know i may be sounding foolish but for many, this may be worth asking.