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
476 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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 …

776 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