Psyko-Mike
Psyko-Mike

Reputation: 1

How to change a Datagridviewcombboxcell by another Datagridview

I got two different Datagridviews. Datagridview2 has just text columns. Datagridview1 contains mostly text columns and one DatagridviewComboBoxColumn. The comboxbox contains a list of all values from one column in Datagridview2. I want to update each comboboxcell everytime Datagridview2 changes. I’ve a read and tried a lot but i still haven’t found a way how i can change the values of a DatagridviewComboboxcell during runtime with content from another Datagridview.

To make things a bit more clear, here is a little code-snippet of how i want to change the content on a specific event (this code don’t work)

var cls = new List<string>();

        if (dataGridView2.Rows.Count > 0)
        {

            for (int rows = 0; rows < dataGridView2.Rows.Count; rows++)
            {

                if (dataGridView2[0, rows].Value != null)
                {

                    cls.Add(dataGridView2[0, rows].Value.ToString());

                }

            }

            for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
            {

                DataGridViewComboBoxCell cl = new DataGridViewComboBoxCell();
                var cl_content = cls;
                cl.DataSource = cl_content;

                var cell = dataGridView1[1, rows] as DataGridViewComboBoxCell;


                cell.DataSource = cl;

            }

        }

I’ve tried some ideas from another post

Dynamically setting DataGridViewComboBoxCell's DataSource to filtered DataView based off of other cell selection

but nothing worked.

Upvotes: 0

Views: 53

Answers (1)

Pabinator
Pabinator

Reputation: 1641

Maybe just copy the contents from one dataGridView to the other dataGridView when you finish editing the dataGridView.

Here is a copy dataGridViews function:

public void CopyDataGridViews(DataGridView SourceDGV, DataGridView DestinationDGV)
{
    // Clear Destination DataGridView
    DestinationDGV.Columns.Clear();
    DestinationDGV.Rows.Clear();

    // Create Destination Columns
    for (int j = 0; j < SourceDGV.Columns.Count; j++)
    {
        DestinationDGV.Columns.Add(SourceDGV.Columns[j].Clone() as DataGridViewColumn);
    }

    // Create Destination Rows
    for (int i = 0; i < SourceDGV.Rows.Count - 1; i++)
    {
        DestinationDGV.Rows.Add(SourceDGV.Rows[i].Clone() as DataGridViewRow);
    }

    // Copy Data to Destination
    for (int column = 0; column < SourceDGV.Columns.Count; column++)
    {
        for (int row = 0; row < SourceDGV.Rows.Count; row++)
        {
            DestinationDGV.Rows[row].Cells[column].Value = SourceDGV.Rows[row].Cells[column].Value;
        }
    }
}

Upvotes: 0

Related Questions