简体   繁体   English

如何将项目从一个gridview移到另一个gridview?

[英]How do I move items from one gridview to another gridview?

I have two gridviews, and when the user highlights a row on the first gridview and clicks a button, it should move to the second gridview. 我有两个gridview,当用户在第一个gridview上突出显示一行并单击一个按钮时,它应移至第二个gridview。

When I click on the button that record gets added but it only add the last row I've selected (if I select 20 rows, only the last gets added). 当我单击该按钮时,将添加记录,但记录仅添加我选择的最后一行(如果我选择20行,则仅添加最后一行)。

All records that are selected should be moved. 所有选定的记录应被移动。

How do I do this in ASP.NET? 如何在ASP.NET中做到这一点?

private void button1_Click_1(object sender, EventArgs e)
{
    DataGridViewRow dr = dataGridView1.SelectedRows[0];

    dtItems.Columns.Add("city_ID");
    dtItems.Columns.Add("city_Name");
    dtItems.Columns.Add("status");
    dtItems.Columns.Add("date");

    if (dataGridView1.Rows.Count > 1)
    {
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value != null)
            {
                DataRow row;
                row = dtItems.NewRow();

                row["city_ID"] = dataGridView1.Rows[i].Cells[1].Value.ToString();
                row["city_Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString();
                row["status"] = dataGridView1.Rows[i].Cells[3].Value.ToString();
                row["date"] = dataGridView1.Rows[i].Cells[4].Value.ToString();

                dtItems.Rows.Add(row);
            }
        }
    }

    Form2 frm = new Form2(dtItems);
    frm.ShowDialog();
}

In Form2 copy this code: 在Form2中复制以下代码:

public Form2(DataTable dtIt)
{
    dtItems = dtIt;
    InitializeComponent();
}

private void AddEmptyRows()
{
    for (int i = 1; i <= 5; i++)
    {
        dataGV.Rows.Add();
    }
}

private void Form2_Load(object sender, EventArgs e)
{
    AddEmptyRows();

    for (int i = 0; i < dtItems.Rows.Count; i++) {
        dataGV.Rows[i].Cells[0].Value = dtItems.Rows[i]["city_ID"];
        dataGV.Rows[i].Cells[1].Value = dtItems.Rows[i]["city_Name"];
        dataGV.Rows[i].Cells[2].Value = dtItems.Rows[i]["status"];
        dataGV.Rows[i].Cells[3].Value = dtItems.Rows[i]["date"];
    }

    dataGV.Enabled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM