Solved

Linqdatasource set values according to dropdownlist selection

Posted on 2010-11-21
12
1,365 Views
Last Modified: 2013-11-11
Hi
I have a gridview that has a dropdown column, and regular text fields.
Both the gridview and dropdown are bound to their own separate linqdatasources.
The dropdown is in the first column.  
When a user selects the dropdown, I need to fill in a couple of the regular text fields in the same row.
I would be willing to unbind, change controls, whatever, in order to get this accomplished.

How can I provide such filtering?
Thanks
############# A TRY IN LINQ ROW INSERTING ################

    Protected Sub grdServiceDetails_RowInserting(ByVal sender As Object, ByVal e As TestWeb.GridViewInsertEventArgs) Handles grdServiceDetails.RowInserting
        Dim s As String = e.NewValues(0).ToString
        Dim d As String = e.NewValues(1).ToString
        Dim b As String = e.NewValues(2).ToString
        Dim c As String = e.NewValues(3).ToString

        Dim ddServ As DropDownList = DirectCast(Me.grdServiceDetails.InsertRow.FindControl("ddServiceOffered"), DropDownList)
        Dim tst As TestWeb.ExtendedGridView = Me.grdServiceDetails

        If ddServ Is Nothing Then

            ddServ = DirectCast(Me.grdServiceDetails.Rows(Me.grdServiceDetails.SelectedIndex).FindControl("ddServiceOffered"), DropDownList)
        End If


        Dim iServOffered As Integer = 0
        If Not ddServ Is Nothing Then
            If ddServ.SelectedIndex > -1 Then
                ' Dim gvRow As GridViewRow = DirectCast(ddServ.Parent.Parent, GridViewRow)

                iServOffered = ddServ.SelectedValue

                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                         And a.LUServiceID = iServOffered Select a).FirstOrDefault
                If Not q Is Nothing Then
                    e.Row.Cells(2).Text = q.Describe
                    e.Row.Cells(3).Text = q.Price
                End If
            End If

        End If
    End Sub

############### A TRY IN DROPDOWN LIST #############

    Protected Sub ddServiceOffered_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
       
        '      e.Result.GetType()
        Dim ddServ As DropDownList = DirectCast(sender, DropDownList)
        'Dim value As String = Me.grdServiceDetails.Rows(row.RowIndex).Cells(0).Text
        'Session("value") = value
        Dim tst As TestWeb.ExtendedGridView = Me.grdServiceDetails
        Dim row As GridViewRow = DirectCast(ddServ.NamingContainer, GridViewRow)

        Dim txt As TextBox = DirectCast(tst.InsertRow.FindControl("txtDescribe"), TextBox)
        ' If Not txt Is Nothing Then
        Dim iServOffered As Integer = 0
        If Not ddServ Is Nothing Then
            If ddServ.SelectedIndex > -1 Then
                iServOffered = ddServ.SelectedValue
                'ddServ.DataBind
                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                         And a.LUServiceID = iServOffered Select a).SingleOrDefault
                If Not q Is Nothing Then
                    If tst.InsertRowActive Then
                        tst.EnableViewState = True

                        ' ddServ.DataBind()
                        'If Not txt Is Nothing Then
                        '    txt.Text = q.Describe

                        'End If
                        tst.Rows(-1).Cells(2).Text = q.Describe
                        tst.Rows(0).Cells(3).Text = q.Price
                        'tst.DataBind()
                    End If
                    '  Me.grdServiceDetails.InsertRow.Cells.Item(3).Text = q.Describe
                    'Me.grdServiceDetails.InsertRow.Cells(3).Text = q.Price
                End If
            End If

        End If

        end if
        Dim ddServ As DropDownList = DirectCast(sender, DropDownList)

        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddServ.Parent.Parent, GridViewRow)
        Dim sPrice As String = ddServ.Items("Price").ToString
        Dim sDesc As String = ddServ.Items("Describe").ToString
        Dim ddlParent As DataControlFieldCell = DirectCast(ddServ.Parent, DataControlFieldCell)
        If ddServ.SelectedItem.Value > -1 Then
            Dim i As Integer = ddServ.SelectedValue
            ''I need the other control to change it's source
            Dim dc As New SalonDataClassesDataContext
            Dim u As New SalonUtilities
            Dim sUser As String = u.CheckSessionAndReturnUser
            Dim q = (From a In dc.ServicesLUs Where a.UserID.ToString = sUser _
                     And a.LUServiceID = i Select a)
            If Not q Is Nothing Then

            End If
        End If


    End Sub

################## ASPX ####################

           <cc1:ExtendedGridView ID="grdServiceDetails" 
                runat="server" AllowInserting="True" DataSourceID="LnqServiceDetails" DataKeyNames="ServiceDetailID,LUServiceID" 
              AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" 
              AscendingImageUrl="~/SortAsc.gif" BorderColor="Silver" CellPadding="4" 
              DescendingImageUrl="~/SortDesc.gif" ForeColor="#333333" PageSize="7" 
              ShowResultSummary="True" Width="100%" Font-Size="9pt" 
                  OnRowInserting="grdServiceDetails_RowInserting" >
                <PagerSettings Position="topAndBottom" />
                <Columns>
                    <asp:BoundField Visible="false" ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="ServiceID" >
                        
                        <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow"></ItemStyle>
                    </asp:BoundField>
                  <asp:TemplateField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" HeaderText="Service Type">
                  <ItemTemplate>
                 <%#Eval("ServicesLU.ServiceOffered")%>
    			</ItemTemplate>	         
              <EditItemTemplate>
                  <gdd:ExtendedDropDownList ID="ddServiceOffered" runat="server" DataSourceID="LnqServicesLU"
                     DataTextField="ServiceOffered" DataValueField="LUServiceID" 
                     OnSelectedIndexChanged="ddServiceOffered_SelectedIndexChanged" AutoPostBack="true"
                     SelectedValue='<%# Bind("LUServiceID") %>' >
                      
                  </gdd:ExtendedDropDownList>
              </EditItemTemplate>
              <InsertItemTemplate>
                  <gdd:ExtendedDropDownList ID="ddServiceOffered" runat="server" DataSourceID="LnqServicesLU"
                     DataTextField="ServiceOffered" DataValueField="LUServiceID" 
                     OnSelectedIndexChanged="ddServiceOffered_SelectedIndexChanged" AutoPostBack="true"
                     SelectedValue='<%# Bind("LUServiceID") %>' >
                      
                  </gdd:ExtendedDropDownList>
             </InsertItemTemplate>
              
              <ControlStyle BorderStyle="None" CssClass="gridcontrol" />

                <ItemStyle CssClass="gridrow" Width="20%"></ItemStyle>
                </asp:TemplateField>
                    <asp:BoundField Visible="true" ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="Describe" >
                        
                        <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow"></ItemStyle>
                    </asp:BoundField>

                
                
                    <asp:BoundField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" DataField="Price" HeaderText="Price" 
                        ApplyFormatInEditMode="True" DataFormatString="{0:C}" 
                        >
                    <ControlStyle CssClass="gridcontrol"></ControlStyle>

                    <ItemStyle CssClass="gridrow" Width="10%"></ItemStyle>
                    </asp:BoundField>
                    
                  <asp:TemplateField ControlStyle-CssClass="gridcontrol" 
                        ItemStyle-CssClass="gridrow" HeaderText="Formula">
                  <ItemTemplate>
                 <%#Eval("FormulasLU.FormulaName")%>
    			</ItemTemplate>	         
                      <EditItemTemplate>
                          <gdd:ExtendedDropDownList ID="ddFormula" runat="server" DataSourceID="LnqFormulasLU"
                             DataTextField="FormulaName" DataValueField="FormulaID" 
                             SelectedValue='<%# Bind("FormulaID") %>' >
                              
                          </gdd:ExtendedDropDownList>
                      </EditItemTemplate>
                      <ControlStyle BorderStyle="None" CssClass="gridcontrol" />

                    <ItemStyle CssClass="gridrow" Width="20%"></ItemStyle>
                    </asp:TemplateField>
                    <asp:ButtonField CommandName="FormulaView" Text="View Formula" />
                                     
                    <asp:CommandField HeaderText="Action" ShowDeleteButton="True" 
                        ShowEditButton="True" ShowHeader="True" ShowInsertButton="True">
                        <ControlStyle Font-Size="9pt" />
                    </asp:CommandField>
                </Columns>
                <FooterStyle CssClass="gridheader" />
                <RowStyle BackColor="#f9e6fb"  Font-Size="9pt" Font-Names="Verdana" />
                <EditRowStyle BackColor="#f9e604"  Font-Size="9pt" Font-Names="Verdana" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"  Font-Size="9pt" ForeColor="#333333" />
                <PagerStyle HorizontalAlign="Center" Font-Size="8pt" Font-Names="Verdana" />
                <HeaderStyle BackColor="Purple" Font-Bold="True"  Font-Size="9pt" ForeColor="White" />
                <AlternatingRowStyle BackColor="White"  Font-Size="9pt" Font-Names="Verdana" />                                  
    			
                </cc1:ExtendedGridView>         
              <asp:LinqDataSource ID="LnqServicesLU" runat="server"
                  ContextTypeName="SalonDataClassesDataContext" OrderBy="ServiceOffered" 
                  TableName="ServicesLUs" Where="UserID == @UserID" OnSelected="LnqServicesLU_Selected">
                  <WhereParameters>
                      <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
                  </WhereParameters>
              </asp:LinqDataSource>
                
              <asp:LinqDataSource ID="LnqFormulasLU" runat="server" 
                  ContextTypeName="SalonDataClassesDataContext" OrderBy="FormulaName" 
                  TableName="FormulasLUs" Where="UserID == @UserID">
                  <WhereParameters>
                      <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
                  </WhereParameters>
              </asp:LinqDataSource>
              
              <asp:LinqDataSource ID="LnqServiceDetails" runat="server" 
                  ContextTypeName="SalonDataClassesDataContext" EnableDelete="True" 
                  EnableInsert="True" EnableUpdate="True" TableName="ServiceDetails" 
                  Where="ServiceID == @ServiceID">
                  <WhereParameters>
                      <asp:ControlParameter ControlID="txtServiceID" Name="ServiceID" 
                          PropertyName="Text" Type="Int32" />
                  </WhereParameters>
              </asp:LinqDataSource>

Open in new window

0
Comment
Question by:Sheritlw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
12 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 34190961
Create and OnSelectedIndexChanged even on the dropdown. Handle that event by pulling the data you need and updating that row in the GV to set the controls to the proper/new values.
0
 

Author Comment

by:Sheritlw
ID: 34191345
That's exactly what I did in my posted sample code.  I even tried using the linqdatasource inserting event.
Neither fills in the bound fields according the dropdown selection.

Thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34204532
You can use a control parameter with the LINQDataSource that is used for the textboxes.

Check the code example here

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.whereparameters.aspx
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Sheritlw
ID: 34207459
Hi CodeCruiser,

I attached a screen shot of what I'm trying to do.
I have where parameters on both linqdatasources.
When someone selects an item from the dropdownlist, I need to fill the other cells with default values from the dropdownlists linqdatasource.
The gridview I am using is an extended gridview and the row index is -1, virtually non-existent.
I thought if I could insert the default values into the LinqDataSource from the dropdownlist selectedindexchanged event, then I might be able to figure out a way to get this to work.
If I need to change from the gridview to something else, that is fine.
I appreciate your help.
Thank you
0
 

Author Comment

by:Sheritlw
ID: 34207470
Forgot attachment DropDownList in Grid
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34208062
So you want default values in Description, Price, Qty, and Total columns? Where would the values come from?
0
 

Author Comment

by:Sheritlw
ID: 34209808
The linqdatasource that is bound to the dropdownlist contains the values.

Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34211400
Trouble is that the LINQDataSource does not provide a way of retrieving values from the active record(in my knowledge). When you select an item from the dropdown list, does the page postback? If it does, you may have to write code to retrieve the values for selected item and put into the textboxes.
0
 

Author Comment

by:Sheritlw
ID: 34217866
Hi CodeCruiser,

I don't think I'm going to be able to find a solution for this using the standard grid with linq.
I am changing grids and going to use fpSpreads grid.  It will allow me to do what I need to do.
Thank you for your help.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34217889
Hi sheritlw,
Sorry for not being able to fix this for you but glad that you found a work around.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question