Object Based Datagrid Tempate column binding

Discussion in 'ASP .Net' started by =?Utf-8?B?SGFyb2xk?=, Apr 28, 2004.

  1. Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something

    Here are 2 methods. One is the DataGrid's ItemDataBound event to catch the grid population event and the other is to populate the dropdownlist

    I'll start with the method that binds the dropdownlist. As mentioned it is object based and also works fine everywhere but in a template column. I have put in some comments to illustrate what I am doing, please forgive if this is old hat to you

    public static DropDownList RetrieveSalesPartners(ref DropDownList ddl

    Employee searchCriteria = new Employee(); // instance the single objec
    searchCriteria.DepartmentID = 1; // set department criteri
    Employees employees = new Employees(); instance the many objec
    employees.Retrieve(searchCriteria) // call the retieve methoth in the dataclass with criteria

    foreach(Employee employee in employees

    // loop through adding the employee
    ListItem listItem = new ListItem(employee.LastName + ", " + employee.FirstName, employee.EmployeeID)
    ddl.Items.Add(listItem)

    return dd


    the above works great and is commonplace for how we do things. The next part is my issu
    I wire up the ItemDataBound event for the DataGrid control, lets call it dg

    private void dg_ItemDateBound(object sender, DataGridItemEventArgs e

    DropDownList tmpSalesPartner = (DropDownList) e.Item.FindControl("ddlSalesPartner")
    string test = tmpSalesPartner.SelectedValue.ToString(); // this retrieves the test value I put on the control and work
    RetrieveSalesPartners(ref tmpSalesPartner); // bombs on not an instance of the objec


    Thats it in a nutshell. I can retrieve the value of the box but cant populate it dynamically. I have seen examples using datasets, views, ect but all my work is object based so I wont be using any of those. Again the RetrieveSalesPartners works perfectly on contrals not embedded in a grid. It actually is overloaded several times for reuse for other applications, different departments, ect but for brevity I dumbed it down to illustrate the point

    If anyone can offer a suggestion I would be most appreciative. i'll keep plugging away at it but I know I must be ignorant of the datagrid control itself. Also this DropDownList is in the Header not items

    Thanks again

    Harold
    =?Utf-8?B?SGFyb2xk?=, Apr 28, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?SGFyb2xk?=

    avnrao Guest

    this error will come up when e.Item.FindControl could not find your control.
    since dg_ItemDataBound doesnt have any filter like (e.Item.ItemType ==
    ListItemType.Item or EditItem), its going to run the code for every call.
    i would say double check to find out if you are actually getting your
    control (do a response.write when the control is null).

    Av.

    "Harold" <> wrote in message
    news:...
    > Hi all, having a little difficulty binding up a dropdownlist control in a
    > datagrid column. I know I must be missing something.
    >
    >
    > Here are 2 methods. One is the DataGrid's ItemDataBound event to catch the
    > grid population event and the other is to populate the dropdownlist.
    >
    > I'll start with the method that binds the dropdownlist. As mentioned it is
    > object based and also works fine everywhere but in a template column. I
    > have put in some comments to illustrate what I am doing, please forgive if
    > this is old hat to you.
    >
    > public static DropDownList RetrieveSalesPartners(ref DropDownList ddl)
    > {
    > Employee searchCriteria = new Employee(); // instance the single
    > object
    > searchCriteria.DepartmentID = 1; // set department criteria
    > Employees employees = new Employees(); instance the many object
    > employees.Retrieve(searchCriteria) // call the retieve methoth in
    > the dataclass with criterian
    >
    > foreach(Employee employee in employees)
    > {
    > // loop through adding the employees
    > ListItem listItem = new ListItem(employee.LastName + ", " +
    > employee.FirstName, employee.EmployeeID);
    > ddl.Items.Add(listItem);
    > }
    > return ddl
    > }
    >
    > the above works great and is commonplace for how we do things. The next
    > part is my issue
    > I wire up the ItemDataBound event for the DataGrid control, lets call it
    > dg.
    >
    > private void dg_ItemDateBound(object sender, DataGridItemEventArgs e)
    > {
    > DropDownList tmpSalesPartner = (DropDownList)
    > e.Item.FindControl("ddlSalesPartner");
    > string test = tmpSalesPartner.SelectedValue.ToString(); // this
    > retrieves the test value I put on the control and works
    > RetrieveSalesPartners(ref tmpSalesPartner); // bombs on not an
    > instance of the object
    > }
    >
    > Thats it in a nutshell. I can retrieve the value of the box but cant
    > populate it dynamically. I have seen examples using datasets, views, ect
    > but all my work is object based so I wont be using any of those. Again the
    > RetrieveSalesPartners works perfectly on contrals not embedded in a grid.
    > It actually is overloaded several times for reuse for other applications,
    > different departments, ect but for brevity I dumbed it down to illustrate
    > the point.
    >
    > If anyone can offer a suggestion I would be most appreciative. i'll keep
    > plugging away at it but I know I must be ignorant of the datagrid control
    > itself. Also this DropDownList is in the Header not items.
    >
    > Thanks again,
    >
    > Harold
    avnrao, Apr 28, 2004
    #2
    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. Jim Hammond

    datagrid template column binding mystery

    Jim Hammond, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    543
    Jim Hammond
    Nov 24, 2003
  2. vishnu
    Replies:
    1
    Views:
    1,191
    Patrick.O.Ige
    Mar 25, 2006
  3. sam
    Replies:
    0
    Views:
    729
  4. Larry R
    Replies:
    1
    Views:
    377
    marss
    Jun 8, 2007
  5. quiggle

    Binding Two DataTable Columns to One DataGrid Column

    quiggle, Apr 2, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    165
    quiggle
    Apr 2, 2004
Loading...

Share This Page