0

i am working with Desktop Application and i am trying to create a datatable from a datagridview. but i only want to add those rows when checkbox is selected or checked. currently i created a datatable from a whole datagridview like this.

DataTable tbl = (DataTable)dataGridView1.DataSource;

I have added a checkbox coloumn inside datagridview.. so please guide me how to add selected rows to a new datatable

Thanks.. Here is the new code Update

for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                    {
                        DataRow dr0 = tbl.NewRow();
                        dr0["ID"] = dataGridView1.Rows[i].Cells["ID"].Value;
                        dr0["BikeCC"] = dataGridView1.Rows[i].Cells["BikeCC"].Value.ToString();
                        dr0["ChassisModel"] = dataGridView1.Rows[i].Cells["ChassisModel"].Value.ToString();
                        dr0["BikeCC"] = dataGridView1.Rows[i].Cells["BikeCC"].Value.ToString();
                        dr0["ChassisModel"] = dataGridView1.Rows[i].Cells["Chassismodel"].Value.ToString();
                        tbl.Rows.Add(dr0);

                    }
                }

                objMdl.RecordTable = tbl;

This is the way i could find to add selected rows into a new datatable... it is lenghty but i am still facing little issues. which i will post in a bit for your look.

5
  • Add Event datagridview_DataError Commented Sep 18, 2013 at 7:08
  • And your code works now? In fact your code is some kind of traditional, it's of course lengthy, however you can still use ImportRow to copy the row instead of assigning cells yourself. Commented Sep 18, 2013 at 8:19
  • Yes, it does do the required job, but additionally it is adding copy of all rows to the data grid.. do i have to clear the datatable each time?? Commented Sep 18, 2013 at 9:20
  • 1
    remember to init tbl as ((DataTable)dataGridView1.DataSource).Clone and check the condition in if-statement, I doubt it's true in all the loops. Commented Sep 18, 2013 at 10:19
  • Thanks alot for your help and time.. yes .Clone did work.. it is solved now :) Commented Sep 18, 2013 at 10:36

1 Answer 1

1

Suppose your checkbox column is named yourCheckBoxColumn:

DataTable tbl = ((DataTable)dataGridView1.DataSource).Clone();//Clone structure first
var rows = dataGridView1.Rows.OfType<DataGridViewRow>()
                        .Where(r=>Convert.ToBoolean(r.Cells["yourCheckBoxColumn"].Value))
                        .Select(r=>((DataRowView)r.DataBoundItem).Row);
foreach(var row in rows)
   tbl.ImportRow(row);
Sign up to request clarification or add additional context in comments.

20 Comments

Problem when i type in my check box column.. it gives error that column does not belong to table tbl.
Why do you type in CheckBoxColumn? it's for mouse interaction, anyway I still don't get what your problem is now.
my Checkbox column name is check when i type "Check" for the column name it doesnt find this name inside the datagrid.
@RanaDotnet check or Check? the case-sensitivity is important. Could you show the code to add your checkbox column?
and when i give index of the column, i get error saying Specific cast is not valid.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.