如何将所选Xtragridview的值传递到另一个表单上的文本框 [英] How to pass values of the selected Xtragridview to text boxes on to another form
问题描述
在我的代码示例下面找到了我用来将gridview中选定行的值传递给另一种形式的文本框的代码;
有一种情况:我有2个表单,第一个表单包含一个按钮和文本框,该按钮打开第二个表单"form 2". form2包含Xtragrid,从此表单中我想执行双击事件以在第一个表单中加载每个选定的行值.但是我仍然没有得到正确的输出,实际上它确实可以工作,但是仅当我将第一个表单注册到第二个表单加载事件时才起作用;
find below a sample of my code that i used to pass values of the selected row in the gridview to text boxes on another form ;
there is the scenario : i have 2 forms the first form contains a button and textboxes , the button opens the second forms "form 2 ". The form2 contains the Xtragrid , from this form i want to perform a double click event to load each selected row values in the first form .. I have used " Observer pattern concept"; but i still dont get the correct output ,actually it does work but only when i register the first form onto the second form load event ;
// there is the interface class
namespace InterFaces
{
interface IUserSubject
{
void RegistrObserver(IUserObserver observer);
void RemoveObserver(IUserObserver observer);
void NotifyObservers(Issue outData);
}
public interface IUserObserver
{
void Update(Issue outData);
}
public class Issue
{
public int value1;
public int value2;
public int value3;
}
}
// form 2
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Helpers;
using InterFaces;
namespace GridProject
{
public partial class Form2 : Form, IUserSubject
{
public Form2()
{
InitializeComponent();
}
public void Update(Issue outData)
{
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 form = new Form1();
RegistrObserver(form);
form2.Show();
DataHelper dh = new DataHelper(DSparametr.doubleDS);
gridControl1.DataSource = dh.DataSet;
gridControl1.DataMember = dh.DataMember;
gridView1.OptionsBehavior.Editable = true;
gridView1.ShownEditor += new EventHandler(gridView1_ShownEditor);
}
void gridView1_ShownEditor(object sender, EventArgs e)
{
gridView1.ActiveEditor.DoubleClick += new EventHandler(ActiveEditor_DoubleClick);
}
void ActiveEditor_DoubleClick(object sender, EventArgs e)
{
Issue outData = new Issue()
{
value1 = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0])),
value2 = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1])),
value3 = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2])),
};
NotifyObservers(outData);
}
public void RegistrObserver(IUserObserver observer)
{
if (observer == null)
throw new ArgumentNullException("Observer");
OnNotifyObsirvers += new NotifyObsirversHandler(observer.Update);
}
public void RemoveObserver(IUserObserver observer)
{
if (observer == null)
throw new ArgumentNullException("Observer");
OnNotifyObsirvers -= new NotifyObsirversHandler(observer.Update);
}
public void NotifyObservers(Issue outData)
{
if (OnNotifyObsirvers == null)
throw new ArgumentNullException("outData");
OnNotifyObsirvers(outData);
}
private delegate void NotifyObsirversHandler(Issue outData);
private event NotifyObsirversHandler OnNotifyObsirvers;
}
}
// Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using InterFaces;
namespace GridProject
{
public partial class Form1 : Form, IUserObserver
{
public Form1()
{
InitializeComponent();
}
private void SendToEditors(GridView view)
{
}
public void Update(Issue outData)
{
textBox1.Text = outData.value1.ToString();
textBox2.Text = outData.value2.ToString();
textBox3.Text = outData.value3.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void simpleButton1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
}
推荐答案
做到这一点的方法是使用委托.您可以在两个表单之间连接代表,然后它们可以基于事件相互发送消息.
The way to do this is with delegates. You hook up delegates between the two forms, then they can send each other messages based on events.
这是关于表单协作的常见问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].
Actually, please see all the answers in this discussion.
—SA
这篇关于如何将所选Xtragridview的值传递到另一个表单上的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!