基于复选框将数据从一个数据网格视图填充到另一个数据网格视图 [英] Populating Data from one datagridview to another based on checked box

查看:59
本文介绍了基于复选框将数据从一个数据网格视图填充到另一个数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



我使用vs2005 for windows applications.and language c#。

我必须创建一个

将从数据库中的用户名表中检索记录,并在datagridview中显示与之关联的复选框。并执行以下任务。

1.通过复选框选择行

2.单击表单上的按钮

3.在另一个Datagridview中传输选定的行。

我已经解决了一个部分,即填充数据网格带复选框但无法传输所选记录

Hello friends,

I m using vs2005 for windows applications.and language c#.
I have to create an application that
will retrieve record from "user name" table in database and show in datagridview with check box associated with it.and perform the following task.
1.Selected the rows through checkbox
2.Click a button on the form
3.Transfer selected rows in another Datagridview.
I have solved one part i.e populating data in grid with check box but unable to transfer selected record

推荐答案

假设你有一个如下所示的DataTable

Let's say you have a DataTable like below
DataTable dt = new DataTable();

dt.Columns.Add("Selected", typeof(bool));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));



然后将它绑定到像这样的DataGridView


Then you bind it to a DataGridView like this

dataGridView1.DataSource = dt.DefaultView;



在按钮点击事件中,您可以使用以下内容代码


In the button click event, you can use the following code

private void button1_Click(object sender, EventArgs e)
{
    var selectedRows =
        from row in dt.AsEnumerable()
        where row.Field<bool>("Selected")
        select row;

    if (selectedRows.Count() > 0)
    {
        DataTable boundTable = selectedRows.CopyToDataTable();
        dataGridView2.DataSource = boundTable.DefaultView;
    }
}





这个例子只是众多方法中的一种。



This example is just one of many ways to do it.


这篇关于基于复选框将数据从一个数据网格视图填充到另一个数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆