Solved

Gridview dropdown - bind record source in code

Posted on 2011-09-07
9
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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