【问题标题】:Copy items from one GridView to another将项目从一个 GridView 复制到另一个
【发布时间】:2011-11-06 19:26:05
【问题描述】:

我有一个网络用户控件,它有一个包含 3 列的表格 1) 首先是 GridView, 2)第二个过滤选项(文本框), 3) 第三个是另一个 GridView。

当我在过滤器(文本框)中输入文本时,会从数据库中选择一些行并显示在第三列的 GridView 中。这个 GridView 有选择按钮,当它被按下时,我希望将该行(或其中的一些列)添加到 GridView 的第一列。

据我所知,在这些情况下使用 DataTable 是很常见的。我拥有的代码:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    DataTable tempTable = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        tempTable.Columns.Add("ObjectID");
        tempTable.Columns.Add("Name");
        tempTable.Columns.Add("Price");
        currentDay.DataSource = tempTable;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }

    protected void objectChooser_SelectedIndexChanged(
        object sender, EventArgs e)
    { 
        DataRow newRow = tempTable.NewRow();
        newRow["ObjectID"] = objectChooser.SelectedRow.Cells[0].Text;
        newRow["Name"] = objectChooser.SelectedRow.Cells[1].Text;
        newRow["Price"] = objectChooser.SelectedRow.Cells[2].Text;           

        tempTable.Rows.Add(newRow);                        
        currentDay.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();

        var objects = from p in dc.VisitingObjects
                  where p.City == tbCityFilter.Text
                  select p;

        objectChooser.DataSource = objects;
        objectChooser.DataBind();
    }
}

但是这有问题。当我第一次按下选择按钮(在 GridView 中)时,它可以工作(新值被添加到第一个 GridView,但之后,按下选择按钮只会更改第一个 GridView 中的值,但不会添加新行。

您能告诉我我的代码有什么问题吗,或者有更好的方法来使用从 GridView1 到 GridView2 的复制值?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    I feel like I've said this before...

    您需要将 DataTable 存储在不会在回发之间消失的地方。 Session 可能是一个很好的起点。

    objects = Session["data"]; //cast this
    if (objects == null)
        //init objects to a new list of your obj type
    objects = objects.Union (
        //your code for filtering grid rows
        );
    Session["data"] = objects;
    

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多