473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dropdown column in a datagrid

47
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?????
Feb 23 '09 #1
22 4250
Frinavale
9,735 Expert Mod 8TB
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.
Feb 24 '09 #2
kimbred
47
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.
Feb 24 '09 #3
Frinavale
9,735 Expert Mod 8TB
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.
Feb 24 '09 #4
kimbred
47
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" Height="205px" Style="z-index: 102; left: 19px;
  2.         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">
  3.         <Columns>
  4.             <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" />
  5.             <asp:TemplateField HeaderText="Seq" ShowHeader="False" Visible="False">
  6.                 <EditItemTemplate>
  7.                     <asp:TextBox ID="txtSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:TextBox>
  8.                 </EditItemTemplate>
  9.                 <ItemTemplate>
  10.                     <asp:Label ID="lblSeq" runat="server" Text='<%# Bind("WPRCK7") %>' Visible="False"></asp:Label>
  11.                 </ItemTemplate>
  12.             </asp:TemplateField>
  13.             <asp:TemplateField HeaderText="Type">
  14.                 <EditItemTemplate>
  15.                     <asp:DropDownList ID="ddlType" runat="server"  SelectedValue='<%# Eval("WPPHTP")%>' DataSourceID="PhoneTypes" DataValueField="DRKY" DataTextField="DRDL01"> </asp:DropDownList> 
  16.                     <asp:ObjectDataSource ID="PhoneTypes" runat="server" SelectMethod="GetPhoneTypes" TypeName="Intranet.DAL.SelfServiceSql"></asp:ObjectDataSource>
  17.                 </EditItemTemplate>
  18.                 <ItemTemplate>
  19.                    <asp:Label ID="lblPType" runat="server" Text='<%# Bind("WPPHTP") %>'></asp:Label>
  20.                 </ItemTemplate>
  21.             </asp:TemplateField>
  22.             <asp:TemplateField HeaderText="Area Cd">
  23.                 <EditItemTemplate>
  24.                     <asp:TextBox ID="txtACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:TextBox>
  25.                 </EditItemTemplate>
  26.                 <ItemTemplate>
  27.                     <asp:Label ID="lblACode" runat="server" Text='<%# Bind("WPAR1") %>'></asp:Label>
  28.                 </ItemTemplate>
  29.             </asp:TemplateField>
  30.             <asp:TemplateField HeaderText="Nbr">
  31.                 <EditItemTemplate>
  32.                     <asp:TextBox ID="txtPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:TextBox>
  33.                 </EditItemTemplate>
  34.                 <ItemTemplate>
  35.                     <asp:Label ID="lblPNbr" runat="server" Text='<%# Bind("WPPH1") %>'></asp:Label>
  36.                 </ItemTemplate>
  37.             </asp:TemplateField>
  38.         </Columns>
  39.         <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
  40.         <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
  41.         <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
  42.         <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
  43.         <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
  44.         <AlternatingRowStyle BackColor="Gainsboro" />
  45.     </asp:GridView>

I get the following error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'WPPHTP'.
Feb 24 '09 #5
Hi
You can use this code.
Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid runat=server id=gvfilesdata>
  2.                         <Columns>
  3.                         <asp:TemplateColumn>
  4.  <ItemTemplate>
  5.                                      <asp:DropDownList ID="ddPage" runat=server Width="150px">
  6. <asp:listitem text='1' value='1'></asp:listitem>
  7. <asp:listitem text='2' value='2'></asp:listitem>
  8. </asp:DropDownList>
  9.  
  10. </itemtemplate>
  11. </asp:TemplateColumn>
  12.  
  13.  
  14.  
  15.                         </Columns>
  16.  
  17. </asp:datagrid>
  18.  
//You can perform this code rowediting of datagrid..



Code of vb
Expand|Select|Wrap|Line Numbers
  1.  Protected Sub gvFilesData_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFilesData.RowEditing
  2.               Dim id As Integer
  3.  
  4.  
  5.         pageid = CType(gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage"), DropDownList).SelectedValue
  6.               CType((gvFilesData.Rows(e.NewEditIndex).Cells(4).FindControl("ddPage")), DropDownList).SelectedValue = pageid
  7.         bindDataOfFileUpload()
  8.     End Sub




Please implement this code.
Feb 25 '09 #6
kimbred
47
The dropdown list must remain dynamic
Feb 25 '09 #7
Frinavale
9,735 Expert Mod 8TB
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.
Feb 25 '09 #8
kimbred
47
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.
Feb 25 '09 #9
Frinavale
9,735 Expert Mod 8TB
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.
Feb 25 '09 #10
kimbred
47
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?
Feb 25 '09 #11
Frinavale
9,735 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
  2.     GridView1.EditIndex = e.NewEditIndex
  3.     Dim thisRowsDdlType As DropDownList = Ctype(GridView1.Rows(e.NewEditIndex).FindControl(ddlType), DropDownList)
  4.     thisRowsDdlType.Items(thisRowsDdlType.Items(thisRowsDdlType.SelectedIndex)).Selected = False
  5.  
  6.     thisRowsDdlType.Items(3).Selected = True
  7.  
  8. End Sub
If this doesn't work, then you need to do something similar, only in the RowDataBound event instead.
Feb 25 '09 #12
kimbred
47
Is there somthing similar in C#?
Feb 25 '09 #13
Frinavale
9,735 Expert Mod 8TB
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):
Expand|Select|Wrap|Line Numbers
  1. void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
  2. {
  3.     GridView1.EditIndex = e.NewEditIndex;
  4.     DropDownList thisRowsDdlType  = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl(ddlType);
  5.     thisRowsDdlType.Items[thisRowsDdlType.Items[thisRowsDdlType.SelectedIndex]].Selected = false;
  6.  
  7.     thisRowsDdlType.Items[3].Selected = true;
  8.  
  9. }
Feb 25 '09 #14
kimbred
47
What is the following line in your code attempting to do?

DropDownList thisRowsDdlType = (DropDownList) GridView1.Rows[e.NewEditIndex].FindControl("ddlType");
Feb 25 '09 #15
Frinavale
9,735 Expert Mod 8TB
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?
Feb 25 '09 #16
kimbred
47
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
Feb 25 '09 #17
kimbred
47
I don't think it's finding the control
Feb 25 '09 #18
kimbred
47
This is the line I'm getting the error on.

thisRowsDdlType.Items[thisRowsDdlType.Items[thisRowsDdlType.SelectedIndex]].Selected = false;
Feb 25 '09 #19
Frinavale
9,735 Expert Mod 8TB
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.
Feb 25 '09 #20
@kimbred
Yes, you can do this way
Expand|Select|Wrap|Line Numbers
  1. <asp:datagrid id='gvfilesdata' runat=server>
  2. <column>
  3. <asp:TemplateColumn>
  4.   <ItemTemplate>
  5.       <asp:DropDownList ID="ddlanguage" runat=server Width="150px"></asp:DropDownList>
  6.   </ItemTemplate>
  7.   </asp:TemplateColumn>
  8. </column>
  9. </asp:datagrid>
Code of vb

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub gvFilesData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gvFilesData.ItemDataBound
  2.         If e.Item.ItemType = ListItemType.Item Then
  3.        Dim query As String
  4.  
  5.             query = "select * from tbl_language"
  6.  
  7. '************* con of connection string name*****
  8. dim adp as new sqldataadpater(query,con)
  9.  
  10. dim ds as new dataset
  11.  
  12.          adp.fill(ds)
  13.             If ds.Tables(0).Rows.Count > 0 Then
  14.                 CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataSource = ds
  15.                 CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataTextField = "lang_name"
  16.                 CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataValueField = "lang_id"
  17.                 CType((e.Row.Cells(0).FindControl("ddLanguage")), DropDownList).DataBind()
  18.             End If
  19.             ds.Dispose()
  20.  
  21.         End If
  22.     End Sub
  23.  
  24.   Protected Sub gvFilesData_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFilesData.RowEditing
  25.         Dim pageid, id As Integer
  26.  
  27.  
  28.         pageid = CType(gvFilesData.Rows(e.NewEditIndex).Cells(0).FindControl("ddlanguage"), DropDownList).SelectedValue
  29.  
  30.         CType((gvFilesData.Rows(e.NewEditIndex).Cells(0).FindControl("ddlanguage")), DropDownList).SelectedValue = pageid
  31.         bindDataOfFileUpload()
  32.     End Sub
Feb 26 '09 #21
kimbred
47
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();
Feb 26 '09 #22
@kimbred


Sorry kimbred,
i am not able to get your question. Can you explain again?
Feb 27 '09 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
2
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...
1
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...
6
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...
1
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"...
1
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...
1
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...
2
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...
0
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...
0
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,...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.