简体   繁体   English

将一行从网格视图传递到另一个网格视图asp.net C#

[英]Pass one row from grid view to another grid view asp.net c#

I am wondering of how to pass data, that's one row from Grid View1 to Grid View2 in the same Web form!I created two tables, the first one contains four columns and contains records: Id, name, address and age. 我想知道如何传递数据,这是在同一Web窗体中从Grid View1到Grid View2的一行!我创建了两个表,第一个表包含四列并包含记录:Id,名称,地址和年龄。 I also created the second form which also contains four columns: Id, name, address and age, but left empty for purpose of transmitting data from the first table. 我还创建了第二个表单,该表单也包含四列:Id,名称,地址和年龄,但为了从第一张表中传输数据而保留为空。 I wonder if it is possible if to select one row from Grid view1 and then click a button which enables to pass that selected row to Grid View2 !? 我想知道是否有可能从Grid View1中选择一行,然后单击一个按钮,以将所选的行传递给Grid View2! One row each a time added after hitting the send button to the second grid view and guarantees that data added to the second table! 按下发送按钮后,每次在第二个网格视图中每次添加一行,并确保将数据添加到第二个表中!

Any ideas of how to do that would be much appreciated. 任何有关如何做到这一点的想法将不胜感激。

I'll give you an overview of some steps you might want to take 我将概述您可能要采取的一些步骤

I assume you use a DataSet or a DataTable where your records are stored in. 我假设您使用存储记录的DataSetDataTable

  • use the eventHandler of the gridview, like CellClick , most of them have a e.RowIndex value, which you can use to get the index of the row that is selected 使用gridview的eventHandler,例如CellClick ,它们大多数都有e.RowIndex值,您可以使用该值获取所选行的索引。

Like this: 像这样:

    private void dg1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
            selectedIndex = e.RowIndex;
    }
  • use that index to get the id from that record, so you know what data you are talking about 使用该索引从该记录中获取id ,这样您就知道您在说什么数据

like this: 像这样:

int id = Convert.ToInt32(dataset1.Tables[0].Rows[selectedIndex][0]);

then just find the row by id or use the knowledge above to combine this 然后只需按ID查找行,或使用上面的知识将其组合起来

DataRow row = dataset1.Tables[0].Rows.Find(Id);
dg2.Rows.Add(row);

it's better to use datasets and give the datagridview a datasource to that dataset, not sure what your intentions are 最好使用数据集并为datagridview提供该数据集的数据源,但不确定您的意图是什么

  • you could also get the id from the row you selected, based on that you call another sqlCommand to get that current row, a good way if you have more data you want to load from the database that rely on that id 您还可以从选定的行中获取id ,这是基于您调用另一个sqlCommand来获取当前行的一种方法,如果您想从依赖该id的数据库中加载更多数据,则可以采用这种方法

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

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