user4861279
user4861279

Reputation: 53

pass datagridview from one form to another c#

i want to pass my datagridview from form1 to form2.I've tried with constructor but without result, datagridview in second form is empty. Could someone help me here, i'm stacked for hours.I dont use sql and i dont need to use dataTable.Here is my code:

I fill datagridview3 on cellClick event of datagridview2. When I click on dayagridview2 cellClick_event my datagridview3 is filled with this method:

 private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataD d = new DataD();
        d.Mat(dataGridView1,dataGridView2,dataGridView3);

    }

Here is method from which is filled dataGridView3:

   public void Mat(DataGridView dataGridView1, DataGridView dataGridView2,DataGridView dataGridView3)
    {
        Named n = new Named();

        foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
        {
            IList<String> lista = new List<String>();
            n.Data = string.Empty;
            n.Data2 = string.Empty;
            int indexOfYourColumn = 9;
            int index2 = 0;
            var restaurantList = new List<Named>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                n.Data = row.Cells[indexOfYourColumn].Value.ToString();

                if (cell.Value.ToString() == n.Data.ToString())
                {

                    restaurantList.Add(new Nalozi()
                    {
                        Data = row.Cells[indexOfYourColumn].Value.ToString(),
                        Data2 = row.Cells[index2].Value.ToString()
                    });


                }
            }


            dataGridView3.DataSource = restaurantList;
        }


    }

So now i just need on buttnClick to show this dataGridView3 in another form.

Upvotes: 1

Views: 10071

Answers (1)

Adnan Umer
Adnan Umer

Reputation: 3689

If you want to pass DataGridView from one form to another you might have to pass DataGridView.DataSource of DataGridView from one Form to other. Something like that

new SecondForm(dataGridView.DataSource)

and your SecondForm will accepts passed DataSource and pass that to DataGridView of that form

class SecondForm
{
    public SecondForm(object dataSource)
    {
        InitializeComponents();

        dataGridView.DataSource = dataSource;
    }
}

If you want to pass copy of DataSource you can create a new DataTable from existing data inside DataGridView of FirstForm

private DataTable GetDataTableFromDGV(DataGridView dgv)
{
    var dt = new DataTable();
    foreach (DataGridViewColumn column in dgv.Columns)
    {
        if (column.Visible)
        {
            // You could potentially name the column based on the DGV column name (beware of dupes)
            // or assign a type based on the data type of the data bound to this DGV column.
            dt.Columns.Add();
        }
    }

    object[] cellValues = new object[dgv.Columns.Count];
    foreach (DataGridViewRow row in dgv.Rows)
    {
        for (int i = 0; i < row.Cells.Count; i++)
        {
            cellValues[i] = row.Cells[i].Value;
        }
        dt.Rows.Add(cellValues);
    }

    return dt;
}

and update first call to this

new SecondForm(GetDataTableFromDGV(dataGridView))

Upvotes: 4

Related Questions