Solved

Gridview dropdown - bind record source in code

Posted on 2011-09-07
9
429 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:edaj6
  • 5
  • 4
9 Comments
 

Author Comment

by:edaj6
ID: 36494013
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

0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 500 total points
ID: 36494213
Bind dropdownlist in the grid's OnRowCreated event.
 OnRowCreated="gvStates_RowCreated".

For more details look at: http://webthingsconsidered.blogspot.com/2005/09/dropdownlist-inside-gridview-or.html

0
 

Author Comment

by:edaj6
ID: 36494310
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?
0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 500 total points
ID: 36494331
Better set DataSource. Refer the link: http://forums.asp.net/t/1311963.aspx/1
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:edaj6
ID: 36494438
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();

...
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36494477
Whether your previous code DropDownList drpBuild = (DropDownList)e.Row.Cells[0].FindControl("ddlStatus"); was working properly? If so, use the same.
0
 

Author Comment

by:edaj6
ID: 36494714
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") %>'>
0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 500 total points
ID: 36494837
No.  SelectedValue can't be set in that way.  Use any one of below code.

ddl.SelectedIndex = Yourindex;  

ddl.SelectedValue = YourValue;
0
 

Author Comment

by:edaj6
ID: 36495329
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

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now