Reputation: 21188
I am trying to do this as asked earlier. The only difference that I found is additional List item that was included in above code.
I tried to use AppendDataBoundItems=true
but it is still not working. I also want to set the its default value to the value that was being displayed in label of itemtemplate i.e. DropDownList's SelectedValue='<%# Eval("DepartmentName") %>'
but thie property is not available to me in dropdownlist.
What could be the reason. ??
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server"
DataSourceID="dsDepartment_Edit" DataTextField="DepartmentName"
DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:SqlDataSource ID="dsDepartment_Edit" runat="server"
ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown"
SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>' >
</asp:Label>
</ItemTemplate>
I am using GridView
Upvotes: 10
Views: 69134
Reputation: 1
In Visual Studio 2022, I wasn't able to use a asp:HiddenField, but using a <asp:BoundField DataField="Count">
that was hidden - worked out well. One problem after that was that from the RowDataBound handler, e.Row.FindControl("Count") returned null for the hidden <asp:BoundField DataField="Count">.
What I really wanted to do from the "GridViewTest_RowDataBound" handler was to get at the original one row of SQL DATA that was related to this one Grid row. With access to that original data, I could do a FindControl for the DropDownList and then set DropDownList.SelectedIndex = iCount.
I found one obscure reference for how to do that. From your GridViewTest_RowDataBound handler, add the following code:
int iCount = (int) DataBinder.Eval(e.Row.DataItem, "Count");
DropDownList DropDownListCount = (e.Row.FindControl("DropDownListCount") as DropDownList);
DropDownListCount.SelectedIndex = iCount;
In the example above, "Count" is the name of the desired data column in the SQL rowset returned. That code works even after the <asp:BoundField DataField="Count"> is removed.
See https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa479342(v=msdn.10)?redirectedfrom=MSDN for a Microsoft example using DataBinder.
Upvotes: 0
Reputation: 4838
Why are you guys suggesting to use loops, when there is a GridView
method specifically made for when a row's condition changes - the RowDataBound()
?
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvRow = (GridViewRow)e.Row;
HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID");
if (hfAgentID != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent");
ddlAgent.SelectedValue = hfAgentID.Value;
}
}
}
Upvotes: 1
Reputation: 149
The use of the GridView_DataBound
event handler solves the problem.
In your case you will need to add a HiddenField
to store the PK_DepartmentId
value:
<asp:GridView ID="gvExample" runat="server" AutoGenerateColumns="False" OnDataBound="gvExample_DataBound">
<Columns>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment_Edit" runat="server" DataSourceID="dsDepartment_Edit"
DataTextField="DepartmentName" DataValueField="PK_DepartmentId">
</asp:DropDownList>
<asp:HiddenField ID="hfDepartmentId" runat="server" Value='<%# Bind("PK_DepartmentId") %>' />
<asp:SqlDataSource ID="dsDepartment_Edit" runat="server" ConnectionString="<%$ ConnectionStrings:BlackHillsConnect %>"
ProviderName="System.Data.SqlClient" SelectCommand="sp_GetDepartmentDropDown" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDepartmentName" runat="server" Text='<%# Eval("DepartmentName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
protected void gvExample_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in gvExample.Rows)
{
DropDownList ddlDepartment = gvRow.FindControl("ddlDepartment_Edit") as DropDownList;
HiddenField hfDepartmentId = gvRow.FindControl("hfDepartmentId") as HiddenField;
if (ddlDepartment != null && hfDepartmentId != null)
{
ddlDepartment.SelectedValue = hfDepartmentId.Value;
}
}
}
Upvotes: 4
Reputation:
On your grid there is an event called ItemCommand
. Create a method for it:
protected void Grid1_ItemCommand(object source, GridCommandEventArgs e)
Now simply create a case statement that will recognize when the user has clicked the edit button on the grid:
case Grid.EditCommandName:
//set a member variable to the string of the cell you are editing.
//something like: mString = e.item..["Column"].toString();
break;
Now you have a member variable set to the string you want to be selected before the dropdown is even loaded/prerendered. Use the event OnPrerender
or OnLoad
for the dropdownbox
and set the selected item to this String.
Upvotes: 0
Reputation: 49165
DataValueField
seems to be wrong - shouldn't it be DepartmentId
? Similarly, you need to have SelectedValue='<%# Eval("**DepartmentId**") %>'
- DepartmentName
would be the SeletectText
.
Upvotes: 10
Reputation: 801
This is the best i have found....
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList;
HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField;
if (rbl != null && hf != null)
{
if (hf.Value != "")
{
//clear the default selection if there is one
rbl.ClearSelection();
}
rbl.SelectedValue = hf.Value;
}
}
}
Upvotes: 0