I have three TemplateFields in a Gridview. In each, the ItemTemplate has a label whose text value is bound to the GridView's DataSource. But in the EditItemTemplate the DropDownList is populated with a separate DataSource. This is a fairly common setup, so that wasn't too much to deal with.
What I need to do is, when the GridView goes in to Edit mode and the DropDownList is populated and displayed I want the value that was in the bound label to be the default selected value in the DropDownList. As it currently is, the DropDownList renders with "Select..." as the default value regardless of the actual value for that field in the table. And as you probably know, clicking update as it is now would actually update that field with an empty string.
Here's the relevant code for one of the DropDownLists and its DataSource:
<asp:gridview id="gvDataPoint" runat="server" autogeneratecolumns="False" datasourceid="dsDataPoint" allowpaging="True" pagesize="20" allowsorting="True" datakeynames="DataPointID" borderwidth="0px" borderstyle="None" rowstyle-backcolor="whitesmoke" rowstyle-height="27px" alternatingrowstyle-backcolor="#FFFFBB" pagerstyle-horizontalalign="Center" pagerstyle-backcolor="#CCCC99" headerstyle-backcolor="#CCCC99" rowstyle-font-size="10px"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderStyle-Width="125px" /> <asp:TemplateField HeaderText="L0PDUnit" HeaderStyle-Width="100px"> <ItemTemplate> <asp:Label ID="lblL0PDUnitID" runat="server" Text='<%# Bind("L0PDUnit") %>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlEditL0PDUnitID" runat="server" DataSourceID="dsL0PDUnitForDdl" DataTextField="ShortName" DataValueField="L0PDUnitID" SelectedValue='<%# Bind("L0PDUnitID") %>' AppendDataBoundItems="true"> <asp:ListItem Text="Select..." Value="" /> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:gridview> <asp:ObjectDataSource ID="dsL0PDUnitForDdl" runat="server" SelectMethod="GetL0PDUnitforDDL" TypeName="AgCodeNET2.BusinessObject.DataPointSystem" /> <asp:ObjectDataSource ID="dsDataPoint" runat="server" SelectMethod="GetListDataPoint" DeleteMethod="DeleteDataPoint" InsertMethod="UpdateDataPoint" UpdateMethod="UpdateDataPoint" TypeName="AgCodeNET2.BusinessObject.DataPointSystem"> <DeleteParameters> <asp:Parameter Name="DataPointID" Type="int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="DataPointID" Type="Int32" /> <asp:Parameter Name="L0PDUnitID" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="DataPointID" Type="Int32" /> <asp:Parameter Name="L0PDUnitID" Type="Int32" /> </UpdateParameters> </asp:ObjectDataSource>As you can see by the ObjectDataSources, we use a business layer for the data. I have to use a stored proc for any select, update, edit, delete, etc. Any other questions to help clarify, please let me know.
Thanks!
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |
Can't you then just change the binding of the SelectedValue property to the "L0PDUnit" field instead? Outsid of that, I'd mention that it's a good idea to place the ObjectDataSource for your DropDownList within the same template.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Thanks, ecbruck, but unfortunately that won't work. "L0PDUnit" isn't in the SELECT statement of the DropDownList's DataSource. It's a column alias in the SELECT for the GridView's DataSource.
I'm not sure if I need to do this somehow in my stored proc or in the code-behind.
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |
That's the whole point. The data-binding for the SelectedValue property of your DropDwonList should come from a field within your GridView's DataSource. It doens't have to have the same name a field being bound to your DropDownList as long as the underlying value from the GridView.DataSource lies somewhere within the DropDownList value items.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Okay - I do understand what you mean by that last post, but in this case what I want to do is allow the user to change the L0PDUnit to any L0PDUnit from the L0PDUnit table.
Using just the column in my example above, here's the GV's stored proc:
SELECT dp.DataPointID, --PKey column L0.ShortName as L0PDUnit FROM DataPoint dp LEFT JOIN L0PDUnit L0 ON dp.L0PDUnitID = L0.L0PDUnitIDAs you can see by the stored proc, there's a L0PDUnitID column in the DataPoint table that points to the corresponding shortname in the L0PDUnit table. Since I want to allow the user to change the value in the ShortName column in the DataPoint table to any listed in the L0PDUnit table, I used the DDL with it's own DataSource. Here's the SELECT for the DDL:
SELECT L0PDUnitID, --PKey column ShortName FROM L0PDUnit ORDER BY ShortNameMaybe that's more information than you need, or maybe it's not even relevant to what you're trying to say (in which case I'm obviously not understanding - sorry), but I hope it helps.
Thanks
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |
Ok, then your code should work. How about removing the AppendDataBoundItems property along with the hard-coded ListItem. Does it work then? If so, and you still want to add your intial item into the list, then you can inset this ListItem during the DropDownList.DataBound event.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
No dice :(
I removed the AppendDataBoundItems property and the hardcoded "Select...". It took me a while to figure out that I also had to remove the bound "SelectedValue" property from the DDL code as well. So here's what my DDL looks like now:
<asp:TemplateField HeaderText="L0PDUnit"> <ItemTemplate> <asp:Label ID="lblL0PDUnitID" runat="server" Text='<%# Bind("L0PDUnit") %>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlEditL0PDUnit" runat="server" DataSourceID="dsL0PDUnitForDdl" DataTextField="ShortName" DataValueField="L0PDUnitID"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField>Now, when the GV goes to edit mode the default selected value is the first item in the list from the stored proc, of course. But again, presumably since the DDL is using a different DataSource, whatever is selected in the DDL doesn't get into the Update. Plus I still need the DDL to default to whatever value that record has there.
This question is getting more into the realm of SQL-specific stuff, but is there any way to use my select stored proc for the GV to also "hold" the info for the DDL? I'm guessing not and that's not the direction to go to solve this...
Thanks
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |
I think I see what's happening now. In the DataSource feeding your GridView, your returning the "ShortName" field from the "LOPDUnit" which is associated with the "LOPDUnitID" field in the "DataPoint" table. You can't bind to the SelectedValue property of the DropDownList because the value field is bound to the "LOPDUnitID" field of the "LOPDUnit" table. These two obviously don't mesh.
In the query for your GridView, you should simply dump the join and return the "LOPDUnitID" field from the "DataPoint" table. If you do this, no your fields will match up. Then you can simply bind "LOPDUnitID" to the SelectedValue of your DropDownList.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
I think I understand what you mean. However, the ASPX and SQL code I provided above is only pertaining to the fields relevant to the question (at the time). Your suggestion to drop the join kind of sent up a red flag, only because of what else is going on in the GV and stored proc. Here's the full stored proc that populates the GV (the actual GV displays all of these columns and the Update uses all of them as well):
CREATE PROCEDURE dbo.sp_dpa_GetDataPoint AS SELECT dp.DataPointID, dp.DataPointTypeID, dpt.ShortDescription as DataPointType, l0pdu.ShortName as L0PDUnit, l1pdu.ShortName as L1PDUnit, dp.IdentityNumber, dp.AltDescription1, dp.AltDescription2, dp.Latitude, dp.Longitude, dp.ShortName, dp.LongName FROM locn_DataPoint dp LEFT JOIN locn_DataPointType dpt ON dp.DataPointTypeID = dpt.DataPointTypeID LEFT JOIN locn_L0PDUnit l0pdu ON dp.L0PDUnitID = l0pdu.L0PDUnitID LEFT JOIN locn_L1PDUnit l1pdu ON dp.L1PDUnitID = l1pdu.L1PDUnitID WHERE dp.Deleted IS NULL ORDER BY l1pdu.ShortName, dp.IdentityNumber GOSo you see, I dummied down the code a good bit earlier. In light of that, I'll emphasize that the GV needs to display the ShortName (not the L0PDUnitID - and not to be confused with the dp.ShortName field!) that comes from the joined L0PDUnit table. And also, of course, a list of all ShortNames in a DDL from the L0PDUnit table when the GV is in edit mode.
You suggested I "dump the join and return the L0PDUnitID from the DataPoint table". I'm not sure I can do that and still have a bound label in the GV displaying the ShortName gotten from the L0PDUnit table. Either I misunderstand what you're suggesting in your last post, or this added info changes what you would suggest...
I fear I may be confusing things, so please let me know if I need to clarify!
Thanks a lot for your help!
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |
Then you still need to return the "LOPDUnitID" field from the "DataPoint" table or you can't bind to the SelectedValue of your DropDownList.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Well, whaddya know - it was a simple matter of adding dp.L0PDUnitID & dp.L1PDUnitID to the SELECT stored proc! (Which is what I think you just said in your last post!)
Thanks a million for your help, ecbruck!
"f u cn rd ths, u cn gt a gd jb n cmptr prgmmng." - Anon
![]() |
0 |
![]() |