Link to home
Start Free TrialLog in
Avatar of edaj6
edaj6Flag for Denmark

asked on

Gridview dropdown - bind record source in code

I am trying to bind the recordsource of a dropdownlist to a datasource in code. The datasource is dependend on the value in a cell.
With this code I get the error: Databinding can only be used in the context of a databound control.

 
<asp:TemplateField HeaderText="Action">    
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlAction" runat="server" SelectedValue='<%# Bind("Action") %>' DataSourceID="objActionTypes"></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>  
                <asp:CommandField ShowEditButton="True" ButtonType="Button"/>


 protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strStatus = DataBinder.Eval(e.Row.DataItem, "Status").ToString();

                if (gvReportLines.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList drpBuild = (DropDownList)e.Row.Cells[0].FindControl("ddlStatus");

                    if (strStatus == "Standby")
                        drpBuild.DataSourceID = "objStatusTypesAll";
                    else
                        drpBuild.DataSourceID = "objStatusTypes";
                }
            }
        }

Open in new window

Avatar of edaj6
edaj6
Flag of Denmark image

ASKER

Sorry, I posted the wrong template field. Here is the right one.

<asp:TemplateField HeaderText="Status">    
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlStatus" runat="server" DataValueField="StatusType" AppendDataBoundItems="true" 
                        EnableViewState="True"  SelectedValue='<%# Bind("Status") %>' DataSourceID="objStatusTypes"></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                    </ItemTemplate>
                    <AlternatingItemTemplate></AlternatingItemTemplate> 
                    </asp:TemplateField>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edaj6

ASKER

I get an error on line:
drpBuild.DataSourceID = "objStatusTypesAll";

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]

The example in link is not setting the datasource to a DataSourceID, is this not possible?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edaj6

ASKER

Ok thanks. The ddlStatus control can't be found, is this because it's in an edit template?

if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus");
                    ddl.DataSource = BLL.EquipErrorManager.GetStatusTypesAll();
                    ddl.DataBind();

...
Whether your previous code DropDownList drpBuild = (DropDownList)e.Row.Cells[0].FindControl("ddlStatus"); was working properly? If so, use the same.
Avatar of edaj6

ASKER

No not working, just tried to but on item template instead and then it can be found. It can't find a control in an edit template.

When I set Datasource in code is it fine to set selected value in  grid? SelectedValue='<%# Bind("Status") %>'>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edaj6

ASKER

Thankyou for comments, I got a bit futher.

I got the code to work in row_databound, cant do it on row_created since it cant find the control in edit template. The drop down works, but when I click update it can't find the value for status.

 
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == gvReportLines.EditIndex)
            {
                string strStatus = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlStatus");
                
                if (strStatus == "Standby")
                    ddl.DataSource = BLL.EquipErrorManager.GetStatusTypesAll();
                else
                    ddl.DataSource = BLL.EquipErrorManager.GetStatusTypes();

                ddl.DataBind();
                ddl.SelectedValue = strStatus;
           }

Open in new window