Question

private void buttonFilterA_Click(object sender, EventArgs e)
    {

        List<DataGridViewRow> lstRows = new List<DataGridViewRow>();
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            object bloodFiltered = dr.Cells[txtBloodGroup.Index].Value;
            if (bloodFiltered != null && bloodFiltered.ToString() == "A")
            {
                lstRows.Add(dr);                    
            }
        }

        foreach (DataGridViewRow dr in lstRows)
        {
            secondForm.dataGridView2.Rows.Add(dr);                
        }
        secondForm.ShowDialog();                        
    }

Hi guys, all I did in this part of programming is filtering the Type of blood group and I wanted to show it in another empty datagridview from Form2. It works well if I just remove the row, but then I met problems when I add row inside the datagridview in the Form2. It just didn't work.

Here is the problem:

An unhandled exception of type 'System.InvalidOperationException' occurred in
System.Windows.Forms.dll

No row can be added to a `DataGridView` control that does not have columns. Columns must be added first.

XXXXXSOLVEDXXXXX

private void buttonFilterA_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewColumn dc in dataGridView1.Columns)
        {
            secondForm.dataGridView2.Columns.Add(dc.Name, dc.HeaderText);
        }
        List<DataGridViewRow> lstRows = new List<DataGridViewRow>();
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            object bloodFiltered = dr.Cells[txtBloodGroup.Index].Value;
            if (bloodFiltered != null && bloodFiltered.ToString() == "A")
            {
                lstRows.Add(dr);                    
            }
        }            
        foreach (DataGridViewRow dr in lstRows)
        {
            var newRow = (DataGridViewRow)dr.Clone();
            foreach (DataGridViewCell cell in dr.Cells)
            {
                newRow.Cells[cell.ColumnIndex].Value = cell.Value;
            }
            secondForm.dataGridView2.Rows.Add(newRow);                              
        }
        secondForm.ShowDialog();                        
    }

Thanks to the great help from Grant Winney, now this code is able to display the filtered row on the datagridview in the Form2 without interupting the original data on the datagridview in Form1(with Cell Leave event of serialization).

Était-ce utile?

La solution

From your comment, it looks like you already figured out the first issue - adding columns.

You should be able to copy them all using a loop:

foreach (DataGridViewColumn col in dataGridView1.Columns)
    dataGridView2.Columns.Add(col.Name, col.HeaderText);

As for the second error, you can't add a DataRow from the first grid directly to the second, but you can clone it and then add that. Cloning doesn't copy the cells values, but you can do that in a loop too.

This snippet clones the first row from one grid to the other:

var newRow = (DataGridViewRow)dataGridView1.Rows[0].Clone();

foreach (DataGridViewCell cell in dataGridView1.Rows[0].Cells)
    newRow.Cells[cell.ColumnIndex].Value = cell.Value;

dataGridView2.Rows.Add(newRow);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top