Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 ;


C#
// 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; 
} 
 
} 



C#
 // 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; 
 
 
} 
} 


C#
// 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();
  }
  }
  }
Posted
Updated 28-Jul-12 8:27am
v2

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.
 
Share this answer
 
Comments
Lombela 28-Jul-12 20:43pm    
Provide a sample if u dont mind , because i used this approach as well
Christian Graus 29-Jul-12 0:11am    
https://www.google.com.au/search?sugexp=chrome,mod=3&sourceid=chrome&ie=UTF-8&q=using+delegates+C%23 - any of the hits there will show you how to use delegates.
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900