如何从一个网格视图传输和删除单个选定的行到另一个网格视图

时间:2016-04-14 04:49:13

标签: c# asp.net .net gridview

我有两个网格视图,当我选中它传输多行的复选框,但它没有传输单个选中的行我试过的代码是:我想传输和删除单个选定的行和多个选定的行。

选择gvDetails行传输gridview。

private void GetRemoveRows()
        {

            if (Session["GetRecords"] != null)
            DataTable  dtre = (DataTable)Session["GetRecords"];

            Session["GetRecords"] = dtre ;

            gvDetails.DataSource = dtre; 
            gvDetails.DataBind();

        } 
任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

查看这些代码。

HTML:

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
        <ItemTemplate>
            <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<br />
<u>Selected Row Values: </u>
<br />
<br />
<asp:Label ID="lblValues" runat="server" Text=""></asp:Label>

C#代码:

页面加载:

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
 {
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    GridView1.DataSource = dt;
    GridView1.DataBind();
 }
}

选择行索引..

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
 //Accessing BoundField Column
 string name = GridView1.SelectedRow.Cells[0].Text;

 //Accessing TemplateField Column controls
 string country = (GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;

 lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
}

如果您有任何疑问,请注释。