如何在C#中将formgridview值从form1传递到form 2 [英] How to pass datagridview values from form1 to form 2 in C#
本文介绍了如何在C#中将formgridview值从form1传递到form 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Once i Click the payment button and then open the form like popup, and then how to pass datagridview and invoice number values from that popup to insert into database using c#. Please give me someone for proper solution
我尝试过:
What I have tried:
else
{
SalesPay popup = new SalesPay();
popup.NetTotal = netPrice.Text;
popup.Netgst = gstPrice.Text;
this.dataGridColumns();
DialogResult dialogResult = popup.ShowDialog();
}
}
private void dataGridColumns()
{
for (int i = 0; i <= metroGrid1.Rows.Count - 1; i++)
{
string prodName = metroGrid1.Rows[i].Cells[0].Value.ToString();
string prodRetailPrice = metroGrid1.Rows[i].Cells[1].Value.ToString();
string prodQuantity = metroGrid1.Rows[i].Cells[2].Value.ToString();
string prodTotalPrice = metroGrid1.Rows[i].Cells[3].Value.ToString();
}
}
推荐答案
与使用OpenFileDialog的方式相同或者类似:你在弹出窗口中创建属性,当弹出窗口关闭时,父窗体通过实例弹出窗口进行访问。
因为你使用ShowDialog,所以父代码将在该行冻结,然后在用户关闭弹出窗口时继续使用以下代码行。
The same way you do with a OpenFileDialog or similar: you create properties in the pop-up which the "parent" form accesses via the pop-up from instance when the pop-up closes.
Because you use ShowDialog, the "parent" code will freeze at that line and then continue with the following line of code when the user closes the pop-up.
这是使用静态 BindingList 的解决方案,程序代码:
Here is a solution using a static BindingList, Program code:
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace TestForm1
{
static class Program
{
public static BindingList<MyClass> masterBindingList;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//// Test data grid with BindingList.
Application.Run(new Form1());
}
/// <summary>
/// Simple class with one string.
/// </summary>
public class MyClass
{
public string Title { get; set; }
}
}
}
Form1代码:
Form1 code:
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestForm1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Init();
}
private void Init()
{
Program.masterBindingList = new BindingList<Program.MyClass>();
this.dataGridViewMaster.DataSource = Program.masterBindingList;
}
private void ButtonAddRowClick(object sender, EventArgs e)
{
var rowIndex = this.dataGridViewMaster.RowCount;
this.AddRow(rowIndex);
this.ScrollToRow(rowIndex);
}
/// <summary>
/// Add new row via the BindingList.
/// </summary>
private void AddRow(int rowIndex)
{
var myRow = new Program.MyClass { Title = "Row " + rowIndex.ToString() };
Program.masterBindingList.Add(myRow);
}
private void ScrollToRow(int rowIndex)
{
this.dataGridViewMaster.ClearSelection();
this.dataGridViewMaster.FirstDisplayedScrollingRowIndex = rowIndex;
this.dataGridViewMaster.Focus();
}
}
}
您可以将Form2设置为共享BindingList,如下所示:
You can set your Form2 to the shared BindingList as follows:
private void buttonShow_Click(object sender, EventArgs e)
{
var form2 = new Form2();
// form2.dataGridView1 Modifiers set to public in Designer.
form2.dataGridView1.DataSource = Program.masterBindingList;
form2.ShowDialog();
}
当然你也可以在 InitializeComponent()
之后在Form2中设置DataSource。
Of course you can also set the DataSource in Form2 after InitializeComponent()
.
这篇关于如何在C#中将formgridview值从form1传递到form 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文