edaj6
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.
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";
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.FindCo ntrol("ddl Status");
ddl.DataSource = BLL.EquipErrorManager.GetS tatusTypes All();
ddl.DataBind();
...
if (e.Row.RowType == DataControlRowType.DataRow
{
DropDownList ddl = (DropDownList)e.Row.FindCo
ddl.DataSource = BLL.EquipErrorManager.GetS
ddl.DataBind();
...
Whether your previous code DropDownList drpBuild = (DropDownList)e.Row.Cells[ 0].FindCon trol("ddlS tatus"); was working properly? If so, use the same.
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") %>'>
When I set Datasource in code is it fine to set selected value in grid? SelectedValue='<%# Bind("Status") %>'>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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;
}
ASKER
Open in new window