Object Based Datagrid Tempate column binding

Object Based Datagrid Tempate column binding

Post by SGFyb2x » Thu, 29 Apr 2004 23:16:06

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)

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


Object Based Datagrid Tempate column binding

Post by avnra » Fri, 30 Apr 2004 00:23:14

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).