<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="5" OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" DataKeyNames="UserId,UserName"> <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.png" ToolTip="Edit" Height="20px" Width="20px" /> <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="UserName"> <ItemTemplate> <asp:Label ID="lblitmusr" runat="server" Text='<%Eval("UserName")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <ItemTemplate> <asp:Label ID="lblitmcity" runat="server" Text='<%Eval("City") %>' ></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Designation"> <ItemTemplate> <asp:Label ID="lblitmdesignation" runat="server" Text='<%Eval("Designation")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Label ID="lblresult" runat="server"></asp:Label>
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["grid"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView(); } } protected void BindGridView() { con.Open(); SqlCommand cmd = new SqlCommand("select * from GridDetails", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvdetails.DataSource = ds; gvdetails.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvdetails.DataSource = ds; gvdetails.DataBind(); int columncount = gvdetails.Rows[0].Cells.Count; gvdetails.Rows[0].Cells.Clear(); gvdetails.Rows[0].Cells.Add(new TableCell()); gvdetails.Rows[0].Cells[0].ColumnSpan = columncount; gvdetails.Rows[0].Cells[0].Text = "No Records Found"; } } protected void gvdetails_RowEditing(object sender,GridViewEditEventArgs e ) { gvdetails.EditIndex = e.NewEditIndex; gvdetails.DataBind(); } protected void gvdetails_RowDeleting(object sender,GridViewDeleteEventArgs e) { int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString()); string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString(); con.Open(); SqlCommand cmd = new SqlCommand("delete from GridDetails where UserId=" + userid, con); int result = cmd.ExecuteNonQuery(); con.Close(); if (result == 1) { BindGridView(); lblresult.Text = username + "details deleted successfully"; } }
UserName City Designation <%Eval("UserName")%> <%Eval("City") %> <%Eval("Designation")%> <%Eval("UserName")%> <%Eval("City") %> <%Eval("Designation")%> <%Eval("UserName")%> <%Eval("City") %> <%Eval("Designation")%>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)