views:

3227

answers:

5

Hi, I am using a Gridview to display some data. In EditItemTemplate of gridview I am using DropDownList for one of the column of gridview. DataSource of gridview is a table "UserEntries". And Datasource of Dropdown is another table "TypeEntries". Columns of TypeEntries are - Guid and TypeName. Guid is DataValueField of dropdown and TypeName is DataTextField. I am storing DataValueFiels of dropdown in UserEntries table.

Now when user clicks Edit button of gridview, how to populate dropdown with "TypeEntries" table? I am using

    Dropdownlist tempddl = new Dropdownlist();
    tempddl = (Dropdownlist)gvUserData.FindControl("ddlTypeListInGrid");
    tempddl.DataSource = _section.GetTypeEntries();
    tempddl.DataBind();

but it is not working. Can anyone tell me any other way to do this task? Thanks in advance.

A: 

C# is case sensitive, you should use DropDownList instead.

Mehrdad Afshari
I used DropDownList now.. Stil it not working. It is giving error "Object reference not set to an instance of an object."
Devashri
A: 

Are you looking for Convert.ChangeType, I would have to see more code to get at your problem.

Mike Geise
A: 

I am uploading whole code of function.

protected void gvUserData_OnRowEditing(object sender, GridViewEditEventArgs  e)
{
    gvUserData.EditIndex = e.NewEditIndex;

    gvUserData.DataSource = _section.GetUserEntries();
    gvUserData.DataBind();

    DropDownList tempddl = new DropDownList();       //I am not sure whether this is correct or not..        
    tempddl = (DropDownList)gvUserData.FindControl("ddlTypeListInGrid");
    tempddl.DataSource = _section.GetTypeEntries();
    tempddl.DataBind();        

}
Devashri
+1  A: 

If you get "Object reference not set to an instance of an object." in exception, it means the "ddlTypeListInGrid" control was not found. So you can not cast NULL ( NOTHING ) to target type.

You propably do this code in wrogn page's life cycle. Try it in one of later oage events (Load, LoadComplete, etc..) or check, if the container realy contains control with ID "ddlTypeListInGrid".

TcKs
Is there any other way to do it rather than converting dropdown control itself into dropdown??
Devashri
The problem is not converting, but the control named "ddlTypeListInGrid" was not found. So you have nothing to convert. Set breakpoint to the line "gvUserData" and look which controls is in it.
TcKs
A: 

As a side note ( not related with your problem, just for your information ) the folowing line :

 DropDownList tempddl = new DropDownList();

Could be :

 DropDownList tempddl;

You dont need to create a new instance of DropDownList since at the next line, your tryng to find an instance nammed "ddlTypeListInGrid". Then, as Tcks said, if the ddlTypeListInGrid control does not exist, then you will likely get a NullReferenceException.

Wyvern