dropdownlist in datagrid

S

samuelberthelot

Hi,
I'm having a bad time trying to bind a dpl in a datagrid.
Here are my SQL tables:
[Person] {PersonId, PersonName, PersonGroupId}
[PersonGroup] {PersonGroupId, PersonGroupName}

The first column in my grid shows the PersonGroupName's
The second column should be a dpl which source is [Person] for the
PersonGroup of the same row. The value field in the dpl should be
PersonId and the display field should be PersonName.

I already added the columns via templatecolumns. Now I don't know how
to do the databinding :(

Can you help ?
 
G

gotzonen

Hi,
First if all do the binding of the datagrid:

datagrid1.datasource=dataset
datagrid1.databind

Next thing to do is to find out the dpl in each of the rows in the
datagrid.
dim dgi as datagriditem
for each dgi in datagrid1.items
In this line you have to do the binding of the dpl control
CType(dgi.FindControl("<name of the dpl control>"),
Dropdownlist).datasource=dataset
CType(dgi.FindControl("<name of the dpl control>"),
Dropdownlist).databind
next

I hope this could be useful for you!
 
S

samuelberthelot

it is useful indeed.
on which event do you bind the dpl's ?

And also, how can i get the persongroupid (this is the DataKeyNames of
the grid) of the item i'm on in the loop ? because the dpl has to be
filled according to this persongroupid.

Thank you
 
G

gotzonen

You can bind the dpl's after binding the datagrid on page_init or
page_load method.

To get the id try to take it from the dataset which binds the datagrid:

datasetGrid.tables(0).rows(dgi.itemindex).item("persongroupid")

Here you have the persongroupid from the row of the datagrid where you
are.

bye.
 
S

samuelberthelot

Thank you.
I've done what you said and it almost works. The problem is that the
dropdownlist is not showing items properly, but instead in shows
System.Data.DataRowVIew :(
why is that ?
 
S

samuelberthelot

arf.. found out, I had to specify value and display members.
It works now;
thank you so much for your help !

bye
 
S

samuelberthelot

me again...
I've found a problem using this technique, when I want to remove rows
from the grid.

Here's my code:

'set the source of the grid and bind it
GridView2.DataSource = ds.Tables(1)
GridView2.DataBind()


For Each dgi As DataGridItem In GridView2.Items
'get the datasource for the current dropdownlist
Dim dt As DataTable =
BindQueries(ds.Tables(1).Rows(dgi.ItemIndex).Item("QueryGroupId"))

If dt.Rows.Count = 0 Then
'no records found so set rowstate to delete
ds.Tables(1).Rows(dgi.ItemIndex).Delete()
'otherwise bind the dropdownlist
Else
CType(dgi.FindControl("GroupQueries"),
DropDownList).DataSource = dt

CType(dgi.FindControl("GroupQueries"),
DropDownList).DataValueField = "QueryId"
CType(dgi.FindControl("GroupQueries"),
DropDownList).DataTextField = "QueryName"
End If
Next
'call accept changes
ds.Tables(1).AcceptChanges

And here instead of having only my remaining rows properly bound, I
still have all the rows. Those which were deleted are unbound (no
data).
Why ?
 
G

gotzonen

Hi,

I think you have to do the binding for the drop's and for the grid
again after Next statement, instead of calling acceptchanges.
And for deleting the row in the dataset use remove instruction instead
of delete one.

Bye.
 
G

gotzonen

Sorry,

Don´t do the last thing, the one to change the delete method, that's
OK.

Bye.
 
S

samuelberthelot

Hey,
Thanks, but I've changed the way I do it so that I don't even have to
remove rows in this loop, the datatable contains correct record
already. Makes it simpler :)
 

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

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top