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?????
22 4250
Are you preforming a DataBind in the Page Load event....or in any function that is executed before the Event code occurs in the asp life cycle?
If you are, then your data is being re-bound to the GridView and anything edited will be lost.
I think I'm getting close. I've found on the internet a procedure that uses the EVAL in the source. The problem is I get an error msg. Please see the attached code.
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....stating that an error occurs is not going to help us in understanding the problem that you're having.
- <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.DataRowView' does not contain a property with the name 'WPPHTP'.
Hi
You can use this 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>
-
//You can perform this code rowediting of datagrid..
Code of vb -
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.
The dropdown list must remain dynamic
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.
Do you have an example you could provide? I've tried other methods of setting the value of this column in edit mode, but can't get it right. Any help would be appreciated.
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.
The article really didn't give me much in regard to setting the initial value of the column. When I use the code below, I get the Title of the row but nothing more. How do you get the value of the selected column?
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. - 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
If this doesn't work, then you need to do something similar, only in the RowDataBound event instead.
Is there somthing similar in C#?
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): -
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;
-
-
}
What is the following line in your code attempting to do?
DropDownList thisRowsDdlType = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl("ddlType");
Declare a DropDownList named thisRowsDdlType.
Uses the FindControl method on the GridView1 e.NewEditIndex Row to retrieve the DropDownList located in that row...
Since the FindControl method returns an Object, it casts it into a DropDownList.
So, basically it declares a DropDownList and retrieves the DropDownList found in the row being edited.
Is there something wrong with it?
Error 10 Argument '1': cannot convert from 'System.Web.UI.WebControls.ListItem' to 'int' C:\Projects\Intranet\Intranet_Web\SelfService\Cont rols\PersonalInfo.ascx.cs 403 31
I don't think it's finding the control
This is the line I'm getting the error on.
thisRowsDdlType.Items[thisRowsDdlType.Items[thisRowsDdlType.SelectedIndex]].Selected = false;
Just remove that line then.
It's to make sure that any selection in the DropDownList is deselected before selecting the new item. I don't think that your DropDownList will have a selection at that point.
@kimbred
Yes, you can do this way -
<asp:datagrid id='gvfilesdata' runat=server>
-
<column>
-
<asp:TemplateColumn>
-
<ItemTemplate>
-
<asp:DropDownList ID="ddlanguage" runat=server Width="150px"></asp:DropDownList>
-
</ItemTemplate>
-
</asp:TemplateColumn>
-
</column>
-
</asp:datagrid>
Code of vb -
Protected Sub gvFilesData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gvFilesData.ItemDataBound
-
If e.Item.ItemType = ListItemType.Item Then
-
Dim query As String
-
-
query = "select * from tbl_language"
-
-
'************* con of connection string name*****
-
dim adp as new sqldataadpater(query,con)
-
-
dim ds as new dataset
-
-
adp.fill(ds)
-
If ds.Tables(0).Rows.Count > 0 Then
-
CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataSource = ds
-
CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataTextField = "lang_name"
-
CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataValueField = "lang_id"
-
CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataBind()
-
End If
-
ds.Dispose()
-
-
End If
-
End Sub
-
-
Protected Sub gvFilesData_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFilesData.RowEditing
-
Dim pageid, id As Integer
-
-
-
pageid = CType(gvFilesData.Rows(e.NewEditIndex).Cells(0).FindControl("ddlanguage"), DropDownList).SelectedValue
-
-
CType((gvFilesData.Rows(e.NewEditIndex).Cells(0).FindControl("ddlanguage")), DropDownList).SelectedValue = pageid
-
bindDataOfFileUpload()
-
End Sub
If the following line of code gets the correct existing value, how does one go about setting the index of a dropdown thats buried like this is?
string oldvalue = ((Label)row.Cells[2].FindControl("lblType")).Text.Trim();
@kimbred
Sorry kimbred,
i am not able to get your question. Can you explain again?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: HalaszJ |
last post by:
Is there no work around for this?
>-----Original Message-----
>I have asked this question on about every board I can
>think of and I have yet to get an answer, so If anyone
>would know I...
|
by: Michelle |
last post by:
Hello,
I put a dropdown list in ItemTemplate Column. I can select its value from
the dropdown list and store in a dataset, but when I retrieve records from
dataset, it doesn't show up in the...
|
by: Vijay Kerji |
last post by:
Hi,
I have a datagrid with dropdown list and Remove hyperlink in it as
columns.
When I remove a row from the datagrid, Dropdown list selection is
retaining its previous value. i.e, removed...
|
by: Jenna Alten |
last post by:
I have a datagrid with a template column that contains a dropdown list.
I currently fill and display the dropdown list on the page load. This is
working correctly. I am NOT using an Edit Column. I...
|
by: Mike P |
last post by:
I am populating a drop down column in a datagrid on page load. Here is
my code :
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="ddlUserName" Font-Name="Verdana"
Font-Size="8pt"...
|
by: Mike C |
last post by:
I'm new to Web development so I'm doing some bootstrapping and would really
appreciate any help. Been grappling with this for days and am utterly
confused by all the information on the web. I have...
|
by: vishnu |
last post by:
Hi,
I have a fully editable datagrid which has dropdownlist in two
columns. Now i have to bind the dropdown in the second column based on
the value selected in the dropdown in the firstcolumn. so...
|
by: msrahul |
last post by:
HI ALL
Plz Help Me.
I have a datagrid with a few colums. One column is conaining 1's or 0's.
Its like a flag column. It either has 0 or 1. Now what i need is that if any row has 1 in this column...
|
by: Lloyd Sheen |
last post by:
I have been trying for last couple of hours, googling etc to find how to use
a simple one column datagridview where the one and only column is a dropdown
combobox.
I have not been successful , I...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |