Solved

Linqdatasource set values according to dropdownlist selection

Posted on 2010-11-21
12
1,317 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
  • 5
  • 4
12 Comments
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Sheritlw
Comment Utility
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
Comment Utility
Forgot attachment DropDownList in Grid
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 83

Expert Comment

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

Author Comment

by:Sheritlw
Comment Utility
The linqdatasource that is bound to the dropdownlist contains the values.

Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Hi sheritlw,
Sorry for not being able to fix this for you but glad that you found a work around.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now