?
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
Medium Priority
?
491 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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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.
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)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

752 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