GRIDVIEW ALL IN ONE-GRIDVIEW EDIT DELETE AND UPDATE EVENTS


ALL GRIDVIEW PROPERTIES AND EVENTS EDITING,DELETING,PAGING



DESIGN PART




SOURCE PART
<asp:GridView ID="gvModule" runat="server" AutoGenerateColumns="False" DataKeyNames="Module_ID"
            Width="100%" ForeColor="#333333" AllowPaging="true" PageSize="10" CellPadding="4"
            CellSpacing="2" GridLines="None" OnRowDataBound="gvModule_RowDataBound" OnSelectedIndexChanged="gvModule_SelectedIndexChanged"
            OnPageIndexChanging="gvModule_PageIndexChanging" OnRowDeleting="gvModule_RowDeleting">
            <Columns>
                <asp:BoundField DataField="Module_Id" HeaderText="Module_Id" Visible="false">
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="#No">
                    <ItemTemplate>
                        <%# Container.DataItemIndex +1  %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Module_NAME" HeaderText="Module">
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Edit">
                    <ItemTemplate>
                        <asp:ImageButton ID="ibEdit" runat="server" CommandName="select" Width="16px" Height="16px"
                            ImageUrl="~/admin/images/edit_icon.png" />
                    </ItemTemplate>
                    <HeaderStyle Width="100px" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:ImageButton ID="ibDelete" runat="server" CommandName="delete" Width="16px" Height="16px"
                            ImageUrl="~/admin/images/delete_icon.png" OnClientClick='javascript:if(confirm("Do you wish to delete?")){return true;} return false;' />
                    </ItemTemplate>
                    <HeaderStyle Width="100px" />
                    <ItemStyle HorizontalAlign="Left" />
                </asp:TemplateField>
            </Columns>
            <HeaderStyle CssClass="GridHeader" HorizontalAlign="Left"></HeaderStyle>
            <PagerStyle CssClass="GridHeader" HorizontalAlign="Center" />
            <RowStyle BorderColor="#f08f40" BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EmptyDataTemplate>
                No Data Found
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#DCE6F7" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle Font-Names="Helvetica" VerticalAlign="Middle" Height="25px" />
            <EditRowStyle BackColor="#2461BF" Font-Bold="true" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>




C# CODE PART

private void FillGrid()
    {
  DataTable dt = objMd.Select_All_Modules();//CODE FOR TAKING VALUE AS DATATABLE
        if (dt.Rows.Count > 0)
        {
            gvModule.DataSource = dt;
            gvModule.DataBind();
        }
        else
        {
            idWarning.Visible = true;//MESSAGE
        }
    }

    private void FillForm(int Id)
    {
        DataTable dt=objMd.Select_One_Modules(Id);
        if (dt.Rows.Count > 0)
        {
            hidVid.Value = Id.ToString();
           
        txtModule.Text = dt.Rows[0]["Module_NAME"].ToString();
        txtDescription.Text = dt.Rows[0]["Module_DESCRIPTION"].ToString();
        }

    }

  //SELECTED INDEX CHANGED FOR EDITING ONE ROW IN GRID GIVES COMMAND NAME AS SELECT
    protected void gvModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillForm(int.Parse(gvModule.SelectedDataKey[0].ToString()));
        FillGrid();
    }


//FOR ALTERNATE ROW STYLES
protected void gvModule_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#fdfcf6'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#f6f6f6'");
        }
    }


//FOR PAGE INDEXING OR PAGING
protected void gvModule_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvModule.PageIndex = e.NewPageIndex;
        FillGrid();
        idWarning.Visible = false;
        lblMsg.Visible = false;
    }

//DELETE FUNCTION
    private void Delete(int Id)
    {
        bool Del = objMd.Delete_Module(Id);
        if (Del == true)
        {
            lblMsg.Visible = true;
lblMsg.Text = "Module deleted sucessfully . ";
          

        }
    }

//FOR DELETING IN GRID USING DELETE COMMAND
    protected void gvModule_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dvModule.Style["display"] = "none";
        Delete(int.Parse(gvModule.DataKeys[e.RowIndex].Value.ToString()));
        FillGrid();
     

    }

Comments