Solved

Populating  Dropdown Lists for respective rows in a grid view for preference column with values -,1,2,3 and 4 such that when 1 is selected other dropdown lists in other rows don't show 1 &  likewise.

Posted on 2013-01-11
1
480 Views
Last Modified: 2013-02-26
We have a Given situation to populate the drop downs (Preference Selection Column) of each rows of a Gridview such that for a given rows in a gridview only that many no of  Preferences should be present in the drop down lists of the grid view.
The Values to be present in drop down  are "-", "1", "2", "3" and "4".
Suppose we have 3 rows fetched in gridview then drop down list of all rows can at max show values from "-" to "4".
When we select any list item on a particular column, then  dropdown list of other rows should not show that value in data list. (we have to remove the data item)
and hence it goes on.

The Code.

Protected Sub chk()
        'Session("ids") = ""

        'For i As Integer = 0 To HPrefenceGridView.Rows.Count - 1
        '    Dim ddl As DropDownList = CType(HPrefenceGridView.Rows(i).Cells(5).FindControl("preference"), DropDownList)
        '    If ddl.SelectedValue <> "-" Then
        '        Session("ids") = Session("ids") & ddl.SelectedValue
        '    End If

        'Next
        Dim connec As New OleDbConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
        If connec.State = ConnectionState.Closed Then
            connec.Open()
        End If
        Dim str As String = "SELECT ROWNUM, T3.TBH_HO_CODE HOUSE_CODE, T3.ADDRESS ADDRESS, T3.FLOOR_INFO FLOOR, T3.RENT_INFO RENT FROM (SELECT   T2.TBH_HO_CODE, DF_ADD_FETCH (T2.TBH_HO_CODE) ADDRESS,PKG_HOUSE.FLOOR_INFO (T2.TBH_HO_CODE) FLOOR_INFO,PKG_HOUSE.RENT_INFO (T2.TBH_HO_CODE) RENT_INFO FROM T_BAC_HOUSE T2, T_CYCLE_MASTER T1 WHERE(T1.TCM_CYCLE_NO = T2.TBH_CYCLE_NO) AND T2.TBH_UPLOADED='Y' AND TRUNC (SYSDATE) BETWEEN TRUNC (T1.TCM_START_DATE) AND TRUNC (T1.TCM_END_DATE) ORDER BY T2.TBH_HO_CODE) T3"

        Dim cmd As New OleDbCommand(str, connec)
        Dim ad As New OleDbDataAdapter(cmd)
        Dim ds As New DataSet
        ad.Fill(ds)

        HPrefenceGridView.DataSource = ds
        HPrefenceGridView.DataBind()
        cmd.Dispose()
        ad.Dispose()
        ds.Dispose()
        Dim n As Integer = HPrefenceGridView.Rows.Count - 1
        Dim i As Integer
        For i = 0 To n
            Dim ddl As DropDownList = CType(HPrefenceGridView.Rows(i).Cells(5).FindControl("preference"), DropDownList)

            Select Case HPrefenceGridView.Rows.Count

                Case 1
                    ddl.Items.Insert(0, New ListItem("0", "0"))
                    ddl.Items.Insert(1, New ListItem("1", "1"))

                    ddl.SelectedItem.Text = "-"



                Case 2
                    ddl.Items.Insert(0, New ListItem("-", "0"))
                    ddl.Items.Insert(1, New ListItem("1", "1"))
                    ddl.Items.Insert(2, New ListItem("2", "2"))
                    ddl.SelectedItem.Text = "-"

               Case 3
                    Dim newListItem As ListItem
                    newListItem = New ListItem("-", "-")
                    newListItem.Selected = True
                    Dim newListItem1 As ListItem
                    newListItem1 = New ListItem("1", "1")
                    Dim newListItem2 As ListItem
                    newListItem2 = New ListItem("2", "2")
                    Dim newListItem3 As ListItem
                    newListItem3 = New ListItem("3", "3")
                    ddl.Items.Add(newListItem)
                    ddl.Items.Add(newListItem1)
                    ddl.Items.Add(newListItem2)
                    ddl.Items.Add(newListItem3)

                    Case Else

                        ddl.Items.Insert(0, New ListItem("0", "0"))
                       ddl.Items.Insert(1, New ListItem("1", "1"))
                       ddl.Items.Insert(2, New ListItem("2", "2"))
                       ddl.Items.Insert(3, New ListItem("3", "3"))
                       ddl.Items.Insert(4, New ListItem("4", "4"))
                      ddl.SelectedItem.Text = "-"

            End Select

        Next
End Sub


Problem is that:
Data populates such that no of pref items is equal to no of rows in grid view.
But when one is selected that value comes up again in another dropdown of other rows.

Please give a suggestion to solve this or a method to fire when we call the OnSelectedIndexChanged event of dropdown. Code Mentioned to populate.
0
Comment
Question by:surendra_oracle
1 Comment
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
ID: 38769391
Hi,

You can achieve it using JavaScript/jQuery!

I have created a sample using jQuery -

GridView
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                    <asp:ListItem>A</asp:ListItem>
                    <asp:ListItem>B</asp:ListItem>
                    <asp:ListItem>C</asp:ListItem>
                    <asp:ListItem>D</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
            
              <FooterTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click1" />
            </FooterTemplate>
        </asp:TemplateField>
    
   
        </Columns>
        </asp:gridview>

Open in new window


Script
     <script type="text/javascript">
        $('#' + '<%=Gridview1.ClientID %>' + ' select').change(function () {
            var myDataList = new Array();
            $('select').each(function () {

                $("option").each(function () {

                    if ($(this).attr("selected") && $(this).html() != "Select") {

                        var mylist = $(this).html();
                        myDataList.push(mylist);

                    }

                    for (i = 0; i < myDataList.length; i++) {
                        if (myDataList[i] == $(this).html() && mylist != $(this).html()) {
                            $(this).remove();
                        }
                    }

                });

            });
        });



    </script>

Open in new window


Hope it helps u...
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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