Solved

Gridview dropdown - bind record source in code

Posted on 2011-09-07
9
431 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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