简体   繁体   中英

How can I pass multiple records from one form to another form in C#?

I have 2 forms, form1 and form2 . Whenever I double click on a cell of datagrid in form1, it will go form 2 and there I am displaying some items and price of them in a datagrid. I am selecting one row there and that value is passing form 1 datagridview. but I want to pass second valu that also over writing first passed value of form 1. I want to add that in next row. What do I have to do?

Here is my code of form 2

private void Form2_Load(object sender, EventArgs e)// loading data to datagrid from database file
    {
        SqlDataAdapter sda = new SqlDataAdapter(@"select     brnDb.compname as [company name], brnDb.catname as [category Name],     itemDB.fullname as [Item Name], itemDb.itmbyp as [Buying Price], itemDB.itmdlrp [Dealer Price],itemDB.itmmrp as [MRP],itemDb.itmunit as [Unit Of Measure], itemDB.itmml [Liters], itemDB.itmgr[KGs], itemDb.itmpc[Units] from brnDB inner join itemDb on brnDb.brname=itemDB.brname order by itemDB.fullname asc", con);
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }

passing selected row with a button click to form 1

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 b = new Form1(dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),
                            dataGridView1.SelectedRows[0].Cells[3].Value.ToString(),
                            dataGridView1.SelectedRows[0].Cells[4].Value.ToString(),
                            dataGridView1.SelectedRows[0].Cells[5].Value.ToString());
        b.ShowDialog();
    }

Code of form 1

 public Form1(string Item_Name, string Buying_Price, string Dealrer_Price, string MRP )
    {
        InitializeComponent();
        int n;


        n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells[1].Value = Item_Name;
        dataGridView1.Rows[n].Cells[3].Value = Buying_Price;
        dataGridView1.Rows[n].Cells[4].Value = Dealrer_Price;
        dataGridView1.Rows[n].Cells[5].Value = MRP;
    }

So value is being passed from form 2 but overwriting in first row each time. But I want to add new row in form1 before passing.Maybe I want to use some loops here but I don't know how. for looping purpose I took declared a integer also. I googled for that but end up with no good result.

You have to add a List in form1 which can be accessed in form2.

Define Form1 b = null; as global variable

Add b = new Form1(); Form2_Load function

Add b.list.Add(dataGridView1.SelectedRows[0]); to button1_click function before ShowDialog();

Keep only b.ShowDialog(); line in button1_click function

Add public List<DataGridViewRow> list = new List<DataGridViewRow>(); to form1

We can also have another option is to create static class that have generic list . We can use that list as temporary storage.

static class Global
{
    private static List<GridRows> _globalVar = new List<GridRows>();

    public static void ResetGridData()
    {
        _globalVar = new List<GridRows>();
    }
    public static GridRows SetRow
    {
        set { _globalVar.Add(value); }
    }

    public static List<GridRows> GetSetting { get { return _globalVar; } }

    public class GridRows
    {
        public string Cell_1 { get; set; }

        public string Cell_2 { get; set; }

        public string Cell_3 { get; set; }

        public string Cell_4 { get; set; }
    }
}

This class have public static SetRow setter. Use this setter to save data in global list which is "_globalVar ".

This class also have method to reset that list in any point. Just call that static method to reset that grid.

How to use

This is form2 code that get data and pass to form1.

public partial class Form2 : Form
{
    public Form2()
    {
        //To reset our temporary store on load
        Global.ResetGridData();
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Send data from form 2 to form 1
        Form1 b = new Form1(dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),
        dataGridView1.SelectedRows[0].Cells[3].Value.ToString(),
        dataGridView1.SelectedRows[0].Cells[4].Value.ToString(),
        dataGridView1.SelectedRows[0].Cells[5].Value.ToString());
        b.ShowDialog();

    }
}

Receive and save data to our global list in form 1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public Form1(string cell1, string cell2, string cell3, string cell4)
    {
        InitializeComponent();
        //Save data to our list using global class that
        Global.SetRow = new Global.GridRows()
        {
            Cell_1 = cell1,
            Cell_2 = cell2,
            Cell_3 = cell3,
            Cell_4 = cell4
        };
    }
}

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