简体   繁体   中英

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM