Solved

Gridview dropdown - bind record source in code

Posted on 2011-09-07
9
430 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

25 Experts available now in Live!

Get 1:1 Help Now