I have a datagrid that has a dropdown column. All works fine until I go to edit mode and it seems to forget the original value of the column in the selected row. How can I retain that value in edit mode?????
Dropdown column in a datagrid
Collapse
X
-
Please post your code instead of using attachments.
Attachments are not working at this time.
Also, please post the error message that you are getting....stat ing that an error occurs is not going to help us in understanding the problem that you're having.Comment
-
Dropdown column in a datagrid
Code:<asp:GridView ID="GridView1" runat="server" Height="205px" Style="z-index: 102; left: 19px; position: absolute; top: 28px" Width="325px" BorderStyle="None" TabIndex="11" AutoGenerateColumns="False" DataSourceID="PhoneList" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" BackColor="White" BorderColor="#999999" BorderWidth="1px" CellPadding="3" GridLines="Vertical" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="ddlSuper_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ButtonType="Image" DeleteImageUrl="~/App_Themes/Default/Images/Delete16x16.gif" EditImageUrl="~/App_Themes/Default/Images/SelectEdit.gif" InsertText="" NewText="" CancelImageUrl="~/App_Themes/Default/Images/Reject16x16.gif" UpdateImageUrl="~/App_Themes/Default/Images/Approve16x16.gif" /> <asp:TemplateField HeaderText="Seq" ShowHeader="False" Visible="False"> <EditItemTemplate> <asp:TextBox ID="txtSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Type"> <EditItemTemplate> <asp:DropDownList ID="ddlType" runat="server" SelectedValue='<%# Eval("WPPHTP")%>' DataSourceID="PhoneTypes" DataValueField="DRKY" DataTextField="DRDL01"> </asp:DropDownList> <asp:ObjectDataSource ID="PhoneTypes" runat="server" SelectMethod="GetPhoneTypes" TypeName="Intranet.DAL.SelfServiceSql"></asp:ObjectDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblPType" runat="server" Text='<%# Bind("WPPHTP") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Area Cd"> <EditItemTemplate> <asp:TextBox ID="txtACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Nbr"> <EditItemTemplate> <asp:TextBox ID="txtPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="Gainsboro" /> </asp:GridView>
I get the following error:
DataBinding: 'System.Data.Da taRowView' does not contain a property with the name 'WPPHTP'.Last edited by Frinavale; Feb 24 '09, 07:21 PM. Reason: Added [code] tags: Please post code between [code][/code] tagsComment
-
Dropdownlist in datagrid
Hi
You can use this code.
Code:<asp:DataGrid runat=server id=gvfilesdata> <Columns> <asp:TemplateColumn> <ItemTemplate> <asp:DropDownList ID="ddPage" runat=server Width="150px"> <asp:listitem text='1' value='1'></asp:listitem> <asp:listitem text='2' value='2'></asp:listitem> </asp:DropDownList> </itemtemplate> </asp:TemplateColumn> </Columns> </asp:datagrid>
Code of vb
Code:Protected Sub gvFilesData_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFilesData.RowEditing Dim id As Integer pageid = CType(gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage"), DropDownList).SelectedValue CType((gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage")), DropDownList).SelectedValue = pageid bindDataOfFileUpload() End Sub
Please implement this code.Last edited by Frinavale; Feb 25 '09, 03:52 PM. Reason: Added [code] tags: Please post code between [code][/code] tagsComment
-
Take the Eval statement out.
The GridView uses it's data source's properties and methods to bind to.
It's trying to bind the data to DataRowView, which obviously does not have a property named "WPPHTP".
Instead, implement a method that handles the GridView's RowDataBound event and set the selected item in the DropDownList here.Comment
-
Not really....each implementation of the RowDataBound event is going to be different depending on your datasource and what your GridView is meant to display.
Check out the GridView RowDataBound event for more information.Comment
-
Ok, in typing a response I realized where I made the mistake in attempting to help you....sorry.
You shouldn't be looking at the RowDataBound Event.
I think you should be looking at the Editing Event.
It is here that you want to set the selected value of the DropDownList.
Code:Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex Dim thisRowsDdlType As DropDownList = Ctype(GridView1.Rows(e.NewEditIndex).FindControl(ddlType), DropDownList) thisRowsDdlType.Items(thisRowsDdlType.Items(thisRowsDdlType.SelectedIndex)).Selected = False thisRowsDdlType.Items(3).Selected = True End Sub
Comment
-
I'm not great with C#...it would be just as hard for me to change the syntax into C# as it would be for you.
But if you really want an example in C# please check out the links I showed you.
Here goes my attempt (not guaranteed to work...but you'll get the idea):
Code:void GridView1_RowEditing(Object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; DropDownList thisRowsDdlType = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl(ddlType); thisRowsDdlType.Items[thisRowsDdlType.Items[thisRowsDdlType.SelectedIndex]].Selected = false; thisRowsDdlType.Items[3].Selected = true; }
Comment
Comment