Loading data from selected row in datagridview into dialog box
-
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
-
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
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.”
-
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.”
Thanks. I've had a play with that previously, couldn't really figure out out to implement it though, I'll just keep going.
-
Thanks. I've had a play with that previously, couldn't really figure out out to implement it though, I'll just keep going.
Hi Martin, The
DataGridView
has a property calledSelectedRows
, which is a collection of the rows currently selected. There is also an event calledOnRowEnter
. Both of these give you access to theDataGridViewRow
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.”
-
Hi Martin, The
DataGridView
has a property calledSelectedRows
, which is a collection of the rows currently selected. There is also an event calledOnRowEnter
. Both of these give you access to theDataGridViewRow
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.”
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 -
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. MartinOne 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, topublic
. 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.”
-
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, topublic
. 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.”
Thanks so much, that works beautifully. Not entirely sure exactly how it works, but I'll work it out hehe. Many thanks Martin