Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Comments
aloknetuser 24-Nov-15 12:13pm    
There r two columns containing first name m last name in user name table
Suvendu Shekhar Giri 24-Nov-15 12:25pm    
That's pretty easy stuff. What have you tried so far?
jgakenhe 24-Nov-15 13:29pm    
An easy way would be to save your dataset to a ViewState.

GridView1.DataSource = ds;
ViewState["GridView1"] = ds;
YourGridView.DataBind();

Now you can bind your dataset to the other GridView when you are ready; such as in a click event.
j snooze 24-Nov-15 17:25pm    
viewstate won't work here. says in the description this is a windows app, not a web app.
try looking at this link to see if it helps you with looping through to check for a row with a checked value.
http://stackoverflow.com/questions/1237829/datagridview-checkbox-column-value-and-functionality

1 solution

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

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

Then you bind it to a DataGridView like this
C#
dataGridView1.DataSource = dt.DefaultView;

In the button click event, you can use the following code
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900