Today i want to show how to delete, and modify in gridview plus to have a user friendly viewing functionality about the a Total records that user want to show on GridView and total records of all data.
Lets Try..
The ASP.NET Page
<asp:Label ID="LErrorMessage" ForeColor="Red" runat="server"></asp:Label> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mainTable" BorderColor="Gray" BorderStyle="Solid" BorderWidth="1px" AllowPaging="True" PageSize="15" Width="864px" EmptyDataText="Tiada Rekod Dijumpai" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="addJavascript" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <RowStyle BorderColor="Gray" BorderStyle="Solid" BorderWidth="1px" /> <AlternatingRowStyle BackColor="#F7F6F3" BorderColor="Gray" BorderStyle="Solid" BorderWidth="1px" ForeColor="#333333" /> <Columns> <asp:TemplateField HeaderText="No."> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> <ItemStyle Width="20px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:TemplateField> <asp:BoundField DataField="UserId" HeaderText="User ID"> <ItemStyle Width="100px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:BoundField> <asp:BoundField DataField="First_Name" HeaderText="First Name"> <ItemStyle Width="150px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:BoundField> <asp:BoundField DataField="Last_Name" HeaderText="Last Name"> <ItemStyle Width="100px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:BoundField> <asp:BoundField DataField="IsLockedOut" HeaderText="is Lock"> <ItemStyle Width="80px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:BoundField> <asp:CommandField CausesValidation="False" ShowDeleteButton="True"> <ItemStyle Width="50px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:CommandField> <asp:CommandField SelectText="Modify" CausesValidation="false" ShowSelectButton="True"> <ItemStyle Width="50px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /> </asp:CommandField> </Columns> <HeaderStyle ForeColor="White" BackColor="#6699CC" Font-Bold="true" /> <PagerStyle BackColor="#6699CC" ForeColor="White" /> </asp:GridView> <br /> <br /> <asp:Panel ID="PanelDropDownGVPage" runat="server" Visible="false"> Total users per page : <asp:DropDownList ID="DropDownList1" CssClass="defaultSelect" Width="50px" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <br /> </asp:Panel> Shows records <asp:Literal ID="LRekodShow" runat="server"></asp:Literal> from <asp:Literal ID="LiteralTotalRekod" runat="server"></asp:Literal> users <br />
The JavaScript - put in header
<script type="text/javascript"> function ConfirmOnDelete(item) { if (confirm("Are you sure to remove " + item + "?") == true) return true; else return false; } </script>
The Code Behind
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGridview(); } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindGridview(); } private void BindGridview() { string err = ""; DataTable UsersTable = GetAllUsersInformation(ref err); if (err.Trim() != "") { LErrorMessage.Text = err; return; } int pageIndex = GridView1.PageIndex; if (UsersTable.Rows.Count > GridView1.PageSize) { PanelDropDownGVPage.Visible = true; if (pageIndex == 0) { LRekodShow.Text = "1 - " + GridView1.PageSize.ToString(); } else { int pageSize = GridView1.PageSize; int numberRecord = pageIndex * pageSize; int balance = UsersTable.Rows.Count - numberRecord; int startRecordToShow = numberRecord + 1; if (balance > GridView1.PageSize) { LRekodShow.Text = startRecordToShow.ToString() + " - " + (GridView1.PageSize + numberRecord).ToString(); } else { LRekodShow.Text = startRecordToShow.ToString() + " - " + (balance + numberRecord).ToString(); } } } else if (UsersTable.Rows.Count == 0) { LRekodShow.Text = "0"; } else { LRekodShow.Text = "1 - " + UsersTable.Rows.Count.ToString(); } GridView1.DataSource = UsersTable; GridView1.DataBind(); string[] listAll = { "10", "15", "25", "35", "50" }; DropDownList1.DataSource = listAll; DropDownList1.DataBind(); DropDownList1.SelectedValue = GridView1.PageSize.ToString(); LiteralTotalRekod.Text = UsersTable.Rows.Count.ToString(); } private DataTable GetAllUsersInformation(ref string err) { DataTable users = new DataTable(); users.Columns.Add("UserId", typeof(string)); users.Columns.Add("First_Name", typeof(string)); users.Columns.Add("Last_Name", typeof(string)); users.Columns.Add("IsLockedOut", typeof(string)); DataRow row; for (int i = 0; i < 100; i++) { row = users.NewRow(); row["UserId"] = "AHMAD" + i.ToString(); row["First_Name"] = "AHMAD FIRST NAME" + i.ToString(); row["Last_Name"] = "AHMAD LAST NAME" + i.ToString(); row["IsLockedOut"] = "FALSE"; users.Rows.Add(row); } return users; } protected void addJavascript(object sender, GridViewRowEventArgs e) { if (e.Row.RowState != DataControlRowState.Edit) // check for RowState { if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType { string id = e.Row.Cells[1].Text; // Get the id to be deleted LinkButton lb = (LinkButton)e.Row.Cells[5].Controls[0]; //cast the ShowDeleteButton link to linkbutton if (lb != null) { //add script to link button lb.Attributes.Add("onclick", "return ConfirmOnDelete('User " + id + "');"); //attach the JavaScript function with the ID as the paramter } } } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string err = ""; //string UserID = GridView1.Rows[e.RowIndex].Cells[1].Text; string UserID = e.Values["UserId"].ToString(); //do logic delete based on user id } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { //modifi user action //get user id from selected row string UserID = GridView1.SelectedRow.Cells[1].Text; //do logic to modify user } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string val = DropDownList1.SelectedValue.ToString(); GridView1.PageSize = Convert.ToInt16(val); BindGridview(); }
The Output
Happy coding...
By Mohd Zulkamal
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
0 comments:
Post a Comment