I have a grid view with dropdown list. I have to be able to edit it by clicking on ‘Edit’ link button. Dropdown at this point should be populated with data.
1. I need to display one of the values of the dropdown in the grid (read only). This value is what is coming to populate entire row of the grid view. Even I do not have an error on initial load of grid view and while
debugging I see a data coming to populate grid view row and a dropdown, the column is empty.
How can I populate a value of the grid view column of the row as a selected field of the dropdown?
2. I am getting an error when link button ‘Edit’ clicked:
The DataSourceID of '' must be the ID of a control of type IDataSource.A control with ID 'ddlGroupNames' could not be found.
Here is my grid view:
<asp:gridviewrunat="server"
id="grdGoalsView"
width="100%"onpageindexchanged="PageIndexChanging"pagesize="15"selectedindex="0"allowpaging="True"allowsorting="True"datakeynames="GroupID"AutoGenerateDeleteButton="True"AutoGenerateEditButton="True"AutoGenerateSelectButton="True"emptydatatext="No data available."autogeneratecolumns="False"OnSelectedIndexChanged="grdGoalsView_SelectedIndexChanged"OnRowCancelingEdit="grdGoalsView_RowCancelingEdit"OnRowDeleted="grdGoalsView_RowDeleted"OnRowDeleting="grdGoalsView_RowDeleting"OnRowEditing="grdGoalsView_RowEditing"OnRowUpdated="grdGoalsView_RowUpdated"OnRowUpdating="grdGoalsView_RowUpdating"OnSorted="grdGoalsView_Sorted"OnSorting="grdGoalsView_Sorting"OnRowDataBound="grdGoalsView_RowDataBound"OnDataBinding="grdGoalsView_DataBinding"><columns><asp:boundfieldHeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Centerdatafield="GroupID"Visible=
Falseheadertext="Group ID"SortExpression="GroupID"/><asp:TemplateFieldHeaderText="Group Name"HeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Center><EditItemTemplate>asp:DropDownListrunat=server
DataSourceID="ddlGroupNames"
/></EditItemTemplate></asp:TemplateField><asp:boundfieldHeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Centerdatafield="month01"headertext="01"/><asp:boundfieldHeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Centerdatafield="month02"headertext="02"/><asp:boundfieldHeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Centerdatafield="month03"headertext="03"/><asp:boundfieldHeaderStyle-HorizontalAlign=centerItemStyle-HorizontalAlign=Centerdatafield="month04"headertext="04"/></columns></asp:gridview>Here is the code behindprotectedvoid Page_Load(object sender,
EventArgs e){if (!(Page.IsPostBack)){loadControls();}}protectedvoid loadControls(){List<CashFlowGoal> cashFlowGoal = GetGoalData();grdGoalsView.DataSource = (object)cashFlowGoal;grdGoalsView.DataBind();}protectedvoid grdGoalsView_RowEditing(object sender,
GridViewEditEventArgs e){// Set new EditIndexgrdGoalsView.EditIndex = e.NewEditIndex;// Re-bind dataList<CashFlowGoal> CFGoal = GetGoalData();grdGoalsView.DataSource = (object)CFGoal;grdGoalsView.DataBind(); //I am getting the error here}protectedvoid grdGoalsView_DataBinding(object sender,
EventArgs e){DropDownList ddl = (DropDownList)grdGoalsView.FindControl("ddlGroupNames");ddl = newDropDownList();List<CashFlowProgramName> cashFlowGroupNames = GetGroupNames();ddl.DataSource = (object)cashFlowGroupNames;ddl.DataTextField = "CFProgramName";ddl.DataValueField = "CFProgramID";ddl.DataBind();}Can you please tell me what am I missing and what am I doing wrong?
This is reset your reference. So, remove that line.
Hope it helps.
KAMMIE Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
I am not sure why opening tag is missing in my posted question - it is present in the actual code. Using the ID instead of data source Id helped the error. Thank you. I still need help for Item #1. How do I populate
data in dropdown and make the selected item in dropdown to be is what coming back for this item in the row.
KAMMIE Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
Your RowEditing event is fine. Do the thing I told you in my previous post. If you have already created your
RowDataBound event, then copy the code in that event here so we can take a look.
KAMMIE Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
nata100
Member
12 Points
35 Posts
The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 07:14 PM|LINK
I have a grid view with dropdown list. I have to be able to edit it by clicking on ‘Edit’ link button. Dropdown at this point should be populated with data.
1. I need to display one of the values of the dropdown in the grid (read only). This value is what is coming to populate entire row of the grid view. Even I do not have an error on initial load of grid view and while debugging I see a data coming to populate grid view row and a dropdown, the column is empty. How can I populate a value of the grid view column of the row as a selected field of the dropdown?2. I am getting an error when link button ‘Edit’ clicked:
The DataSourceID of '' must be the ID of a control of type IDataSource. A control with ID 'ddlGroupNames' could not be found.Here is my grid view:
<asp:gridview runat="server" id="grdGoalsView" width="100%" onpageindexchanged="PageIndexChanging" pagesize="15" selectedindex="0"allowpaging="True"allowsorting="True"datakeynames="GroupID"AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"AutoGenerateSelectButton="True" emptydatatext="No data available." autogeneratecolumns="False" OnSelectedIndexChanged="grdGoalsView_SelectedIndexChanged" OnRowCancelingEdit="grdGoalsView_RowCancelingEdit" OnRowDeleted="grdGoalsView_RowDeleted" OnRowDeleting="grdGoalsView_RowDeleting" OnRowEditing="grdGoalsView_RowEditing" OnRowUpdated="grdGoalsView_RowUpdated" OnRowUpdating="grdGoalsView_RowUpdating" OnSorted="grdGoalsView_Sorted" OnSorting="grdGoalsView_Sorting" OnRowDataBound="grdGoalsView_RowDataBound" OnDataBinding="grdGoalsView_DataBinding"> <columns> <asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="GroupID" Visible = False headertext="Group ID" SortExpression="GroupID" /><asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center> <EditItemTemplate>asp:DropDownList runat=server DataSourceID="ddlGroupNames" /> </EditItemTemplate></asp:TemplateField> <asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month01" headertext="01"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month02" headertext="02"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month03" headertext="03"/><asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="month04" headertext="04"/></columns></asp:gridview> Here is the code behind protected void Page_Load(object sender, EventArgs e) { if (!(Page.IsPostBack)) { loadControls(); }} protected void loadControls() { List<CashFlowGoal> cashFlowGoal = GetGoalData(); grdGoalsView.DataSource = (object)cashFlowGoal; grdGoalsView.DataBind();} protected void grdGoalsView_RowEditing(object sender, GridViewEditEventArgs e) { // Set new EditIndex grdGoalsView.EditIndex = e.NewEditIndex; // Re-bind data List<CashFlowGoal> CFGoal = GetGoalData(); grdGoalsView.DataSource = (object)CFGoal; grdGoalsView.DataBind(); //I am getting the error here } protected void grdGoalsView_DataBinding(object sender, EventArgs e) { DropDownList ddl = (DropDownList)grdGoalsView.FindControl("ddlGroupNames"); ddl = new DropDownList(); List<CashFlowProgramName> cashFlowGroupNames = GetGroupNames(); ddl.DataSource = (object)cashFlowGroupNames; ddl.DataTextField = "CFProgramName"; ddl.DataValueField = "CFProgramID"; ddl.DataBind(); } Can you please tell me what am I missing and what am I doing wrong?b471code3
Star
13877 Points
2598 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:01 PM|LINK
I see three things which would be causing your error:
You are missing the opening < in this line of code
<EditItemTemplate>asp:DropDownList runat=server DataSourceID="ddlGroupNames" />
Your dropdownlist above does not have an ID so you cannot access it.If you want to set a datasource for the dropdownlist use the DataSource property vs the DataSourceId property.
kammie
Contributor
2163 Points
352 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:01 PM|LINK
It should the
ID="ddlGroupNames".
By setting DataSourceID, you are actually setting the data source to the control with the specified ID. So, use ID here.
It should be....
<asp:DropDownList runat=server ID="ddlGroupNames" />
Also in the DataBinding event, do not use
ddl = new DropDownList();
This is reset your reference. So, remove that line.
Hope it helps.
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
nata100
Member
12 Points
35 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:21 PM|LINK
Thank you for the quick replay.
I am not sure why opening tag is missing in my posted question - it is present in the actual code. Using the ID instead of data source Id helped the error. Thank you. I still need help for Item #1. How do I populate data in dropdown and make the selected item in dropdown to be is what coming back for this item in the row.nata100
Member
12 Points
35 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:23 PM|LINK
Without ddl = new DropDownlist(), ddl is null and I can't use it to assign datasource.
b471code3
Star
13877 Points
2598 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:29 PM|LINK
In order for you to access the DDL nested within your gridivew you can do one of two things:
In your TemplateField add a datasource like this:
<asp:TemplateField HeaderText="Group" SortExpression="GroupID"> <ItemStyle HorizontalAlign="center" /> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>' /> </ItemTemplate> <EditItemTemplate> <asp:SqlDataSource ID="Sqldatasource1" runat="server" SelectCommand="SELECT ID, Name FROM MyTable ORDER BY Name></asp:sqldatasource> <asp:Dropdownlist ID="DropDownList1" runat="server" DataSourceId="Sqldatasource1" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("OtherId") %>' /> </EditItemTemplate> </asp:TemplateField>Since your ddl is nested you will have access to your other bound items from the gridviews datasource.
Or you can access it in the code behind like this:
Dim ddl as DropDownList = CType(GridView1.FindControl("DropDownList1), DropDownList)
ddl.DataSource = your datasource
kammie
Contributor
2163 Points
352 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 08:34 PM|LINK
Use RowDataBound event of the GridView and inside that....write like this...
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit) || e.Row.RowState == DataControlRowState.Edit))
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlGroupNames");
ddl.DataSource = --datasource--;
ddl.DataBind();
}
Hope it helps.
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
nata100
Member
12 Points
35 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 09:29 PM|LINK
I added <ItemTemplate> in addition to <EditItemTemplate> and initial load is showing data. Thanks a lot.
When I click on 'Edit' my dropdown is still empty.
here is the code. There should be something else on RowEditing
<asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label ID="lblGroupName" runat="server" Text='<%# Bind("GroupName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat=server ID="ddlGroupNames" DataTextField="GroupName" />
protected void grdGoalsView_RowEditing(object sender, GridViewEditEventArgs e)
{
// Set new EditIndex
grdGoalsView.EditIndex = e.NewEditIndex;
// Re-bind data
List<CashFlowGoal> CFGoal = GetGoalData();
grdGoalsView.DataSource = (object)CFGoal;
grdGoalsView.DataBind();
}
</EditItemTemplate>
</asp:TemplateField>
kammie
Contributor
2163 Points
352 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 20, 2009 09:37 PM|LINK
Your RowEditing event is fine. Do the thing I told you in my previous post. If you have already created your RowDataBound event, then copy the code in that event here so we can take a look.
Don't forget to Mark as Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
nata100
Member
12 Points
35 Posts
Re: The DataSourceID of '' must be the ID of a control of type IDataSource
Apr 21, 2009 02:59 PM|LINK
here is the code
<
asp:boundfield HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center datafield="GroupID" Visible = False headertext="Group ID" SortExpression="GroupID" /><asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign=center ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label ID="lblGroupName" runat="server" Text='<%# Bind("GroupName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat=server ID="ddlGroupNames" DataTextField="CFProgramName" />
</EditItemTemplate>
</asp:TemplateField>
protected
void grdGoalsView_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)|| e.Row.RowState == DataControlRowState.Edit))
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlGroupNames");
List<CashFlowProgramName> cashFlowGroupNames = GetGroupNames();
ddl.DataSource = (object)cashFlowGroupNames;
ddl.DataTextField = "CFProgramName";
ddl.DataValueField = "CFProgramID";
ddl.DataBind();
}
}