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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Loading data from selected row in datagridview into dialog box

Loading data from selected row in datagridview into dialog box

Scheduled Pinned Locked Moved C#
7 Posts 2 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.
  • M Offline
    M Offline
    Martin31088
    wrote on last edited by
    #1

    Hi everyone, I've got a dialog box with checkboxes, comboboxes and textboxes for filling in. I have it saving perfectly into my access database. On this dialog I have Load button, which opens a new dialog. This has a search textbox, a datagridview and a few buttons. The datagridview displays the contents of the database. I would like to be able to get the information in the selected row of the datagridview and load that into the original dialog. I have tried to figure it out myself, but my C# knowledge is limited. I don't really have any code for loading, but If i write anything i'll get it posted. Any direction in this would be greatly appreciated. Many thanks Martin

    H 1 Reply Last reply
    0
    • M Martin31088

      Hi everyone, I've got a dialog box with checkboxes, comboboxes and textboxes for filling in. I have it saving perfectly into my access database. On this dialog I have Load button, which opens a new dialog. This has a search textbox, a datagridview and a few buttons. The datagridview displays the contents of the database. I would like to be able to get the information in the selected row of the datagridview and load that into the original dialog. I have tried to figure it out myself, but my C# knowledge is limited. I don't really have any code for loading, but If i write anything i'll get it posted. Any direction in this would be greatly appreciated. Many thanks Martin

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      Hi Martin, Take a look at 'DataGridViewRow'. That should get you started. If you cannot get it working, post back with an example of what you have tried, and I will try to help.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      M 1 Reply Last reply
      0
      • H Henry Minute

        Hi Martin, Take a look at 'DataGridViewRow'. That should get you started. If you cannot get it working, post back with an example of what you have tried, and I will try to help.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        M Offline
        M Offline
        Martin31088
        wrote on last edited by
        #3

        Thanks. I've had a play with that previously, couldn't really figure out out to implement it though, I'll just keep going.

        H 1 Reply Last reply
        0
        • M Martin31088

          Thanks. I've had a play with that previously, couldn't really figure out out to implement it though, I'll just keep going.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Hi Martin, The DataGridView has a property called SelectedRows, which is a collection of the rows currently selected. There is also an event called OnRowEnter. Both of these give you access to the DataGridViewRow selected by your user. What is needed is a way to track that row and make it available to your calling form when the dialog closes. So: 1. add a private field to your dialog.

          private  DataGridViewRow selectedRow; 
          

          2. add a public property so that it can be accessed from outside the dialog

          public DataGridViewRow SelectedRow
          {
              get
              {
                  return this.selectedRow;
              }
          }
          

          3. If you elect to use OnRowEnter, for example, you can store the row in the private field. In the event handler:

          this.selectedRow = this.dataGridView1.Rows\[e.rowIndex\];
          

          then every time a new row is entered that row is stored in your field. 4. In your calling form:

          if (datagridDialog.ShowDialog() == DialogResult.OK)
          {
              this.FillData(datagridDialog.SelectedRow);
          }
          
          private void FillData(DataGridViewRow loadedRow)
          {
              this.usernameTextBox.Text = loadedRow.Cells\["UserName"\].Value.ToString();
              etc.
          }
          

          Hope this gives you some idea. It has just occurred to me that your calling forms controls might be databound, if so this won't work, please let me know.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          M 1 Reply Last reply
          0
          • H Henry Minute

            Hi Martin, The DataGridView has a property called SelectedRows, which is a collection of the rows currently selected. There is also an event called OnRowEnter. Both of these give you access to the DataGridViewRow selected by your user. What is needed is a way to track that row and make it available to your calling form when the dialog closes. So: 1. add a private field to your dialog.

            private  DataGridViewRow selectedRow; 
            

            2. add a public property so that it can be accessed from outside the dialog

            public DataGridViewRow SelectedRow
            {
                get
                {
                    return this.selectedRow;
                }
            }
            

            3. If you elect to use OnRowEnter, for example, you can store the row in the private field. In the event handler:

            this.selectedRow = this.dataGridView1.Rows\[e.rowIndex\];
            

            then every time a new row is entered that row is stored in your field. 4. In your calling form:

            if (datagridDialog.ShowDialog() == DialogResult.OK)
            {
                this.FillData(datagridDialog.SelectedRow);
            }
            
            private void FillData(DataGridViewRow loadedRow)
            {
                this.usernameTextBox.Text = loadedRow.Cells\["UserName"\].Value.ToString();
                etc.
            }
            

            Hope this gives you some idea. It has just occurred to me that your calling forms controls might be databound, if so this won't work, please let me know.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            M Offline
            M Offline
            Martin31088
            wrote on last edited by
            #5

            Thanks alot for that. I've found a simpler way to get the cell value. I'm just getting the value from the datagridview with: class1.UserID = dtaSearchID.SelectedRows[0].Cells[0].Value.ToString(); I do that for all the cells in the selected row. The problem I have now is that I'm not asure how to automatically fill those details into the first dialog. I am able to do it when i manually trigger an event, ie. click another button. But I can't figure out how to do it on the click of the load button in my Search from. Any help greatly appreciated. Martin

            H 1 Reply Last reply
            0
            • M Martin31088

              Thanks alot for that. I've found a simpler way to get the cell value. I'm just getting the value from the datagridview with: class1.UserID = dtaSearchID.SelectedRows[0].Cells[0].Value.ToString(); I do that for all the cells in the selected row. The problem I have now is that I'm not asure how to automatically fill those details into the first dialog. I am able to do it when i manually trigger an event, ie. click another button. But I can't figure out how to do it on the click of the load button in my Search from. Any help greatly appreciated. Martin

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              One solution to this is not to handle the button-click event in the search form, but in your first dialogue. This is one reason that I suggested the public DataGridViewRow SelectedRow property in my previous post. In your Search dialogue:

                 public event EventHandler LoadButtonClick
                  {
                      add
                      {
                          this.loadButton.Click += value;
                      }
              
                      remove
                      {
                          this.loadButton.Click -= value;
                      }
                  }
              

              doing it this way keeps to OOP principles. Of course if you want to 'cheat' just change the Modifier property for the button, in the properties window, to public. You will have to declare a reference to the Search dialogue as a field of the first dialogue:

              public class FirstDialog : Form
              {
                  private SearchDialog sd = null;
                  ........
                  ........
                  ........
                  ........
              

              Then in the first dialogue, where you create the search dialogue, something like:

              using (sd = new SearchDialog())
              {
                  sd.LoadButtonClick += MyLoadButtonClickHandler;
              
                  if (sd.ShowDialog() == DialogResult.OK) // or whatever
                  {
                     // do anything required when search closes with OK
                     sd.LoadButtonClick -= MyLoadButtonClickHandler;
                  }
                  else
                  {
                     // do anything required when search closes with Cancel
                     sd.LoadButtonClick -= MyLoadButtonClickHandler;
                  }
              }
              

              The click handler can then do

              private void MyLoadButtonClickHandler(object sender, EventArgs e)
              {
                  this.UserNameTextBox.Text = sd.SelectedRow.Cells\["UserName"\].Value.ToString();
                  etc.
              }
              

              Hope this gives you some ideas.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              M 1 Reply Last reply
              0
              • H Henry Minute

                One solution to this is not to handle the button-click event in the search form, but in your first dialogue. This is one reason that I suggested the public DataGridViewRow SelectedRow property in my previous post. In your Search dialogue:

                   public event EventHandler LoadButtonClick
                    {
                        add
                        {
                            this.loadButton.Click += value;
                        }
                
                        remove
                        {
                            this.loadButton.Click -= value;
                        }
                    }
                

                doing it this way keeps to OOP principles. Of course if you want to 'cheat' just change the Modifier property for the button, in the properties window, to public. You will have to declare a reference to the Search dialogue as a field of the first dialogue:

                public class FirstDialog : Form
                {
                    private SearchDialog sd = null;
                    ........
                    ........
                    ........
                    ........
                

                Then in the first dialogue, where you create the search dialogue, something like:

                using (sd = new SearchDialog())
                {
                    sd.LoadButtonClick += MyLoadButtonClickHandler;
                
                    if (sd.ShowDialog() == DialogResult.OK) // or whatever
                    {
                       // do anything required when search closes with OK
                       sd.LoadButtonClick -= MyLoadButtonClickHandler;
                    }
                    else
                    {
                       // do anything required when search closes with Cancel
                       sd.LoadButtonClick -= MyLoadButtonClickHandler;
                    }
                }
                

                The click handler can then do

                private void MyLoadButtonClickHandler(object sender, EventArgs e)
                {
                    this.UserNameTextBox.Text = sd.SelectedRow.Cells\["UserName"\].Value.ToString();
                    etc.
                }
                

                Hope this gives you some ideas.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                M Offline
                M Offline
                Martin31088
                wrote on last edited by
                #7

                Thanks so much, that works beautifully. Not entirely sure exactly how it works, but I'll work it out hehe. Many thanks Martin

                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