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 ;
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;
}
}
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;
}
}
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();
}
}
}