Hello all,
I'm a newbie in the C# world and have figured out a great deal of the information that is pertained in C#. What is eluding me is one thing, in one form, there is a datagridview control with a list of items in it. What I want to do is have the user double-click with a cell in the datagridview to bring up a detail form of what that user selected. I've been trying a number of things, with really no results.
Your help is greatly appreciated.
10 replies to this topic
#1
Posted 08 March 2011 - 06:07 AM
|
|
|
#2
Posted 08 March 2011 - 09:25 AM
Create an event handler for the CellDoubleClick event of the DataGridView. Assuming your DataGridView has its default name still, the code will look like this:
The RowIndex property of the DataGridViewCellEventArgs object will tell you what row the user double clicked on. If this value is -1, it means they double clicked on a row header, so you want to use an if statement to avoid processing those.
void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
// Do something
}
}
The RowIndex property of the DataGridViewCellEventArgs object will tell you what row the user double clicked on. If this value is -1, it means they double clicked on a row header, so you want to use an if statement to avoid processing those.
#3
Posted 08 March 2011 - 09:30 AM
Thanks, but how do you bring up the selected record in the detail form the RowIndex property is pointing to.
Again, thanks in advance.
Again, thanks in advance.
#4
Posted 08 March 2011 - 09:46 AM
That's really going to depend on how you coded the logic of your app. You can use that RowIndex to reference whatever dataset you have bound to that DataGridView. I'd need to see source code in order to be any more helpful than that.
#5
Posted 08 March 2011 - 12:02 PM
That's the funny thing....I don't know what to use as code since I can't find any examples of how to link to two forms together...I can do it in VBA for Access, but this is kind of throwing me for a loop??? Do you have any suggestions on how this can be accomplished.
Thanx
Thanx
#6
Posted 08 March 2011 - 12:20 PM
In .NET, the DataGridView object can be bound to one of many different types of data sets. It can be bound to a List, BindingList, DataSet, DataTable, or you could even implement your own data set type. These offer things such as event listeners for whenever the data in the set gets changed so the DataGridView can update itself to reflect the change, and your user can also edit the values in the data set through the DataGridView control. Whichever data set type you choose to use is up to you.
Suppose for your example you use a BindingList object. You create your class object to represent a record in your data set, and then create a BindingList of type whatever your object is. Then, you bind the DataGridView control to this BindingList with the following code:
Just replace Object with whatever your class is.
Then, add the code I talked about earlier, and when your user double clicks a cell, use the RowIndex in order to reference the correct item in the BindingList. You can read the values of the selected object in the list and do whatever you want with it at that point. You'll need to make a new Form with the required fields, and it should be trivial at this point to populate those fields with the selected data.
Suppose for your example you use a BindingList object. You create your class object to represent a record in your data set, and then create a BindingList of type whatever your object is. Then, you bind the DataGridView control to this BindingList with the following code:
BindingList<Object> bl = new BindingList<Object>(); // Add some records to the binding list here. DataGridView1.DataSource = bl;
Just replace Object with whatever your class is.
Then, add the code I talked about earlier, and when your user double clicks a cell, use the RowIndex in order to reference the correct item in the BindingList. You can read the values of the selected object in the list and do whatever you want with it at that point. You'll need to make a new Form with the required fields, and it should be trivial at this point to populate those fields with the selected data.
#7
Posted 08 March 2011 - 12:22 PM
There's a good tutorial that will walk you through this process here:
C# Tutorial - Binding a DataGridView to a Collection | Switch on the Code
C# Tutorial - Binding a DataGridView to a Collection | Switch on the Code
#8
Posted 08 March 2011 - 01:33 PM
Thanks a huge bunch, this has been a big stumbling block in my programming but this is now soooo much clearer to see.
Again, a big thanks!!!
Again, a big thanks!!!
#9
Posted 08 March 2011 - 01:42 PM
My pleasure. Let me know how your code turns out.
#10
Posted 11 March 2011 - 05:46 AM
I thought that I figured it out, but wrong again, lol. Here's the code that I put in the celldoubleclick event of the datagridview
I don't know what I'm exactly missing on this event, but on the DataRowView, its coming across that its a invalid procedure. Please advise.
Thanks
private void tblDepositDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != 1)
{
DataRowView _rowItemView = (DataRowView)this.tblDepositDataGridView.SelectedRows[0].DataBoundItem;
dbCARSDataSet.tblDepositRow _idRow = (dbCARSDataSet.tblDepositRow)_rowItemView.Row;
int _position = this.dbCARSDataSet.tblDeposit.Rows.IndexOf(_idRow);
new frmDeposit(this.dbCARSDataSet, _position).Show();
}
}
I don't know what I'm exactly missing on this event, but on the DataRowView, its coming across that its a invalid procedure. Please advise.
Thanks
#11
Posted 14 March 2011 - 10:12 AM
First thing I notice is:
That should be -1, not 1. -1 is the row representing the column headers, and so you don't want your logic to do anything on that row. Row 0 is the first actual row of data, row 1 is the 2nd, and so on.
See if that fixes it.
if (e.RowIndex != 1)
That should be -1, not 1. -1 is the row representing the column headers, and so you don't want your logic to do anything on that row. Row 0 is the first actual row of data, row 1 is the 2nd, and so on.
See if that fixes it.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









