我有2个表格
ApodeikseisTimologion
是主要表单,EggrafesTimologionEsodon
是每个表单中的子表单和1 datagridview(dataGridViewProionApodeixeisTimologiouEsodon
和dataGridViewEggrafesProionParastikouEsodon
)。
DataGridViewProionApodeixeisTimologiouEsodon
属于主要表单,dataGridViewEggrafesProionParastikouEsodon
属于子表单。
现在,我想将选中的值从子表单传递给父表单,而不会丢失我已经放在父表单中的其他文本框中的值。
这是我的代码:
表格ApodeikseisTimologion。 我用它来打开子表单
private void LinkLblEisagogiEggrafon_Click(object sender, EventArgs e) {
EggrafesTimologionEsodon eggrTimolEsodon = new EggrafesTimologionEsodon(cmbBoxEponimiaPelatiApodeixeisTimologiouEsodon.Text);
eggrTimolEsodon.ShowDialog();
this.Refresh();
}
表格EggrafesTimologionEsodon
private void EggrafesTimologionEsodon_Load(object sender, EventArgs e) {
try {
con = new SqlConnection();
con = DBAccess.Conn;
con.Open();
adap = new SqlDataAdapter("select ProionParastatikou.* from ProionParastatikou inner join Parastatiko on ProionParastatikou.ParastatikoID = Parastatiko.ParastatikoID" +
" where ProionParastatikou.Ypoloipo > 0.00 and Parastatiko.Eponimia = '" + txtPelatisId.Text + "'", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "BsProionParastatikouEsodon");
dataGridViewEggrafesProionParastikouEsodon.DataSource = ds.Tables[0];
con.Close();
}
catch (Exception ex) {
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
if (MyConn.State != ConnectionState.Closed)
MyConn.Close();
}
}
我有这张图片:
在子窗体中,我还可以检查datagridview的checkbox列中的行,并将它们存储到列表中,以将它们传输到父窗体,而不会丢失我已经存在的其他数据。
private void BtnProsthikiEggrafon_Click(object sender, EventArgs e) {
ApodeixeisTimologion apo = new ApodeixeisTimologion(null);
List<Int64> lst2send = new List<Int64>();
Int64 toSend;
foreach (DataGridViewRow eggrafes in dataGridViewEggrafesProionParastikouEsodon.Rows) {
if (Convert.ToBoolean(eggrafes.Cells["CheckBoxColumn"].Value)) {
int RowIndexCheck = eggrafes.HeaderCell.RowIndex;
Int64.TryParse(dataGridViewEggrafesProionParastikouEsodon.Rows[RowIndexCheck].Cells["proionParastatikouIDDataGridViewTextBoxColumn"].Value.ToString(),out toSend);
lst2send.Add(toSend);
}
}
apo.AfterProsthikiProionParastatikou(null, null, lst2send);
//apo.Refresh();
this.Close();
}
当儿童表格关闭时,我会以父表格形式:
public void AfterProsthikiProionParastatikou(object sender,EventArgs e,IList<Int64> lst) {
String csvLst = string.Empty;
int count = 0;
foreach ( Int64 lstItems in lst) {
if (count == 0) {
csvLst = lstItems.ToString();
count += 1;
}
else {
csvLst = csvLst + "," + lstItems.ToString();
}
}
_con = new SqlConnection();
_con = DBAccess.Conn;
_con.Open();
adap = new SqlDataAdapter("select ProionParastatikou.* from ProionParastatikou where" +
" ProionParastatikou.Ypoloipo > 0.00 and ProionParastatikou.ProionParastatikouID"+
" in( " + csvLst + ")", _con);
ds = new System.Data.DataSet();
adap.Fill(ds, "bsProionApodeixeisTimologiouEsodon");//
dataGridViewProionApodeixeisTimologiouEsodon.DataSource = ds.Tables["bsProionApodeixeisTimologiouEsodon"];
//dataGridViewProionApodeixeisTimologiouEsodon.Refresh();
_con.Close();
}
我在
中创建一个断点ds = new System.Data.DataSet();
adap.Fill(ds, "bsProionApodeixeisTimologiouEsodon");//
dataGridViewProionApodeixeisTimologiouEsodon.DataSource = ds.Tables["bsProionApodeixeisTimologiouEsodon"];
我看到ds填充了正确的值,但是行没有出现在dataGridViewProionApodeixeisTimologiouEsodon中,这是父窗体中的网格。
在我使用datagrids中的数据集和bindingSources的所有表单中,除了checkbox列之外,datagrids是有界的。 有人可以帮忙吗? 如果有人需要更多帮助,我会给予它。
答案 0 :(得分:0)
这是一个极小但有效的样本。 在Form2上拥有一个公共属性,并从Form1访问它。我的代码片段正在传递一个字符串,但是你的代码片段将处理不同的数据类型。相同的概念,不同的数据类型。
Form1.cs的
using System;
using System.Windows.Forms;
namespace PassParamsFromForm2ToForm1_45997869
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2("blah blah blah");
f2.ShowDialog();
label1.Text = f2.returnValue;
}
}
}
Form2.cs
using System;
using System.Windows.Forms;
namespace PassParamsFromForm2ToForm1_45997869
{
public partial class Form2 : Form
{
public string returnValue;
private string submittedString { get; set; }
public Form2(string incomingString)
{
InitializeComponent();
submittedString = incomingString;
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
returnValue = "I'd rather show you my value instead of yours(" + submittedString + ")...";
this.Close();
}
}
}
使用网格类型
Form1.cs的
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace PassParamsFromForm2ToForm1_45997869
{
public partial class Form1 : Form
{
BindingList<gridentry> gridList = new BindingList<gridentry>();
DataGridView dgv = new DataGridView();
DataGridView gridViewForTheReturnedRows;
public Form1()
{
InitializeComponent();
initializeGrid();
addDataToGridSource("name1");
addDataToGridSource("name2");
addDataToGridSource("name3");
addDataToGridSource("name4");
button1.Click += Button1_Click;
}
private void addDataToGridSource(string incomingString)
{
gridList.Add(new gridentry { col1 = incomingString, col2 = incomingString + "in col2", col3 = incomingString + "in col3" });
}
private void initializeGrid()
{
dgv.Location = new System.Drawing.Point(this.Location.X + 5, this.Location.Y + 5);
this.Controls.Add(dgv);
dgv.AutoGenerateColumns = true;
dgv.DataSource = gridList;
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(dgv);
f2.ShowDialog();
if (f2.returnRows != null && f2.returnRows.Count > 0)
{
gridViewForTheReturnedRows = new DataGridView();
gridViewForTheReturnedRows.ColumnCount = f2.returnRows[0].Cells.Count;
gridViewForTheReturnedRows.Rows.InsertRange(0, f2.returnRows.ToArray());
gridViewForTheReturnedRows.Location = new System.Drawing.Point(10, dgv.Location.Y + dgv.Height + 5);
this.Controls.Add(gridViewForTheReturnedRows);
}
}
}
public class gridentry
{
public string col1 { get; set; }
public string col2 { get; set; }
public string col3 { get; set; }
}
}
Form2.cs
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace PassParamsFromForm2ToForm1_45997869
{
public partial class Form2 : Form
{
public DataGridView returnGrid = new DataGridView();
public List<DataGridViewRow> returnRows { get; set; }
private DataGridView submittedGrid { get; set; }
public Form2(DataGridView incomingGrid)
{
InitializeComponent();
submittedGrid = incomingGrid;
submittedGrid.Location = new System.Drawing.Point(this.Location.X + 5, this.Location.Y + 5);
Controls.Add(submittedGrid);
button1.Click += Button1_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
returnGrid = submittedGrid;//if you want to return the grid
//returning rows
/*
* You'll need your own mechanism to differentiate between
*/
bool rowIsModified = false;
returnRows = new List<DataGridViewRow>();
foreach (DataGridViewRow item in submittedGrid.Rows)
{
if (item.Index % 2 != 0)
{
rowIsModified = true;
}
if (rowIsModified)
{
DataGridViewRow r = (DataGridViewRow)item.Clone();
for (int i = 0; i < item.Cells.Count; i++)
{
r.Cells[i].Value = item.Cells[i].Value;
}
returnRows.Add(r);
}
}
this.Close();
}
}
}
答案 1 :(得分:0)
表格ApodeikseisTimologion。我用它来打开子表单
2017-09-18 16:22:53.176524+0530 *** ***[359:20063] Task <5F9376DB-9335-
4A45-B3F0-1D6FD69A19A3>.<29> finished with error - code: -999
2017-09-18 16:22:53.178923+0530 *** ***[359:20142] Task <0DE282EA-3FBD-
4036-8298-C75EFA65F15A>.<40> HTTP load failed (error code: -999 [1:89])
2017-09-18 16:22:53.179821+0530 *** *[359:20063] Task <BE2D8BAE-FFB8-
43CA-8723-111326DEF4FD>.<31> finished with error - code: -999
2017-09-18 16:22:53.180089+0530 *** ***[359:20063] Task <E7C819D4-C11A-
4915-B021-A73F31BE89CD>.<33> finished with error - code: -999
2017-09-18 16:22:53.180365+0530 *** ***[359:20063] Task <3B871761-B006-
4220-B857-6204B385AD34>.<34> finished with error - code: -999
2017-09-18 16:22:53.180523+0530 *** ***[359:20142] Failed to get
TCPIOConnection in addInputHandler.
表格EggrafesTimologionEsodon
private void LinkLblEisagogiEggrafon_Click(object sender, EventArgs e)
{
if (cmbBoxEponimiaPelatiApodeixeisTimologiouEsodon.Text == "")
{
MessageBox.Show("Πρέπει να επιλέξετε Πελάτη", "Προειδοποίηση");
}
else
{
EggrafesTimologionEsodon eggrTimolEsodon = new EggrafesTimologionEsodon(cmbBoxEponimiaPelatiApodeixeisTimologiouEsodon.Text);//p
eggrTimolEsodon.ShowDialog();
listToFill = eggrTimolEsodon.lst2send;
AfterProsthikiProionParastatikou(null,null,listToFill);
//AfterProsthikiProionParastatikou(null);
//this.Refresh();
}
}
由此我得到了最顶层的图像。
private void EggrafesTimologionEsodon_Load(object sender, EventArgs e)
{
//CmbBoxPelatis.SelectedIndex = -1;
//EggrafesTimologionEsodon values = new EggrafesTimologionEsodon();
try
{
con = new SqlConnection();
con = DBAccess.Conn;
con.Open();
adap = new SqlDataAdapter("select ProionParastatikou.* from ProionParastatikou inner join Parastatiko on ProionParastatikou.ParastatikoID = Parastatiko.ParastatikoID" +
" where ProionParastatikou.Ypoloipo > 0.00 and Parastatiko.Eponimia = '" + txtPelatisId.Text + "'", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "BsProionParastatikouEsodon");
dataGridViewEggrafesProionParastikouEsodon.DataSource = ds.Tables[0];
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (MyConn.State != ConnectionState.Closed)
MyConn.Close();
}
}
当儿童表格关闭时,我会以父表格形式:
public List<Int64> lst2send;
private void BtnProsthikiEggrafon_Click(object sender, EventArgs e)
{
lst2send = new List<Int64>();
Int64 toSend;
foreach (DataGridViewRow eggrafes in dataGridViewEggrafesProionParastikouEsodon.Rows)
{
if (Convert.ToBoolean(eggrafes.Cells["CheckBoxColumn"].Value))
{
int RowIndexCheck = eggrafes.HeaderCell.RowIndex;
Int64.TryParse(dataGridViewEggrafesProionParastikouEsodon.Rows[RowIndexCheck].Cells["proionParastatikouIDDataGridViewTextBoxColumn"].Value.ToString(),out toSend);
lst2send.Add(toSend);
}
}
this.Close();
}
我正在得到我想要的结果 enter image description here