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
471 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

11 Experts available now in Live!

Get 1:1 Help Now