dropdownlist in datagrid

Discussion in 'ASP .Net' started by samuelberthelot@googlemail.com, Feb 27, 2006.

  1. Guest

    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 ?
     
    , Feb 27, 2006
    #1
    1. Advertising

  2. gotzonen Guest

    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!
     
    gotzonen, Feb 27, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Feb 27, 2006
    #3
  4. gotzonen Guest

    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.
     
    gotzonen, Feb 27, 2006
    #4
  5. Guest

    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 ?
     
    , Feb 28, 2006
    #5
  6. Guest

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

    bye
     
    , Feb 28, 2006
    #6
  7. Guest

    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 ?
     
    , Feb 28, 2006
    #7
  8. gotzonen Guest

    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.
     
    gotzonen, Feb 28, 2006
    #8
  9. gotzonen Guest

    Sorry,

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

    Bye.
     
    gotzonen, Feb 28, 2006
    #9
  10. Guest

    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 :)
     
    , Feb 28, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,665
    mr2_93
    Oct 2, 2005
  2. tshad
    Replies:
    8
    Views:
    2,260
    tshad
    Oct 19, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,897
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  4. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,336
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  5. keithb
    Replies:
    1
    Views:
    12,166
    shauns1
    Nov 1, 2006
Loading...

Share This Page