Question programmatically add text to a databound textbox

jamie123

Well-known member
Joined
May 30, 2008
Messages
82
Programming Experience
Beginner
I have a textbox databound to my binding source. It changes its value whenever my combo box changes its value, they're both bound to the same source. On one option on my combo box, i'd like to be able to programmatically change the value of my textbox, this is what I have right now but it does nothing but retrieve the value from its datasource.

If cboDesc.Text = "Sales Tax" Then
txtFee.Text = dblTax

End If

cboDesc is my combo box
"Sales Tax" is the string in which when selected I want to change the txtFee.text's value
txtFee is the text box databound

thanks for the help!
 
haha i know i know, but the only thing is when i put in that code, vs2008 gives me errors, well actualy, it only gives me one error. this is what i put in:

DirectCast(DirectCast(ProceduresBindingSource.current, DataRowView), EbtblsDataSet.ProceduresDataRow).Fee = Form1.dblTax
ProceduresBindingSource.ResetBindings(False)

proceduresbindingsource is my binding source
ebtblsdataset.proceduresdatatable is my data table
fee is the bound column

but it gives me an error in this part of the code:
(DirectCast(ProceduresBindingSource.Current, DataRowView)

and says
Error 2 Value of type 'System.Data.DataRowView' cannot be converted to 'WindowsApplication2.EbtblsDataSet.ProceduresDataTable'

i mean, i know what that error means, but i'm not sure how to fix it, do you know what the problem could be?

thanks for all your help so far by the way, much appreciated
 
Mmm, yea, sorry.. 2 things here:

jamie123 said:
DirectCast(DirectCast(ProceduresBindingSource.current, DataRowView), EbtblsDataSet.ProceduresDataTable).Fee = Form1.dblTax

1) i missed a property, sorry
2) i didnt tell you to try and cast to a datatable, but I dont know what the relevant rowtype will be called, but guessing from the table name it will likely be ProceduresDataRow

DirectCast(DirectCast(ProceduresBindingSource.curr ent, DataRowView).Row, EbtblsDataSet.ProceduresDataTable).Fee
 
Mmm, yea, sorry.. 2 things here:



1) i missed a property, sorry
2) i didnt tell you to try and cast to a datatable, but I dont know what the relevant rowtype will be called, but guessing from the table name it will likely be ProceduresDataRow

DirectCast(DirectCast(ProceduresBindingSource.curr ent, DataRowView).Row, EbtblsDataSet.ProceduresDataTable).Fee




Thank you, i'm sorry i don't really know much about datatables and datarows, i tried both the proceduresdatatable and proceduresdatarow just in case. I ended up getting it to work with your new code, thanks for putting up with my noobish questions:)

There is one problem, however, and I think this is because of the resetbindings(). Along with the dblTax being displayed in the textbox, the combo box also resets its value to the last selected value before "Sales Tax" (Example, I have "Occular" selected , i change it to "Sales Tax", it executes the code then goes back to "Occular") i tried putting

cboDesc.SelectedValue = "Sales Tax" in the If statement I had, didn't work
tried cboDesc.Text as well. setting the text property of cboDesc works, but then the value in txtFee resets back to 0. Anyway on getting it to display the correct value (dblTax) in txtFee, AND keeping the selectedvalue on "Sales Tax" ?
 
Oh man, this is getting too much like hard work. What are you actually trying to accomplish here with this? i.e. what is the actual problem youre trying to solve? WHy do you want to programmatically change this value based on a value that you find in another form? What's your data structure? I get the feeling this can be solved way more easily with an expression, but i'm not sure
 
Oh man, this is getting too much like hard work. What are you actually trying to accomplish here with this? i.e. what is the actual problem youre trying to solve? WHy do you want to programmatically change this value based on a value that you find in another form? What's your data structure? I get the feeling this can be solved way more easily with an expression, but i'm not sure

1. I have two forms, one that displays a datagridview, another that displays a combo box with a few textboxes all databound to the same source (a table called Procedures), the combo box has different types of procedures, and the text boxes contain different types of fees associated with that procedure.

2. I want to programmatically change this one value because unlike the other procedures in the combo box which have fixed values (for example "Patient Checkup" -that has a fixed fee) , "Sales Tax", a row in the procedures table, does not, it depends on how many items in the datagridview are taxable. I calculate the tax everytime the grid loses focus, this works fine

3. My data structure is a strongly typed dataset (ebtblsdataset) which contains a LOT of tables, many of which i don't use ( ididn't design the back end for this). The two tables that I do use are called Financial and Procedures. Procedures, like I said before, is used on the second form with the combo box, it lists simply...all of the procedures that can be performed in the doctor's office. The second table, Financial, is where everything is recorded for the patient. For example, I select "Patient visit" on the combo box form in the combo box, the text box databindings automatically update the associated fees in the text box, i hit submit and it inserts it into the financial table and repaints the datagridview.
 
OK, there's still a bit missing in my mind, but can you take a read of this:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

Read it fully, and you should be able to see how to add a column to a datatable taht will calculate a value based on other columns.. So your expression might be:

IIF([Taxable] = 'Y', [Price] * 1.25, [Price])


You can also SUM() a column if youre doing some kind of financial totting up

This will produce a different price if the row has a Y in the Taxable column...
Does that help you get closer?
 
I understand how that could work, but I feel like its going out of the way, because like I said I already have a fairly simple method of calculating tax using stored procedures that works fine.

Maybe these pictures will help clear some things up, I think you may be thinking too deep into my question:

9gy7t2.jpg

This is my datagridview, it displays all the data entered from the add form.

15q28eh.jpg

This is the add form that allows you to add items to the datagridview, all of the text boxes and combo boxes are databound to the same datasource (table called Procedures) As you can see, the selection in the combo box has an associated fee displayed in txtFee (immediately to the right of the combo box). If I hit the submit button, the form will close and all of the data will be updated into the Financial table and reflected int he datagridview and look like other entries that you can see in the first picture.

qyahzd.jpg

This is sales tax, the problem that I've been having the whole time, with your code using directcast, it will put the correct value in txtFee.text (which is dblTax), but the combo box will revert back to the previous selection it had BEFORE directcast was executed. So if I was using this program and these pictures were a series of my steps, as soon as I selected Sales Tax, dblTax would show up in txtFee (in this case, dblTax=0), and the combo box would revert back to "Office/Outpatient Visit,EST, Detailed". So it would display "Office/Outpatient Visit,EST, Detailed" in the cbo, and 0.00(which is = dblTax) in txtFee. This is obviously wrong, since the cbo needs to display "Sales Tax". If I hit submit, it would record the description as "Office/Outpatient Visit,EST, Detailed" instead of "Sales Tax" into my table which is reflected by the datagridview. All I need to do is have it keep the value "Sales Tax" while it also updates the txtFee to equal dblTax only when "Sales Tax" is selected. Like I said, the directcast code you gave me updates the textbox to dblTax like its supposed to, so now I need just the cbo to stay on "Sales Tax" so it can be recorded and reflected in the datagridview correctly



edit: When I said the submit button, i meant add entry but i'm sure you got the gist either way ;)
 
And you re-sum the entire grid and work out the tax? But only if there is a sales tax added as an invoicable entry? I dont think financial systems work like that. Rather, they work like:

There are a list of known products that might also have some tax amount due on them. Food is 0%, a TV is 17.5% (in my country)

So our products table contains:
Name, Price, Tax
Apple, 100, 0
TV, 50000, 17.5


When we buy things, we specify the quantity. Businesses use this to charge for services e.g.:

Workshop Hour, 4000, 17.5

An hour of workshop enigneer time costs 40 GBP


When buying products, into the Orders table, we put the product ID and the quantity, which gives us the price. 3 apples are 300, 2 TVs are 100000, 2.75 hours of engineer time are 11000

We can then get the grid to do the additions for us. We set the ValueAfterTax column to be an expression:

[Price] * [Quantity] * [TaxRate]/100


If we want to find the total cos tof the order, we create yet another column, OrderTotalValue and set its expression:

SUM([ValueAfterTax])


Now if we have 10 rows in the grid, and suppose the ValueAfterTax for them all are 1,2,3,4,5,6,7,8,9,10
The expression will work out 55, and put it as every row in the column. All have 55.
Now you can bind a label to the OrderTotalValue column and no matter what row youre on, the total value is added up and displayed


Advantageously, when you change ANYTHING about the grid at all, any value etc, the expressions are recalculated.

This is such an unbelievably easy way of adding thigns up, when you star tusing it you'll wonder why you resisted initially.. Its a very useful thing to have the data model calculate like this. Think about MS Excel and how it works.. that's what Expression (in a limited form) gives you. Accountants and other number crunchers love excel, you should be loving this..


Adding a line for sales tax to every order? No! You work it out every time the order is called up and print it.. You do not store this kind of info in the database..
 
And you re-sum..

that's a good idea, but I have the balances displayed in labels above the form that are stored procedures, if I wanted to display the total of the ValueAfterTax column in a label, how would I do so..and by that I mean, can I access individual cells of the datagridview if they are not linked to a datasource fairly easily?
 
Last edited by a moderator:
Please avoid pressing "Quote" if you mean to reply; Quoting a 30 line post from me, when youre the next person in-thread to respond makes the thread a bit hard to read. Quote should only be used to reassign context away from current discussion, to previous discussion or to respond to points individually

If you want to display a total, you make a column set to =SUM([column to add]) and then every value in that column, is the total, so it doesnt matter wha thte current row is, the total looks like it doesnt change:

Name, Price, Total (SUM([price])
Apples, 10, 25
Pears, 5, 25
Oranges 7, 25
Grapes 3, 25

The label is bound to the column, we arent interested in accessing a particular row, becuase all rows ar ethe same value

Adding a line of:

Lemons, 8

Would cause all rows to show a new Total of 33
 
Sorry for the late reply, I've been away, but where do you set the Expression. I tried setting it in the data property name of the datagridview on the new unbound column I created but that doesnt' seem to do anything, forgive me if this is a rather dumb question

In the DataSet designer, on the column of the datatable, in the properties window.. because .Expression is a "property" of a DataColumn ;)
 

Latest posts

Back
Top