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
487 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
[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
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

696 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