简体   繁体   English

如何在C#中将多个记录从一种形式传递到另一种形式?

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

I have 2 forms, form1 and form2 . 我有两种形式, form1form2 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. 每当我在form1中双击datagrid的单元格时,它将进入form 2,并在datagrid中显示一些项目和价格。 I am selecting one row there and that value is passing form 1 datagridview. 我在那里选择一行,并且该值正在通过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. 但是我想传递第二个值,也覆盖编写形式1的第一个传递值。我想在下一行中添加它。 What do I have to do? 我需要做什么?

Here is my code of form 2 这是我的表格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 用按钮单击传递选定的行以形成表格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 表格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. 因此,值是从表格2传递的,但每次都会在第一行覆盖。 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. 但是我想在传递之前在form1中添加新行。也许我想在这里使用一些循环,但我不知道如何做。 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. 您必须在form1中添加一个可以在form2中访问的列表。

Define Form1 b = null; 定义Form1 b = null; as global variable 作为全局变量

Add b = new Form1(); 添加b = new Form1(); Form2_Load function Form2_Load函数

Add b.list.Add(dataGridView1.SelectedRows[0]); 添加b.list.Add(dataGridView1.SelectedRows[0]); to button1_click function before ShowDialog(); ShowDialog()之前的button1_click函数;

Keep only b.ShowDialog(); 仅保留b.ShowDialog(); line in button1_click function button1_click函数中的行

Add public List<DataGridViewRow> list = new List<DataGridViewRow>(); 添加public List<DataGridViewRow> list = new List<DataGridViewRow>(); to form1 到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. 此类具有公共静态SetRow设置程序。 Use this setter to save data in global list which is "_globalVar ". 使用此设置器将数据保存在全局列表“ _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. 这是获取数据并传递给form1的form2代码。

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 接收并以表格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
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? 我可以在C#中将对象列表从一个表单传递到另一个表单吗? - Can I pass a List of objects from one Form to another in C#? 在C#中将值从一种形式传递到另一种形式 - Pass value from one form to another form in c# 如何在C#中从另一个表单更新一个表单上的标签? - How can I update a label on one form from another form in C#? 如何将值从一种形式传递到另一种形式,而无需在C#中创建形式的新实例? - how to pass a values from one form to another form without creating new instance of a form in c#? 找不到如何将变量从一种形式传递给另一种C# - Can't find how to pass a variable from one form to another c# 如何通过逗号C#将mysql中的记录加载到另一种形式的一种形式 - How to load records in mysql to one form from another form by separate by commas C# 如何将值从一种形式传递到另一种形式? - How can I pass values from one form to another? 如何将2个变量从一种形式传递给另一种形式? - How can I pass 2 variables from one form to another? 如何将C#表单中的值传递给Python - How can i pass values from a C# form into Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM