-
Mar 5th, 2020, 03:28 PM
#1
Thread Starter
Member
Getting DataGridView Currentrow.index when filter applied
Hi all,
I'm getting the row index for a datagrid so that when clicked, I can get some data from that row record.
I'm simply using....
Code:
MyDataRow = DataGridView1.CurrentRow.Index
But, this pics up the wrong index when the grid is filtered. Is there a simple way of dealing with this?
Thanks in advance
-
Mar 5th, 2020, 03:34 PM
#2
Re: Getting DataGridView Currentrow.index when filter applied
Certainly the row index will change. You would use the PrimaryKey for that record is your goal is to go back to the database and retrieve that record. Where are you trying to retrieve this additional data from??
-
Mar 5th, 2020, 03:42 PM
#3
Thread Starter
Member
Re: Getting DataGridView Currentrow.index when filter applied
I'm showing another form with some more details from the database.
Calling the row using the 'mydatarow' integer from form1 (main) -
Code:
id = Main.dt.Rows(Main.MyDataRow).Item(11)
Can I find the Primary Key of the row selected in the filtered datagrid? Then I can use this to find the row in the second form.
-
Mar 5th, 2020, 04:18 PM
#4
Re: Getting DataGridView Currentrow.index when filter applied
Use a BindingSource, get the current row via .Position property, filter via .Filter, get current row data by casting .Current to the row type in the DataGridView e.g. CType(customersBindingSource.Current,DataRowView).Row.Field(Of T)("field name goes here")
-
Mar 5th, 2020, 04:28 PM
#5
Re: Getting DataGridView Currentrow.index when filter applied
Originally Posted by NigeH
I'm showing another form with some more details from the database.
Calling the row using the 'mydatarow' integer from form1 (main) -
Code:
id = Main.dt.Rows(Main.MyDataRow).Item(11)
Can I find the Primary Key of the row selected in the filtered datagrid? Then I can use this to find the row in the second form.
I don't know if you can the PrimaryKey or not. You haven't explained where the data comes from or how you load they data into the datagridview. But if it's done right, there is no problem getting the current row from the dgv and passing it to another form. A FULL explanation of the situation is needed. Post all relevant code.
-
Mar 5th, 2020, 08:16 PM
#6
Re: Getting DataGridView Currentrow.index when filter applied
Presumably you are populating a DataTable from a database and then binding that to your grid. This is the sort of information that would be included in a FULL and CLEAR explanation of the problem. In that case, as suggested, you should be using a BindingSource. You bind the DataTable to the BindingSource and the BindingSource to the grid. You then filter by setting the Filter property of the BindingSource, which has the same effect as setting the DefaultView.RowFilter of the DataTable. The point of a BindingSource is to provide a one-stop-shop for all things related to that bound data. The BindingSource itself is a list of the records that satisfy the Filter and in the Sort order.
That means that there are various ways this can be done. I doubt that you actually do want the index of a row at all, because the only use that would generally is to get the row at that index. Why not just get that row in the first place? If you are using a BindingSource then, as suggested, the correct approach is to use its Current property, e.g.
vb.net Code:
Dim rowView As DataRowView = DirectCast(myBindingSource.Current, DataRowView) Dim row As DataRow = rowView.Row
If you weren't using a BindingSource then you could go via the appropriate CurrencyManager but that is more convoluted than necessary and part of the reason to use a BindingSource in the first place. The alternative is to just get it from the grid:
vb.net Code:
Dim rowView As DataRowView = DirectCast(myDataGridView.CurrentRow.DataBoundItem, DataRowView) Dim row As DataRow = rowView.Row
Note that complex data-binding, i.e. setting a DataSource property, in WinForms requires an object that implements the IList or IListSource interface. As the name suggests, an IListSource is the source of an IList. It has a GetList method that returns an IList. The DataTable class implements IListSource and the DataView class implements IList. When you bind a DataTable, its GetList method is called and that returns its DefaultView property, which is a DataView, and that's where the bound data actually comes from. That's why sorting and filtering via the DefaultView affects what you see in a bound DataGridView.
That's also why the items bound to the grid are DataRowView objects rather than DataRow objects. If you just want to access the data then you can just use those DataRowViews directly. They behave similarly to DataRows in many cases but are also a bit different. If you genuinely need a DataRow object than, as my examples show, you can get one from the Row property of a DataRowView. The Rows property of the DataTable is a list of all records in the original order while the DefaultView is a list of only records that match the RowFilter in the order specified by the Sort property.
If you use a BindingSource then it provides similar functionality to the DefaultView as well as the underlying CurrencyManager. It is like the DefaultView in the it provides Filter and Sort properties and is itself a list of DataRowView objects and it is like a CurrencyManager in that it provides navigation through Position, Current, MoveNext, etc. It also provides transactional editing. If you're doing more than just displaying read-only data, use a BindingSource.
-
Mar 6th, 2020, 03:12 AM
#7
Thread Starter
Member
Re: Getting DataGridView Currentrow.index when filter applied
Hi all,
Thanks, yes - I am using a binding source
This works perfectly.
Code:
Dim rowView As DataRowView = DirectCast(myBindingSource.Current, DataRowView)
Dim row As DataRow = rowView.Row
I can then get the items in the row variable from the other form. Although, I did have to declare it at the top of the code as a public variable, but works!
Thanks
-
Mar 6th, 2020, 04:33 AM
#8
Re: Getting DataGridView Currentrow.index when filter applied
Originally Posted by NigeH
I can then get the items in the row variable from the other form.
You can get the field values from the DataRowView just as easily as you can from the DataRow, as I suggested earlier.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|