Assigning a value to a column in a data table

W

Woody Splawn

Lets say I have a winform that is populated with a dataset. The dataset and
data table may have several rows in it. Lets say I am looking at the
winform and I want to assign a value to a certain column in the associated
datatable. Lets say there are 10 rows in the table and I am on row 5, and I
want to assign the value to row 5, but I don't know that I am on row 5.
Anyway, my method for assigning the value to the field would be:
Dim Dt As DataTable
Dt = DsMain1.Tables("Contracts")
Dt.Rows(4)("PriorityCon") = "2"

However, if I did not know I was on Row 5 how would I determine this and put
it in a variable like iRow and then use Dt.Rows(iInt)("PriorityCon") = "2"
 
C

Cor

Hi Woody,

If I understand your question well, that the answer can be "It depends on
the control you are using, mostly it will be using selectedindex for the
iInt, but on a datagrid by instance it can be the currentrowindex."
However, if I did not know I was on Row 5 how would I determine this and put
it in a variable like iRow and then use Dt.Rows(iInt)("PriorityCon") = "2"

Cor
 
W

Woody Splawn

Thank you for resonding.

I think I asked the question illogically. I think, (I am asking your
opinion to confirm) if I want to make a change to the record in the
datatable that corresponds to the record I am on in the Winform or the Grid,
and I do not know it's record number in the datatable, I need to uniquely
identify the record I am on (in the winform or grid) and then in the
datatable do a filter or find for the record I want. Looking at it this
way, the record number in the datatable I guess is irrelavant as long as I
make the change to the right column in the right row, which I will have
after ther filter or find.

As I hear myself say it, it seems obvious but thought you might have
something to add.

It is not always easy for me to keep the distinctions in my mind with regard
to DataSet, the DataTable and the actual back-end table like a table in SQL
Server. If I were going to make a change to the underlying data for a text
field on a form (using code) is there any prefrance as to which tool to
use - i.e. a dataset or a datatable? Is one generally used more often in a
Visual Basic application? Is this question understandable?
 
K

Kevin Yu [MSFT]

Hi Woody,

Based on my understanding, you're using data binding with a datatable and
you need to know the actual position you're on.

I think you can use the CurrencyManager to achieve this. Assuming that
you're binding a datatable to the control, you can use the following code
to get the CurrencyManager:

myCurrencyManager = (CurrencyManager)this.BindingContext[myTable];

Then you can use myCurrencyManager.Position property to get the position.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Woody Splawn" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Assigning a value to a column in a data table
| Date: Mon, 3 Nov 2003 14:49:32 -0800
| Lines: 26
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 168.158-60-66-fuji-dsl.static.surewest.net
66.60.158.168
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:153331
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Thank you for resonding.
|
| I think I asked the question illogically. I think, (I am asking your
| opinion to confirm) if I want to make a change to the record in the
| datatable that corresponds to the record I am on in the Winform or the
Grid,
| and I do not know it's record number in the datatable, I need to uniquely
| identify the record I am on (in the winform or grid) and then in the
| datatable do a filter or find for the record I want. Looking at it this
| way, the record number in the datatable I guess is irrelavant as long as I
| make the change to the right column in the right row, which I will have
| after ther filter or find.
|
| As I hear myself say it, it seems obvious but thought you might have
| something to add.
|
| It is not always easy for me to keep the distinctions in my mind with
regard
| to DataSet, the DataTable and the actual back-end table like a table in
SQL
| Server. If I were going to make a change to the underlying data for a
text
| field on a form (using code) is there any prefrance as to which tool to
| use - i.e. a dataset or a datatable? Is one generally used more often in
a
| Visual Basic application? Is this question understandable?
|
|
|
|
|
|
 
C

Cor

Hi Woody,

There were 2 posibilities to answer the question. Kevin did one, so I only
have to do the other one.

Guess you really don't know what row you need for a dataset.

Keep this always in mind when you are working with a dataset.
Dataset.tables(n).rows(n).("items")

With this you can do everything you want, but sometimes you some times have
to pull things of just the same as with the listview (by instance while
deleting you have to do it strange enough using columns, while with removing
you use the rows, and althoug the words look the same there is a big
difference between removing and deleting).

I give you one example, but I think this is the most direct answer to your
question, but there are 1000 ways.

If you want to know the age from teacher Jensen in a dataset that is
selected from a database with just "select teachernames, age from
teachers"

Then you can do (rough typed)
\\\
dim i as integer
for i = 0 to dataset.tables(0).rows(i)("teachernames")
if dataset.tables(0).rows(i)("teachernames") = "Jensen" then
teachersage = dataset.tables(0).rows(i)("age")
exit for
end if
next
///

It is just an example to make it more clear, you can do it with a find on a
indexed key also, you can select the table, you can use a
dataview............. to find it is a sorted table.

I hope this helps a little bit.

Cor
 
C

Cor

Hi Woody,
I saw it was really rough typed and it still is, but this was a to big error
\\\
dim i as integer
for i = 0 to dataset.tables(0).rows(i)("teachernames")
for i = 0 to dataset.tables(0).rows.count -1
if dataset.tables(0).rows(i)("teachernames") = "Jensen" then
teachersage = dataset.tables(0).rows(i)("age")
exit for
end if
next
///
Cor
 
W

Woody Splawn

Thank you. I appreciate the tip. Putting it in a for loop is simple and
straight forward and since I generally don't retrieve a lot of records into
the dataset at one time the speed should be good to.

When doing these kinds of things are you prone to use a dataset or
datatable. Do you have a preference or does it make much difference?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top