![](/img/trans.png)
[英]How can I pass multiple cell values of datagridview from one form to another at same time in C#?
[英]How can I pass multiple records from one form to another form in C#?
我有两种形式, form1
和form2
。 每当我在form1中双击datagrid的单元格时,它将进入form 2,并在datagrid中显示一些项目和价格。 我在那里选择一行,并且该值正在通过1 datagridview传递。 但是我想传递第二个值,也覆盖编写形式1的第一个传递值。我想在下一行中添加它。 我需要做什么?
这是我的表格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;
}
用按钮单击传递选定的行以形成表格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();
}
表格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;
}
因此,值是从表格2传递的,但每次都会在第一行覆盖。 但是我想在传递之前在form1中添加新行。也许我想在这里使用一些循环,但我不知道如何做。 为了循环,我还声明了一个整数。 我用谷歌搜索,但最终没有得到很好的结果。
您必须在form1中添加一个可以在form2中访问的列表。
定义Form1 b = null;
作为全局变量
添加b = new Form1();
Form2_Load函数
添加b.list.Add(dataGridView1.SelectedRows[0]);
ShowDialog()之前的button1_click函数;
仅保留b.ShowDialog();
button1_click函数中的行
添加public List<DataGridViewRow> list = new List<DataGridViewRow>();
到form1
我们还可以有另一个选择是创建具有泛型列表的静态类。 我们可以将该列表用作临时存储。
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; }
}
}
此类具有公共静态SetRow设置程序。 使用此设置器将数据保存在全局列表“ _globalVar”中。
此类还具有随时重置该列表的方法。 只需调用该静态方法即可重置该网格。
如何使用
这是获取数据并传递给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();
}
}
接收并以表格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.