populate textbox from dataview

G

Guest

How can I populate a textbox from a dataview? I have a dataset i'm filtering
on and i want to populate several textboxes based on that filter.

How can i get that to work?

I've tried:

dsDetails = GetCars()
dvDetails = New DataView
With dvDetails
.Table = dsDetails.Tables(0)
.RowFilter = "CARMAKE=" & "'" & strModel& "'"
End With
txtName.DataBindings("NAME").ToString()

and i get this error(s)
Object reference not set to an instance of an object

or I get:

This would cause tow bindings in the collection to bind to the same property
Parameter name: binding

and i'm using this code
txtName.DataBindings.Add(New Binding("Text", dvDetails, "NAME"))

am I missing something or can this not be done?
 
W

W.G. Ryan eMVP

Barney - you need to ensure that you have a DataBinding for the textbox
before you reference it like that. If the bindings are already set - you
don't need to reset them each time. If you get rid of this line
txtName.DataBindings("NAME").ToString() Do you still get the exception?
 
C

Cor Ligthert

Barney,

I am curious, why are you using your code like this
dsDetails = GetCars()
dvDetails = New DataView
With dvDetails
.Table = dsDetails.Tables(0)
.RowFilter = "CARMAKE=" & "'" & strModel& "'"
End With
txtName.DataBindings("NAME").ToString()
You know that with this you type more characters and is used one extra
instruction (not that the last is in my idea important).

And while busy your code can be to show the "name" in the txtName confirm
the currencymanager
\\\
dsDetails = GetCars()
dvDetails as New DataView(dsDetails.Tables(0))
dvDetails.RowFilter = "CARMAKE=" & "'" & strModel& "'"
txtName.DataBindings.Add(New Binding("Text", dvDetails, "NAME"))
///
I hope this helps?

Cor
 
G

Guest

yes i still get the error

W.G. Ryan eMVP said:
Barney - you need to ensure that you have a DataBinding for the textbox
before you reference it like that. If the bindings are already set - you
don't need to reset them each time. If you get rid of this line
txtName.DataBindings("NAME").ToString() Do you still get the exception?
 
G

Guest

I want to filter out the dataset (dataview) and only show the data in the
text box were it equals the users selection.

instead of creating another SQL and another hit to the DB i thought this
would be easier and a better performance.
instead of "select * from table where ID = " & selection "
 
C

Cor Ligthert

Barney,

I did mean why the "with" clause. For the rest I showed how you can do what
you want, some rows lower in my answer.

Cor
 
G

Guest

I follow, just a habit i developed.

even with the example you provided, i still can't pop a text box.
 
C

Cor Ligthert

Barney,

Strange as it looks are this "CARMAKE" and "NAME" you use in a windowform
case sensitive values (not in a webform). And I use nice spaces between the
selections however I don't know if that hurts.

Are you sure the cases are right?

Cor
 
G

Guest

I got it working, sort of. when i first load the form and select a cell in my
datagrid, the textbox is populate correctly, then when i select another cell
i get this error message:

This would cause two bindings in the collection to bind to the same
property. paramter name binding:

how can i get it to work when i select a new selection in my grid?
 
C

Cor Ligthert

Barney,

What code are you using now?
And how do you set the dataview to the datagrid.

Cor
 
G

Guest

here is what i have:

dsDetails = GetCars()
Dim dvDetails As New DataView(dsDetails.Tables(0))
dvDetails.RowFilter = "CARMAKE=" & "'" & strModel & "'"
txtName.DataBindings.Add("Text", dvDetails, "NAME")
Catch ex As Exception
MessageBox.Show("The following error has occurred: " &
ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
 
C

Cor Ligthert

Barney,

You did not show the datagrid as I asked, however when it is like this
dsDetails = GetCars()
Dim dvDetails As New DataView(dsDetails.Tables(0))
datagrid1.datasource = dvDetails
txtName.DataBindings.Add("Text", dvDetails, "NAME")

Than will in the textbox the current selected row be displayed from the
datagrid.
(What is done by the bindingcontext and the currencymanager)

Is that where you are after?

Cor
 
G

Guest

what i have is a function that talks to my db and gets me all the data to
populate the datagrid. That function returns a dataset, so in my form_Load i
have code such as
datagrid1.datasource = GetCars.Table(0) which populates the grid just like i
want. Then i have code in the datagrid1_selectionChanged to populate the
textboxes based on the users selection (the cell they clicked on). The user
can select 1 at a time so when the pick on then them details show up, if they
select another one then detais for that selection need to be displayed and so
on. I can get the first selection to work correctly, its when I choose
another one is when i get the error

I'm able to do this on a asp.net web form, so why will this not work on a
winForm?
 
C

Cor Ligthert

Why don't you than try to change it as I told.

You can do it as well like this.

dsDetails = GetCars()
txtName.DataBindings.Add("Text", dsDetail.Tables(0), "NAME")
datagrid1.datasource = dsDetail.Tables(0)

Dont't compare the webform datagrid with the winform datagrid, that is done
by a lot of winform programmers for a webform datagrid. It are both grids
which has a datasource and with that is in my opinin all said.

Cor
 
G

Guest

I'll give this a shot, but the textboxes are only populated based on the
selection made by the user in the datagrid. The text box is not in the
datagrid, it is on the form below the grid. I'm not sure if your thinking the
textbox is in the grid or not. its not in the grid
 

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