Dropdownlist not displaying selectedItem in GridView
-
I have a Gridview in my form and I use SqlDataSource to fill the Grid. I have an Edit button also in the Gridview to Edit data. The problem is while clicking on Edit, the dropdownlist in the Gridview is not displaying the item selected, but going to the first item of the dropdownlist. How should I make the selected item displayed while click on Edit? Please find below my code:
<asp:GridView ID="grdDTCCRics" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="REDCODE"
OnRowDeleted="grdDTCCRics_RowDeleted"
OnRowUpdated="grdDTCCRics_RowUpdated"
OnRowUpdating="grdDTCCRics_RowUpdating"
OnRowEditing="grdDTCCRics_RowEditing"
OnPageIndexChanged="CmdCancel_Click"
DataSourceID="SqlDataSource1">
<RowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<EmptyDataRowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:BoundField DataField="REDCODE" HeaderText="REDCODE" ReadOnly="True" SortExpression="REDCODE" />
<asp:TemplateField HeaderText="RIC" SortExpression="RIC">
<EditItemTemplate>
<asp:TextBox ID="RICTextBox" runat="server" Text='<%# Eval("RIC") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="RICLabel"
runat="Server" Text='<%# Eval("RIC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server">
<asp:ListItem Text="AM" Value="1"></asp:ListItem>
<asp:ListItem Text="US" Value="2"></asp:ListItem>
<asp:ListItem Text="EU" Value="3"></asp:ListItem>
<asp:ListItem Text="JP" Value="4"></asp:ListItem>
<asp:ListItem Text="AS" Value="5"></asp:ListItem>
<asp:ListItem Text="OF" Value="6"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate> -
I have a Gridview in my form and I use SqlDataSource to fill the Grid. I have an Edit button also in the Gridview to Edit data. The problem is while clicking on Edit, the dropdownlist in the Gridview is not displaying the item selected, but going to the first item of the dropdownlist. How should I make the selected item displayed while click on Edit? Please find below my code:
<asp:GridView ID="grdDTCCRics" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="REDCODE"
OnRowDeleted="grdDTCCRics_RowDeleted"
OnRowUpdated="grdDTCCRics_RowUpdated"
OnRowUpdating="grdDTCCRics_RowUpdating"
OnRowEditing="grdDTCCRics_RowEditing"
OnPageIndexChanged="CmdCancel_Click"
DataSourceID="SqlDataSource1">
<RowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<EmptyDataRowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:BoundField DataField="REDCODE" HeaderText="REDCODE" ReadOnly="True" SortExpression="REDCODE" />
<asp:TemplateField HeaderText="RIC" SortExpression="RIC">
<EditItemTemplate>
<asp:TextBox ID="RICTextBox" runat="server" Text='<%# Eval("RIC") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="RICLabel"
runat="Server" Text='<%# Eval("RIC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server">
<asp:ListItem Text="AM" Value="1"></asp:ListItem>
<asp:ListItem Text="US" Value="2"></asp:ListItem>
<asp:ListItem Text="EU" Value="3"></asp:ListItem>
<asp:ListItem Text="JP" Value="4"></asp:ListItem>
<asp:ListItem Text="AS" Value="5"></asp:ListItem>
<asp:ListItem Text="OF" Value="6"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>Hi, maybe there is a simpler solution, but you could use the rowdatabound event of your gridview and set the index of the dropdown there appropriatly (if in edit mode). Here is an example (which is using c1gridview, just replace it by the microsoft one and check the if conditions, they may be slightly different)
protected void grdRoomTypes\_RowDataBound(object sender, C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs e) { if (e.Row.RowType == C1.Web.UI.Controls.C1GridView.C1GridViewRowType.DataRow) { if ((e.Row.RowState & C1.Web.UI.Controls.C1GridView.C1GridViewRowState.Edit) > 0) { // here find your dropdownlist by id and set the index TimeSchemeEditor editor = (TimeSchemeEditor)e.Row.FindControl("timeSchemeEditor"); editor.TimeScheme = (ServiceDirectoryRoomTypeItem)e.Row.DataItem; } else { // Do nothing here maybe TimeSchemeViewer viewer = (TimeSchemeViewer)e.Row.FindControl("timeSchemeViewer"); viewer.TimeScheme = (ServiceDirectoryRoomTypeItem)e.Row.DataItem; } } }
-
Hi, maybe there is a simpler solution, but you could use the rowdatabound event of your gridview and set the index of the dropdown there appropriatly (if in edit mode). Here is an example (which is using c1gridview, just replace it by the microsoft one and check the if conditions, they may be slightly different)
protected void grdRoomTypes\_RowDataBound(object sender, C1.Web.UI.Controls.C1GridView.C1GridViewRowEventArgs e) { if (e.Row.RowType == C1.Web.UI.Controls.C1GridView.C1GridViewRowType.DataRow) { if ((e.Row.RowState & C1.Web.UI.Controls.C1GridView.C1GridViewRowState.Edit) > 0) { // here find your dropdownlist by id and set the index TimeSchemeEditor editor = (TimeSchemeEditor)e.Row.FindControl("timeSchemeEditor"); editor.TimeScheme = (ServiceDirectoryRoomTypeItem)e.Row.DataItem; } else { // Do nothing here maybe TimeSchemeViewer viewer = (TimeSchemeViewer)e.Row.FindControl("timeSchemeViewer"); viewer.TimeScheme = (ServiceDirectoryRoomTypeItem)e.Row.DataItem; } } }
Thanks for the reply. However that code is not working. Following is my RowDataBound Event:
protected void grdTCCRics_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { //grdDTCCRics.EditIndex = e.Row.RowIndex; //grdDTCCRics.DataBind(); Label lblRegion = (Label)e.Row.FindControl("lblcountry"); if (lblRegion != null) { DropDownList ddlRegion = (DropDownList)e.Row.FindControl("RegionCodeList"); if (ddlRegion != null) { ddlRegion.SelectedItem.Text = lblRegion.Text.Trim(); } } } } }
The Label control lblRegion is coming as null. So how will I get the value? Please help. Thanks meeram395.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
Thanks for the reply. However that code is not working. Following is my RowDataBound Event:
protected void grdTCCRics_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { //grdDTCCRics.EditIndex = e.Row.RowIndex; //grdDTCCRics.DataBind(); Label lblRegion = (Label)e.Row.FindControl("lblcountry"); if (lblRegion != null) { DropDownList ddlRegion = (DropDownList)e.Row.FindControl("RegionCodeList"); if (ddlRegion != null) { ddlRegion.SelectedItem.Text = lblRegion.Text.Trim(); } } } } }
The Label control lblRegion is coming as null. So how will I get the value? Please help. Thanks meeram395.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
I guess in your markup, you have an edititemtemplate and an itemtemplate, is that right? In your rowdatabound event, it depends on the current rowstate, which controls exist and which not. For example, if you have a label in your itemtemplate and a dropdown in your edititemtemplate, then the label will be null if rowstate == edit and the other way around. So first check if RowState == Edit and then find your dropdown. you will not find the controls shown during "view" rowstate (dont read viewstate:P). GL
-
I guess in your markup, you have an edititemtemplate and an itemtemplate, is that right? In your rowdatabound event, it depends on the current rowstate, which controls exist and which not. For example, if you have a label in your itemtemplate and a dropdown in your edititemtemplate, then the label will be null if rowstate == edit and the other way around. So first check if RowState == Edit and then find your dropdown. you will not find the controls shown during "view" rowstate (dont read viewstate:P). GL
Ok. Thanks a lot Michael. I have EditItemTemplate and ItemTemplate. As you said, I checked now only the dropdownlist and I'm getting the object. But how will I fix the selected value. Still it is displaying only the first value.
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server" AppendDataBoundItems="true" >
<asp:ListItem Text="AM" ></asp:ListItem>
<asp:ListItem Text="US" ></asp:ListItem>
<asp:ListItem Text="EU"></asp:ListItem>
<asp:ListItem Text="JP" ></asp:ListItem>
<asp:ListItem Text="AS" ></asp:ListItem>
<asp:ListItem Text="OF" ></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcountry" runat="server" Text = '<%# Eval("REGIONCODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>As per the above, it always show the selected value as 'AM' rather than 'EU' which is the actual value selected. Following is my RowDataBoundEvent.
protected void grdTCCRics_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddlRegion = (DropDownList)e.Row.FindControl("RegionCodeList"); if (ddlRegion != null) { string strregion = ddlRegion.SelectedItem.Text; } } }
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have a Gridview in my form and I use SqlDataSource to fill the Grid. I have an Edit button also in the Gridview to Edit data. The problem is while clicking on Edit, the dropdownlist in the Gridview is not displaying the item selected, but going to the first item of the dropdownlist. How should I make the selected item displayed while click on Edit? Please find below my code:
<asp:GridView ID="grdDTCCRics" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="REDCODE"
OnRowDeleted="grdDTCCRics_RowDeleted"
OnRowUpdated="grdDTCCRics_RowUpdated"
OnRowUpdating="grdDTCCRics_RowUpdating"
OnRowEditing="grdDTCCRics_RowEditing"
OnPageIndexChanged="CmdCancel_Click"
DataSourceID="SqlDataSource1">
<RowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<EmptyDataRowStyle Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:BoundField DataField="REDCODE" HeaderText="REDCODE" ReadOnly="True" SortExpression="REDCODE" />
<asp:TemplateField HeaderText="RIC" SortExpression="RIC">
<EditItemTemplate>
<asp:TextBox ID="RICTextBox" runat="server" Text='<%# Eval("RIC") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="RICLabel"
runat="Server" Text='<%# Eval("RIC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server">
<asp:ListItem Text="AM" Value="1"></asp:ListItem>
<asp:ListItem Text="US" Value="2"></asp:ListItem>
<asp:ListItem Text="EU" Value="3"></asp:ListItem>
<asp:ListItem Text="JP" Value="4"></asp:ListItem>
<asp:ListItem Text="AS" Value="5"></asp:ListItem>
<asp:ListItem Text="OF" Value="6"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>I got the solution. Below is the solution. It will be nice if it helps somebody else. I have added the following along with the dropdownlist:
SelectedValue='<%# Bind("REGIONCODE") %>'
So the code will be:
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server" AppendDataBoundItems="true" SelectedValue='<%# Bind("REGIONCODE") %>' >
<asp:ListItem Text=""></asp:ListItem>
<asp:ListItem Text="AM" ></asp:ListItem>
<asp:ListItem Text="US" ></asp:ListItem>
<asp:ListItem Text="EU"></asp:ListItem>
<asp:ListItem Text="JP" ></asp:ListItem>
<asp:ListItem Text="AS" ></asp:ListItem>
<asp:ListItem Text="OF" ></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcountry" runat="server" Text = '<%# Eval("REGIONCODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>That's it. I have tested so many times. It didn't give any issues. Thanks meeram395.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
Ok. Thanks a lot Michael. I have EditItemTemplate and ItemTemplate. As you said, I checked now only the dropdownlist and I'm getting the object. But how will I fix the selected value. Still it is displaying only the first value.
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server" AppendDataBoundItems="true" >
<asp:ListItem Text="AM" ></asp:ListItem>
<asp:ListItem Text="US" ></asp:ListItem>
<asp:ListItem Text="EU"></asp:ListItem>
<asp:ListItem Text="JP" ></asp:ListItem>
<asp:ListItem Text="AS" ></asp:ListItem>
<asp:ListItem Text="OF" ></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcountry" runat="server" Text = '<%# Eval("REGIONCODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>As per the above, it always show the selected value as 'AM' rather than 'EU' which is the actual value selected. Following is my RowDataBoundEvent.
protected void grdTCCRics_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddlRegion = (DropDownList)e.Row.FindControl("RegionCodeList"); if (ddlRegion != null) { string strregion = ddlRegion.SelectedItem.Text; } } }
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Hi, you have to set the SelectedIndex of your DropDownList in this event (according to the value of your DataItem) Try something like this:
protected void grdTCCRics_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddlRegion = (DropDownList)e.Row.FindControl("RegionCodeList");
if (ddlRegion != null)
{
// You have to replace VALUEFROMDATAITEM by the current region
// value of your DataItem (look in e.Row.DataItem, i dont know it)
ListItem item = ddlRegion.Items.FindByValue("VALUEFROMDATAITEM");
ddlRegion.SelectedIndex = ddlRegion.Items.IndexOf(item);//string strregion = ddlRegion.SelectedItem.Text; // You dont retrieve the value of the dropdown here, you set it } } } }
I hope this helps
-
I got the solution. Below is the solution. It will be nice if it helps somebody else. I have added the following along with the dropdownlist:
SelectedValue='<%# Bind("REGIONCODE") %>'
So the code will be:
<asp:TemplateField HeaderText="REGIONCODE" SortExpression="REGIONCODE">
<EditItemTemplate>
<asp:DropDownList ID="RegionCodeList" runat="Server" AppendDataBoundItems="true" SelectedValue='<%# Bind("REGIONCODE") %>' >
<asp:ListItem Text=""></asp:ListItem>
<asp:ListItem Text="AM" ></asp:ListItem>
<asp:ListItem Text="US" ></asp:ListItem>
<asp:ListItem Text="EU"></asp:ListItem>
<asp:ListItem Text="JP" ></asp:ListItem>
<asp:ListItem Text="AS" ></asp:ListItem>
<asp:ListItem Text="OF" ></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcountry" runat="server" Text = '<%# Eval("REGIONCODE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>That's it. I have tested so many times. It didn't give any issues. Thanks meeram395.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
oh dear god, now THAT is simpler=). Thanks