Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,
In my datagridview all columns have checkbox and user checked if any one selected row.I have a button when user click button I want to transfer which selected row/rows how can I solve this problem?
Thaxs for all :)


C#
Int32 ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString());
            MessageBox.Show("ID" + ID);
            DataTable table = new DataTable();
            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Dorana.mdf;Integrated Security=True;User Instance=True;Pooling=False"))
            {
                string SQL = @"Select * from rezerve Where ( rezerveID = @rzveID);";
                SqlCommand cmd = new SqlCommand(SQL, conn);
                cmd.Parameters.AddWithValue("@rzveID", ID);

                conn.Open();
                table.Load(cmd.ExecuteReader());
                conn.Close();
            }
            rzve rzrv= new rzve();
            // DataGridView dgv1 = new DataGridView { Dock = DockStyle.Fill };
            rzrv.Controls.Add(dataGridView1);
            rzrv.dataGridView1.DataSource = table;
             rzrv.Show();
           
            rzrv.dataGridView1.Columns["name"].HeaderText = "Name";
            rzrv.dataGridView1.Columns["surname"].HeaderText = "Surname";
            rzrv.dataGridView1.Columns["gender"].HeaderText = "Gender";
            rzrv.dataGridView1.Columns["date"].HeaderText = "Date";
Posted
Updated 9-Oct-14 3:06am
v4
Comments
Sinisa Hajnal 9-Oct-14 8:16am    
Transfer where? Also, winforms or webforms? What have you tried until now?
bora1891 9-Oct-14 8:27am    
Sorry,I forgot to write which forms..I work in c# winforms and my database is sql..
Sinisa Hajnal 9-Oct-14 8:38am    
Thank you, now please use Improve question link and give us more detailed description and some code. Thank you
bora1891 9-Oct-14 9:12am    
I use this code but I didn't transfer more than one row when I select 2 rows code will be accept the last one and show the last one in the datagridview

Int32 ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString());
MessageBox.Show("ID" + ID);
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Dorana.mdf;Integrated Security=True;User Instance=True;Pooling=False"))
{
string SQL = @"Select * from rezerve Where ( rezerveID = @rzveID);";
SqlCommand cmd = new SqlCommand(SQL, conn);
cmd.Parameters.AddWithValue("@rzveID", ID);

conn.Open();
table.Load(cmd.ExecuteReader());
conn.Close();
}
rzve rzrv= new rzve();
// DataGridView dgv1 = new DataGridView { Dock = DockStyle.Fill };
rzrv.Controls.Add(dataGridView1);
rzrv.dataGridView1.DataSource = table;
rzrv.Show();

rzrv.dataGridView1.Columns["name"].HeaderText = "Name";
rzrv.dataGridView1.Columns["surname"].HeaderText = "Surname";
rzrv.dataGridView1.Columns["gender"].HeaderText = "Gender";
rzrv.dataGridView1.Columns["date"].HeaderText = "Date";

C#
protected void btnClick(object sender, EventArgs e) {
string ids = String.Empty;
    foreach (DataRowView drv in DataGridView1.Rows) {
        if (drv[0].Checked) { // <-- you might have to cast here
ids += drv["rezerveID "];
        }
            // your query, ids goes into @rzveID placeWHERE rezerve_id IN @rzveID

    }
}



You might consider keeping checked row indexes collection on CheckBoxCellClick (use CellContentClick or checkedChange or some such). In that case you're not iterating through all rows in the grid like the above code, but you're introducing at least one more object (collection), event (checkedChanged) and logic for adding and removing the items.

I would go with simpler solution.

If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
bora1891 10-Oct-14 5:03am    
Thnxs for all but I tried your solution and I didn't solve my problem.I think my problem is ID..When I use ID in my query,program always will be accept the last selection because I select more than one rows with checkboxes but it will accept the last one how can I solve my problem ?..Please give an suggestion
Again thnxs for your help :)
Sinisa Hajnal 10-Oct-14 6:11am    
It seems we don't understand each other. Your code selects always single row. If you want to select more of them, you HAVE to iterate through selected rows...

What you need to do is create a list of selected IDs and modify your query like this:
Where ( rezerveID IN @rzveListID);
(where of course rzveListID is the list created in button click as above 1, 2, 3, 4, ..., n)


If you take time to describe your problem in more details, so that everyone understand what your problem is, the help will be that much faster. Right now, it is not clear that the code above is the source of your problem since you never once said WHAT DO YOU MEAN by TRANSFER selected rows...and there were questions "transfer where" and "whaddaya mean" etc...
bora1891 10-Oct-14 6:39am    
Hajnal,yes I mean tranfer selected rows.
My problem is to transfer data by multiple selection.For example, users makes multiple selection with checkboxes in the table and that you have selected the rows in the DataGrid will show in another form Datagrid in WindowsForms.So I used this code to transfer data from another form's datagrid.I succeed it but I didn't multiple transfer data.This is my code of transfer data.

Int32 ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString());
MessageBox.Show("ID" + ID);
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Dorana.mdf;Integrated Security=True;User Instance=True;Pooling=False"))
{
string SQL = @"Select * from rezerve Where ( rezerveID = @rzveID);";
SqlCommand cmd = new SqlCommand(SQL, conn);
cmd.Parameters.AddWithValue("@rzveID", ID);

conn.Open();
table.Load(cmd.ExecuteReader());
conn.Close();
}
rzve rzrv= new rzve();
// DataGridView dgv1 = new DataGridView { Dock = DockStyle.Fill };
rzrv.Controls.Add(dataGridView1);
rzrv.dataGridView1.DataSource = table;
rzrv.Show();

rzrv.dataGridView1.Columns["name"].HeaderText = "Name";
rzrv.dataGridView1.Columns["surname"].HeaderText = "Surname";
rzrv.dataGridView1.Columns["gender"].HeaderText = "Gender";
rzrv.dataGridView1.Columns["date"].HeaderText = "Date";
Sinisa Hajnal 10-Oct-14 6:49am    
Yes, yes, you mean transfer selected rows, but transfer WHERE!? Into the database, into another grid, what?

If it's just a matter of transfer to another grid, you can use ImportRow into that other grid datasource.

Like this:
foreach loop
newGrid.DataSource.ImportRow(originalGridRow) -- ImportRow is DataTable method so you'll need to cast accordingly.

And did you try my suggestion of creating list of IDs and then getting them from the database?
bora1891 10-Oct-14 8:59am    
I want to transfer into the another grid.But the grid has the other form.
And I tried your suggestion but I get an error.The Error message in the error list;
'object' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?).
Can you explain to me your this suggestion I don't know this way :/

Like this:
foreach loop
newGrid.DataSource.ImportRow(originalGridRow) -- ImportRow is DataTable method so you'll need to cast accordingly
My Project uses datagridviews. Instead of using checkboxes the user can select rows using the row headers (Ctrl+Shift for multi-select).

C#
this.dataGridView1.SelectionChanged += new EventHandler(dataGridViewDevices_SelectionChanged);


void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
            DataGridViewSelectedRowCollection rows = this.dataGridViewDevices.SelectedRows;
            int n = rows.Count;

//copy items

for (int i = 0; i < n; i++)
{
object item = rows[i].Cells[0].Value; //get cells by row index or Column Name
//copy to new grid or datatable bound to other grid                      
}

}
 
Share this answer
 
Comments
bora1891 10-Oct-14 5:04am    
Thnxs for all but I tried your solution and I didn't solve my problem.I think my problem is ID..When I use ID in my query,program always will be accept the last selection because I select more than one rows with checkboxes but it will accept the last one how can I solve my problem ?..Please give an suggestion
Again thnxs for your help :)

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