在asp.net C#中从DataTable中删除记录 [英] Delete Record From DataTable in asp.net C#

查看:82
本文介绍了在asp.net C#中从DataTable中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,



我的aspx文件如下:



 <   asp:内容    ID   =  Content2    ContentPlaceHolderID   =  MainContent    Runat   = 服务器 >  
< ; asp:ToolkitScriptManager ID = ToolkitScriptManager1 runat = server >
< / asp:ToolkitScriptManager >
< div class = ContentClass >
< asp:TabContainer ID = AssetTransactionTab runat = 服务器 ActiveTabIndex = 0 CssClass = 标签 ScrollBars = 垂直 宽度 = 794px 高度 = 290px >
< asp:TabPanel ID = NewTransaction runat = server ScrollBars = 自动 HeaderText = 新交易 < span class =code-attribute> >
< < span class =code-leadattribute> ContentTemplate >
< div align = center >
< asp:UpdatePanel ID = GridUpdatePanel runat = server >
< ContentTemplate >
< asp:GridView ID = GriDisplay runat = server onrowcommand = GriDisplay_RowCommand < span class =code-attribute> onrowdeleting = GriDisplay_RowDeleting 高度 = 162px >
< >
< asp:TemplateField >
< ItemTemplate >
< asp:LinkBut​​ton ID = BtnDelete < span class =code-attribute> CommandArgument
=' ' CommandName = 删除 runat = server > 删除< / asp:LinkBut​​ton >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns >
< / asp:GridView >
< < span class =code-leadattribute> / ContentTemplate < span class =code-keyword>>
< / asp:UpdatePanel >
< / div >
< / div >
< / ContentTemplate >
< / asp:TabPanel >
< < span class =code-leadattribute> / asp:TabContainer >
< / div >
< / asp:Content >



和我的aspx.cs代码如下。

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{

DataTable dtToGrid = new DataTable();
dtToGrid.Columns.Add( 资产 typeof (System。 Int16 ));
dtToGrid.Columns.Add( 员工 typeof string ));
dtToGrid.Columns.Add( 位置 typeof string ));
dtToGrid.Columns.Add( Supplier typeof string ));
dtToGrid.Columns.Add( IssuedDate typeof string ));
会话[ dtToGrid] = dtToGrid;
}
}
受保护 void btnAdd_Click( object sender,EventArgs e)
{
DataTable dtToGrid =(DataTable)Session [ dtToGrid];
DataRow drToGrid = dtToGrid.NewRow();
drToGrid [ 资产] = ddlAssetID.SelectedValue;
drToGrid [ Employee] = ddlEmployeeID.SelectedItem;
drToGrid [ 位置] = ddlLocationID.SelectedItem;
drToGrid [ Supplier] = ddlSupplierID.SelectedItem;
drToGrid [ IssuedDate] = txtIssuedReceivedDate.Text.Trim();
dtToGrid.Rows.Add(drToGrid);
GriDisplay.DataSource = dtToGrid;
GriDisplay.DataBind();


}



 受保护  void  GriDisplay_RowCommand( object  sender,GridViewCommandEventArgs e)
{
if (e.CommandArgument == 删除
{
int ID = Convert.ToInt32(e.CommandArgument);
DataTable updt =(DataTable)会话[ dtToGrid];
int i = 0 ;
while (i!= 0
{
if (Convert.ToInt32(updt.Rows [i] [ 资产])== ID)
updt.Rows [i] .Delete();
i ++;
}
}
}
受保护 void GriDisplay_RowDeleting ( object sender,GridViewDeleteEventArgs e)
{


}







加法记录正在运行但删除(删除)无效。

请帮助我如何从Gridview中删除记录和数据表,还帮助我如何将该记录插入到SQL数据库表中。



谢谢。

解决方案

< blockquote>这是解决方案:









protected void Griview1_RowDeleting (对象发送者,GridViewDeleteEventArgs e)

{



Gridview1.DeleteRow(e.RowIndex);



}


更新Link按钮的gridview源代码,如下所示

< asp:LinkBut​​ton ID = BtnDelete CommandArgument =<%#Eval(Asset)%> CommandName =删除runat =server>删除< / asp:LinkBut​​ton> 





谢谢,

--RG


基于此ID从表中删除您的记录



 < span class =code-keyword> protected   void  grd_CategoryList_RowDeleting( object  sender,GridViewDeleteEventArgs e )
{
try
{
objCM.iID = int < /跨度> .Parse(grd_CategoryList.DataKeys [e.RowIndex] .Value.ToString());

// 您的基于iID的删除代码

}


good Morning,

my aspx file is given below.

<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.

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();
          
          
          }


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.

解决方案

This Is the solution:




protected void Griview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

Gridview1.DeleteRow(e.RowIndex);

}


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


based on this id delete your records from table

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

    }


这篇关于在asp.net C#中从DataTable中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆