Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
good Morning,

my aspx file is given below.

XML
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
         <div class="ContentClass">
         <asp:TabContainer ID="AssetTransactionTab" runat="server" ActiveTabIndex="0" CssClass="Tab" ScrollBars="Vertical"   Width="794px" Height="290px">
         <asp:TabPanel ID="NewTransaction"  runat="server"  ScrollBars="Auto" HeaderText="New Transaction" >
         <ContentTemplate>
  <div align="center" >
             <asp:UpdatePanel ID="GridUpdatePanel" runat="server">
             <ContentTemplate>
             <asp:GridView ID="GriDisplay" runat="server" onrowcommand="GriDisplay_RowCommand" onrowdeleting="GriDisplay_RowDeleting"  Height="162px">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
            <asp:LinkButton ID="BtnDelete" CommandArgument='' CommandName="Delete" runat="server">Remove</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
            </Columns>
           </asp:GridView>
             </ContentTemplate>
             </asp:UpdatePanel>
             </div>
         </div>
         </ContentTemplate>
         </asp:TabPanel>
         </asp:TabContainer>
         </div>
</asp:Content>

and my aspx.cs code is given below.
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
                DataTable dtToGrid = new DataTable();
                dtToGrid.Columns.Add("Asset", typeof(System.Int16));
                dtToGrid.Columns.Add("Employee", typeof(string));
                dtToGrid.Columns.Add("Location", typeof(string));
                dtToGrid.Columns.Add("Supplier",typeof(string));
                dtToGrid.Columns.Add("IssuedDate", typeof(string));
                Session["dtToGrid"] = dtToGrid;
          }
}
  protected void btnAdd_Click(object sender, EventArgs e)
    {
            DataTable dtToGrid = (DataTable)Session["dtToGrid"];
            DataRow drToGrid = dtToGrid.NewRow();
            drToGrid["Asset"] = ddlAssetID.SelectedValue;
            drToGrid["Employee"] = ddlEmployeeID.SelectedItem;
            drToGrid["Location"] = ddlLocationID.SelectedItem;
            drToGrid["Supplier"] = ddlSupplierID.SelectedItem;
            drToGrid["IssuedDate"] = txtIssuedReceivedDate.Text.Trim();
            dtToGrid.Rows.Add(drToGrid);
            GriDisplay.DataSource = dtToGrid;
            GriDisplay.DataBind();
          
          
          }

C#
protected void GriDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      if (e.CommandArgument == "Delete")
      {
          int ID = Convert.ToInt32(e.CommandArgument);
          DataTable updt = (DataTable)Session["dtToGrid"];
          int i = 0;
          while (i!=0)
          {
          if (Convert.ToInt32(updt.Rows[i]["Asset"]) == ID)
          updt.Rows[i].Delete();
          i++;
          }
      }
  }
  protected void GriDisplay_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {


  }



Addition Record is working But Delete (Remove) not working.
Please Help Me how can i Remove Record from Gridview And Data Table and Also assist me how to insert that record into sql DataBase Table .

thanks.
Posted
Updated 10-May-11 8:56am
v2

This Is the solution:




protected void Griview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

Gridview1.DeleteRow(e.RowIndex);

}
 
Share this answer
 
Update gridview source code for Link button as below
<asp:LinkButton ID="BtnDelete" CommandArgument="<%# Eval("Asset" )%>" CommandName="Delete" runat="server">Remove</asp:LinkButton>


Thanks,
--RG
 
Share this answer
 
based on this id delete your records from table

C#
protected void grd_CategoryList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            objCM.iID = int.Parse(grd_CategoryList.DataKeys[e.RowIndex].Value.ToString());
             
                // Your Code for Delete Based of iID

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900