Hello,

I have a datagridview and I allow a user to fill in some checkboxes which call stored procedures to show only certain data based on the criteria given by the user.

I then want to take all that data that is currently shown in the datagridview and copy it into a new datatable. I then plan on adding that table to a dataset so that I can display the data from the datagridview in a Crystal Report.

So any idea on how to copy the rows from a datagridview into a new datatable?

I have spent hours on google trying to find a solution, and I was not able to find a good solution.

A HUGE thanks in advance.

Best regards, Arelius.

I know that i have to use either of these loops to iterate through the rows, but what do i put in the loop?

DataSet1 ds1 = new DataSet1();
            DataTable dt = new DataTable();

            for (int i = 0; i < this.dataGridViewTasksReport.NewRowIndex; i++)
            {
                //what goes here?
                //need to put row in datatable
            }

             // or use this loop
            foreach (DataGridViewRow dgvr in dataGridViewTasksReport.Rows)
            {
                //what goes here?
                //need to put row in datatable
            }

            ds1.Tables.Add(dt);

This should do the trick for you.

private void frmGridView_Load(object sender, EventArgs e)
    {
      this.customerTableAdapter.Fill(this.dataSet1.Customer);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      dt.TableName = "MyTable";
      foreach (DataGridViewColumn col in dataGridView1.Columns)
      {
        dt.Columns.Add(col.DataPropertyName, col.ValueType);
      }
      foreach (DataGridViewRow gridRow in dataGridView1.Rows)
      {
        if (gridRow.IsNewRow)
          continue;

        DataRow dtRow = dt.NewRow();
        for (int i1 = 0; i1 < dataGridView1.Columns.Count; i1++)
          dtRow[i1] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i1].Value);
        dt.Rows.Add(dtRow);
      }
      ds.Tables.Add(dt);
      System.Diagnostics.Debugger.Break();
    }

col.DataPropertyName may not be populated if the grid isn't databound. You could use the columns header value or any other string for the name.

Excellent! Thanks alot!

Are you also familiar with crystal reports at all?

No, i'm not a big fan of their reports.

Please mark this thread as solved if your have found a solution to your question and good luck!

DataTable newTable = (DataTable)dataGridView1.DataSource;

it is basically casting the datasource of the grid view.

i hope it helps.

How can I copy a datagridview to a gridview.. I only ask b/c I need to use the redwecontrol and its not avaiable using datagridview. And Gridview is not an option for me to use in a windows application.. BUT the class is.. very odd but anyway. All the colors formatting etc.. I need to pass to the gridview. Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.