删除行从带的GridView的CommandName [英] Delete Row From Gridview with CommandName

查看:88
本文介绍了删除行从带的GridView的CommandName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除使用从我的的GridView 行的的CommandName ,但它不工作。我使用GET 的rowIndex 来做到这一点。

我没有得到任何错误,它只是不,当我点击做任何事情的ImageButton

下面是我的code:

 < ASP:GridView控件ID =GridView1=服务器WIDTH =538px背景色=白BORDERCOLOR =#DEDFDE边框=无边框宽度= 1px的CELLPADDING =4前景色=黑网格=垂直onselectedindexchanged =DropDownList5_SelectedIndexChanged的CssClass =MGRIDPagerStyle-的CssClass =PGRAlternatingRowStyle-的CssClass =ALTFONT-SIZE =小&GT ;
    < AlternatingRowStyle背景色=白/>    <柱体和GT;
        < ASP:的TemplateField>
            <&ItemTemplate中GT;
                < ASP:ImageButton的ID =ImageButton1=服务器HEIGHT =16像素的ImageUrl =〜/图片/ delete.pngWIDTH =16像素的CommandName =DeleteRow/>
            < / ItemTemplate中>
            < HeaderStyle宽度=30像素/>
            < ItemStyle HEIGHT =10px的/>
        < / ASP:的TemplateField>
    < /专栏>    < FooterStyle背景色=#CCCC99/>
    < HeaderStyle背景色=#6B696BFONT-粗体=真前景色=白/>
    < PagerStyle背景色=#F7F7DE前景色=黑Horizo​​ntalAlign =右/>
    < RowStyle的BackColor =#F7F7DE/>
    < SelectedRowStyle背景色=#CE5D5AFONT-粗体=真前景色=白/>
    < SortedAscendingCellStyle背景色=#FBFBF2/>
    < SortedAscendingHeaderStyle背景色=#848384/>
    < SortedDescendingCellStyle背景色=#EAEAD3/>
    < SortedDescendingHeaderStyle背景色=#575357/>
< / ASP:GridView的>

下面是CS code:

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    如果(Page.IsPostBack)
    {
        如果(e.CommandName.Equals(DeleteRow))
        {
            GridViewRow OITEM =(GridViewRow)((LinkBut​​ton的)e.CommandSource).NamingContainer;
            INT的rowIndex = oItem.RowIndex;
            GridView1.DeleteRow(rowIndex位置);
            的DataBind();
        }
    }
}


解决方案

如果你想删除数据库的行以及反映在页面上的更改,然后使用 GridViewID_RowCommand

ASPX

 < ASP:GridView控件ID =GridView1=服务器
onselectedindexchanged =DropDownList5_SelectedIndexChanged
的CssClass =MGRID
PagerStyle-的CssClass =PGR
AlternatingRowStyle-的CssClass =ALT>        <柱体和GT;
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                    < ASP:ImageButton的ID =ImageButton1=服务器
     HEIGHT =16像素的ImageUrl =〜/图片/ delete.png
    的CommandName =DeleteRow/>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
        < /专栏>
    < / ASP:GridView的>

C#

 保护无效GridView1_RowCommand(对象发件人,GridViewCommandEventArgs E)
    {
        如果(e.CommandName ==DeleteRow)
        {
           INT指数= Convert.ToInt32(e.CommandArgument);
           GridViewRow selectedRow = GridView1.Rows [指数]
           字符串ID = selectedRow.Cells [0]。文本; //假设你的ID是网格的第一列
           SqlConnection的CON =新的SqlConnection(WebConfigurationManager.ConnectionStrings [myconnectionstring]的ConnectionString); //假设你的连接字符串在web.config中
           con.Open();
           的SqlCommand平方=新的SqlCommand(DELETE FROM myTable的其中id ='+身份证+',CON);
           sq.ExecuteNonQuery();
           con.Close();
        }    }

如果你不记得你的 ID的列索引由你仍然可以得到 ID 价值在网​​格标题的名称 按照这个答案

在你的页面加载

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(this.IsPostBack)
       {
        SqlConnection的CON =新的SqlConnection(WebConfigurationManager.ConnectionStrings [myconnectionstring]的ConnectionString);
        con.Open();
        的SqlCommand的SqlCommand =新的SqlCommand(SELECT * FROM myTable的CON);
        SqlDataReader的读者= sqlCommand.ExecuteReader();
        GridView1.DataSource =阅读器;
        GridView1.DataBind();
        con.Close();
     }
   }

编辑:电网不应该在回传被绑定,我建议不要这样做。更新的Page_Load GridView的不是,而是换你的 GridView控件内的的UpdatePanel

 < ASP:的UpdatePanel ID =UpdatePanel2=服务器的UpdateMode =条件>
              <&的ContentTemplate GT;  < ASP:GridView控件ID =GV1...> ...< / ASP:GridView的>                 < /&的ContentTemplate GT;
          < / ASP:的UpdatePanel>

然后调用 UpdatePanel2.Update()无论你需要更新您的网格(在这种情况下,你需要绑定你在哪里修改其源即 RowCommand 和有约束力的电话后电网 UpdatePanel2.Update()

记住不管你做什么,你不能停止回传,因为点击的按钮。

I am trying to delete Rows from my Gridview using a CommandName but its not working. I am using get RowIndex to do this.

I do not get any errors, it just doesn't do anything when I click on the ImageButton.

Here is my code:

<asp:GridView ID="GridView1" runat="server" Width="538px" BackColor="White"  BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" onselectedindexchanged="DropDownList5_SelectedIndexChanged" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" Font-Size="Small"  >
    <AlternatingRowStyle BackColor="White" />

    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" Height="16px" ImageUrl="~/images/delete.png" Width="16px" CommandName="DeleteRow" />
            </ItemTemplate>
            <HeaderStyle Width="30px" />
            <ItemStyle Height="10px" />
        </asp:TemplateField>
    </Columns>

    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FBFBF2" />
    <SortedAscendingHeaderStyle BackColor="#848384" />
    <SortedDescendingCellStyle BackColor="#EAEAD3" />
    <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

Here is cs code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (Page.IsPostBack)
    {
        if (e.CommandName.Equals("DeleteRow"))
        {
            GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            int RowIndex = oItem.RowIndex;
            GridView1.DeleteRow(RowIndex);
            DataBind();
        }
    }
}

解决方案

If you want to delete the row from database as well and reflect the changes on the page then use GridViewID_RowCommand

aspx

    <asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="DropDownList5_SelectedIndexChanged"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" >

        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server"
     Height="16px" ImageUrl="~/images/delete.png"
    CommandName="DeleteRow" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

C#

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteRow")
        {
           int index = Convert.ToInt32(e.CommandArgument);
           GridViewRow selectedRow = GridView1.Rows[index];
           string id = selectedRow.Cells[0].Text; //assuming your ID is the first column of your grid
           SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString); //assuming your connection string is in web.config
           con.Open();
           SqlCommand sq = new SqlCommand("DELETE FROM myTable where id='" + id + "'", con);
           sq.ExecuteNonQuery();
           con.Close();
        }

    }

In case you do not remember the column index of your ID still you can get the ID value by the name of the Grid Header follow this answer

In your pageload

protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack)
       { 
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);
        con.Open();
        SqlCommand sqlCommand = new SqlCommand("SELECT * FROM myTable",con);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataBind();
        con.Close();
     }
   }

EDIT: grids shouldn't be bind on PostBack, I recommend do not do this. Update the gridview not in Page_Load, instead wrap your GridView inside an UpdatePanel

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
              <ContentTemplate>

  <asp:GridView ID="gv1" ...> ... </asp:GridView>

                 </ContentTemplate>
          </asp:UpdatePanel>

then call UpdatePanel2.Update() wherever you need to update your Grid (in this case you'll need to Bind the grid where you are modifying its source i.e RowCommand and after binding call UpdatePanel2.Update())

Remember whatever you do, you can not stop the PostBack because the click is on the button.

这篇关于删除行从带的GridView的CommandName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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