-
Oct 21st, 2020, 06:51 AM
#1
Thread Starter
Member
Return the Index value of a datatable, by searching the Datatable
Hi everyone,
i'm ok with VB but some things are beyond me, and i don't know how to do the following.
I have a datagridview that displays a short filtered list from my datatable. my datatable is normally around 10,000 rows long.
i want to change one of the values in my "datagridviews cell" and then update that value in my datatable.
i have a function that will allow me to update my datatable row.
Code:
''' <summary>
''' Updates the data within the datatable
''' </summary>
''' <param name="TableName">Name of the table</param>
''' <param name="index">The index where the data will change</param>
''' <param name="value">Values that must match with this datatable</param>
Public Sub UpdateDataInDataTable(TableName As String, index As Integer, value() As Object)
Try
Dim dt = ds.Tables(TableName)
dt.Rows(index).ItemArray = value
Catch ex As Exception
MsgBox(ex.Message)
End Try
as you can see this function needs to know the INDEX of which line in the original datatables collection of data to update.
i can't just use the index of the current row of my datagridview because it might only have say 5 rows and my datatable has 10,000 rows.
if it was possible to say "search" the datatable for the unique identifier in my datatable and then return what index row its on that would be great but i don't know how to do that.
my unique identifier is in a column called "Batch_Number". the unique identifier is typically a number like 0013205489 it has the double zeros at the start so i guess it would be treated like a string.
is anyone able to assist with this please?
-
Oct 21st, 2020, 07:04 AM
#2
Re: Return the Index value of a datatable, by searching the Datatable
The Rows collection of a DataTable has a Find method that will find a DataRow by primary key. The index is irrelevant because you'd only be using that to get the row and that Find method returns the row.
-
Oct 21st, 2020, 07:05 AM
#3
Re: Return the Index value of a datatable, by searching the Datatable
i want to change one of the values in my "datagridviews cell" and then update that value in my datatable.
No, that's not what you want to do. What you want to do is simply update your DGV because it should already be bound to the datatable. Since it's already bound, updating one should update the other. If they are not bound, why aren't they?
-tg
-
Oct 21st, 2020, 07:13 AM
#4
Thread Starter
Member
Re: Return the Index value of a datatable, by searching the Datatable
Originally Posted by techgnome
No, that's not what you want to do. What you want to do is simply update your DGV because it should already be bound to the datatable. Since it's already bound, updating one should update the other. If they are not bound, why aren't they?
-tg
well i dont really know if they are or are not, i dont have enough experiance with VB and coding to give you that answer,
DataGridView1.DataSource = dt
is the code i use when i want to change whats in the DataGridView
initially the program was always going to just show you search results. but i now want to add a new feature this requires updating the data in the datatable, so if i search the same thing again i can see that change applied.
-
Oct 21st, 2020, 07:16 AM
#5
Thread Starter
Member
Re: Return the Index value of a datatable, by searching the Datatable
Originally Posted by jmcilhinney
The Rows collection of a DataTable has a Find method that will find a DataRow by primary key. The index is irrelevant because you'd only be using that to get the row and that Find method returns the row.
i know it has a find method, i have no idea how to use it or implement it however.
-
Oct 21st, 2020, 07:38 AM
#6
Re: Return the Index value of a datatable, by searching the Datatable
Originally Posted by AndyAndromeda
i know it has a find method, i have no idea how to use it or implement it however.
So then read the documentation and search for examples on the web.
-
Oct 21st, 2020, 07:45 AM
#7
Thread Starter
Member
Re: Return the Index value of a datatable, by searching the Datatable
Originally Posted by jmcilhinney
So then read the documentation and search for examples on the web.
i have, and i can't get an example to work for me, or one that's annotated enough to help make enough sense.
the documentation also seems to say that i need at least one column to be set at the primary key, while i have a column with unique value i don't have it set at primary key, and again i also don't know how to implement that. i was hoping it would be as simple as 4 to 6 lines of code to return the value i wanted.
-
Oct 21st, 2020, 07:15 PM
#8
Re: Return the Index value of a datatable, by searching the Datatable
Originally Posted by AndyAndromeda
i have a column with unique value i don't have it set at primary key, and again i also don't know how to implement that.
If only the DataTable had a property dedicated to that and the documentation for that property included a code example. Oh look, it does!
Tags for this Thread
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
|