0

这似乎是一个重复的问题,但我无法得到我的答案。

我有一个网格视图,当我单击网格视图外的按钮时,我需要删除特定的行。

 protected void btnDelete_Click(object sender, EventArgs e)
      {
            dtable = (DataTable)Session["data"];
            DataRow row = dtable.Rows[DataGV1.SelectedIndex];
            dtable.Rows.Remove(row);
            DataGV1.DataSource = dtable;
            DataGV1.DataBind();
            Session["data"] = dtable;
        }

会话变量具有数据表的先前状态。

protected void DataGV1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView _gridView = (GridView)sender;

        // Get the selected index 
         _selectedIndex = int.Parse(e.CommandArgument.ToString());
     }

网格视图控件

        onselectedindexchanged="DataGV1_SelectedIndexChanged" 
        OnRowCommand="DataGV1_RowCommand" OnRowDeleting="DataGV1_RowDeleting"
        AutoGenerateSelectButton="False" DataKeyNames="Role,Last_name">
        <Columns>
            <asp:ButtonField DataTextField="last_name" HeaderText="Last_name" CommandName="SingleClick"
                SortExpression="last_name" Text="Button" />
            <asp:BoundField DataField="role" HeaderText="role" SortExpression="role" />
             <asp:BoundField DataField="role" HeaderText="role"                      HeaderText="Frist_name" 
                 SortExpression="first_name" Text="First_name" />
        </Columns>
    </asp:GridView>

这似乎不起作用。你能告诉我哪里出错了吗?

4

3 回答 3

0

您必须将 _selectedIndex 存储在 ViewState 中,然后在删除按钮上单击您从 Viewstate 中检索 _selectedIndex 并使用它从数据集中删除行,然后重新加载网格。

protected void DataGV1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridView _gridView = (GridView)sender;

        // Get the selected index 
         ViewState["SelIndex"] = e.CommandArgument.ToString();
     }

protected void btnDelete_Click(object sender, EventArgs e)

  {
        if(ViewState["SelIndex"] == null)
            return;

        int selIndex = int.Parse(ViewState["SelIndex"]);

        dtable = (DataTable)Session["data"];
        DataRow row = dtable.Rows[selIndex ];
        dtable.Rows.Remove(row);
        DataGV1.DataSource = dtable;
        DataGV1.DataBind();
        Session["data"] = dtable;
    }
于 2012-07-05T06:34:09.233 回答
0

如果按钮在外面,GridView则不需要处理RowCommand事件(实际上是不合适的)。

建议:

您必须添加一TemplateField列,将 CheckBox 控件拖放到 TemplateField 的 ItemTemplate 中,并在按钮的单击处理程序中编写代码以遍历GridView.Rows集合,通过读取控件的值来识别选定的行,CheckBox如果选中则执行删除操作CheckBox

演示数据源 ( List<T>)

public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }

    public static List<Item> Data()
    {
        List<Item> list = new List<Item>()
        {
                new Item(){ ID=11, Name="A"},
                new Item(){ ID=12, Name="B"},
                new Item(){ ID=13, Name="C"},
                new Item(){ ID=14, Name="D"},
                new Item(){ ID=15, Name="E"},
        };
        return list;
    }
}

标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
   <Columns>
        <asp:ButtonField DataTextField="Name" HeaderText="Name" CommandName="SingleClick"
            SortExpression="last_name" Text="Button" />
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
    </Columns>
</asp:GridView>
<asp:Button ID="btnDelete" runat="server" Text="Button" />

代码隐藏 (Page_Load)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["data"] = Item.Data();
        GridView1.DataSource = Session["data"];
        GridView1.DataBind();
    }
    /*--- RowCommand handler ---*/
    GridView1.RowCommand += (sa, ea) =>
        {
            ViewState["RowIndex"] = ea.CommandArgument.ToString();
        };
    /*--- Delete button click handler ---*/
    btnDelete.Click += (sa, ea) =>
        {
            if (ViewState["RowIndex"] != null)
            {
                int index = int.Parse(ViewState["RowIndex"].ToString());
                List<Item> items = Session["data"] as List<Item>;
                items.RemoveAt(index);
                GridView1.DataSource = Session["data"];
                GridView1.DataBind();
                ViewState["RowIndex"] = null;
            }
        };
}
于 2012-07-05T06:38:39.493 回答
0

看看这些文章,这些文章将解释从 gridview 中删除单行/多行。 http://technico.qnownow.com/2012/06/15/how-to-delete-multiple-rows-from-gridview-with-checkboxes/ http://technico.qnownow.com/2012/06/14/如何从gridview-with-client-side-confirmation删除一行/

于 2012-07-08T02:12:14.457 回答