Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to pass the values between the datagridview's in different pages

how to pass the values between the datagridview's in different pages

Scheduled Pinned Locked Moved C#
helptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anjani Poornima
    wrote on last edited by
    #1

    Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance

    L D 2 Replies Last reply
    0
    • A Anjani Poornima

      Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just showing example :

      public class Form1 : Form
      {
      ...
      public string MyText
      {
      set { this.textBox1.Text = value; }
      }
      ...
      }

      public class Form2 : Form
      {
      private Form1 form1;

      public Form2(Form1 form1)
      {
      this.form1 = form1;
      }

      private void buttonBack_Click(object sender, EventArgs e)
      {
      this.form1.MyText = "Data from DataGridView here...";
      }
      }

      D A 2 Replies Last reply
      0
      • L Lost User

        Just showing example :

        public class Form1 : Form
        {
        ...
        public string MyText
        {
        set { this.textBox1.Text = value; }
        }
        ...
        }

        public class Form2 : Form
        {
        private Form1 form1;

        public Form2(Form1 form1)
        {
        this.form1 = form1;
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
        this.form1.MyText = "Data from DataGridView here...";
        }
        }

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        This is NOT a good way to do it as now Form2 is coupled to Form1. Also, what if there are hundreds of objects that need the data? Are you going to have hundreds of constructor overloads? The correct way to do this is to raise an event in Form2 that Form1 (and any other interested parties) subscribes to. If any data needs to be passed, then a custom class derived from event args should be used with that data being passed as a property. This is the way all the classes provided by Microsoft do it, with good reason. A quick example:

        using System;
        using System.Windows.Forms;

        public class Form1 : Form
        {
        public Form1() { }

        public void ShowNewForm2()
        {
            Form2 form2 = new Form2();
            form2.CustomEvent += new EventHandler<CustomEventArgs>(form2\_CustomEvent);
            form2.Show();
        }
        
        void form2\_CustomEvent(object sender, CustomEventArgs e)
        {
            // data is in: e.YourData
        }
        

        }

        public class Form2 : Form
        {
        public event EventHandler<CustomEventArgs> CustomEvent;

        public Form2() { }
        
        protected virtual void OnCustomEvent(CustomEventArgs e)
        {
            EventHandler<CustomEventArgs> eh = CustomEvent;
            if (eh != null)
                eh(this, e);
        }
        
        public void PerformCustomEvent()
        {
            OnCustomEvent(new CustomEventArgs(123));
        }
        

        }

        public class CustomEventArgs : EventArgs
        {
        public CustomEventArgs(int yourData)
        {
        YourData = yourData;
        }

        public int YourData
        {
            get;
            private set;
        }
        

        }

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • A Anjani Poornima

          Hello, In my application i am having two forms. In the first form i am having a textbox and in the second form i am having a datagridview and a button back. when the button back is clicked i like to get the data from the datagridview to the textbox in the first form. Please help me how i can do this. I have no sound about this....plz help me Thanks in advance

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          See my reply to stancrm - there is an example there that gives you the idea. :-D

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          A 1 Reply Last reply
          0
          • L Lost User

            Just showing example :

            public class Form1 : Form
            {
            ...
            public string MyText
            {
            set { this.textBox1.Text = value; }
            }
            ...
            }

            public class Form2 : Form
            {
            private Form1 form1;

            public Form2(Form1 form1)
            {
            this.form1 = form1;
            }

            private void buttonBack_Click(object sender, EventArgs e)
            {
            this.form1.MyText = "Data from DataGridView here...";
            }
            }

            A Offline
            A Offline
            Anjani Poornima
            wrote on last edited by
            #5

            thank you for giving me an idea. i got it :)

            1 Reply Last reply
            0
            • D DaveyM69

              See my reply to stancrm - there is an example there that gives you the idea. :-D

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              A Offline
              A Offline
              Anjani Poornima
              wrote on last edited by
              #6

              Thank you for giving me a nice example. :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups