Link to home
Start Free TrialLog in
Avatar of sk1922
sk1922

asked on

Gridview binding issue - using Generic List


Trying to add to a List(Of Positions) within a loop and binding it back out to my gridview.  
However, my gridview binds only the last values stored in the PayGrade, PayScaleMin properties saved into my custom object (Salary)

So example, say I have 5 rows, all unique values are entered. OnBind, the values from ddl1 (Column1) and tb1 (Column2) are always unique on Bind.  But tb2 (column3) and tb3 (column4) are set to the last two values that were passed in when calling executing the ItemCommand method.  


---- this is inside my GV_ItemCommand() method.

if e.CommandName = "myJobs" Then

                For Each row As GridViewRow In gv_newJobs.Rows

                    oSalary.PayScaleMin = Nothing
                    oSalary.PayGrade = Nothing

                    Dim ddl1 As DropDownList = DirectCast(row.FindControl("ddl_jobType"), DropDownList)
                    Dim tb1 As String = DirectCast(row.FindControl("tb_JobTitle"), TextBox).Text
                    Dim tb2 As DropDownList = DirectCast(row.FindControl("ddl_payGrade"), DropDownList)
                    Dim tb3 As String = DirectCast(row.FindControl("tb_SalaryRange"), TextBox).Text

                    'test:  check position list for regular employees
                    oSalary.PayGrade = tb2.SelectedValue
                    oSalary.PayScaleMin = tb3
                    oPositionRequest.RegularPositionPositionList.Add(New Position(ddl1.SelectedValue, tb1, oSalary))

                    Session("objPositionRequest") = oPositionRequest

                Next

' some more code  and logic here (unrelated to issue)....


                    Dim cPositionRequest As PositionRequest = DirectCast(Session("objPositionRequest"), PositionRequest)
                    Dim oList As List(Of Position) = cPositionRequest.RegularPositionPositionList
                    gv_addedJobs.DataSource = oList
                    gv_addedJobs.DataBind()





----------------  my .aspx code for GV


                    <asp:GridView ID="gv_addedJobs" runat="server" enable ShowFooter="true" AutoGenerateColumns="false">
                   
                            <Columns>
                            <asp:TemplateField HeaderText="Job Type">
                                <ItemTemplate>
                                    <asp:Label ID="lbl1" runat="server" Text='<%#Eval("JobTypeID")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddl_jobType" runat="server" SelectedValue='<%#Eval("JobTypeID")%>'>
                                    </asp:DropDownList>                                    
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Job Title">
                                <ItemTemplate>
                                    <asp:Label ID="lbl2" runat="server" Text='<%#Eval("JobTitle")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="tb_JobTitle" runat="server" Text='<%#Eval("JobTitle")%>' class="autocomp"></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Pay Grade">
                                <ItemTemplate>
                                    <asp:Label ID="lbl3" runat="server" Text='<%#Eval("SalaryType.PayGrade")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddl_payGrade" runat="server" OnSelectedIndexChanged="ddl_payGrade_SelectedIndexChanged" AutoPostBack="true" SelectedValue='<%#Eval("SalaryType.PayGrade")%>'>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Salary Range">
                                <ItemTemplate>
                                    <asp:Label ID="lbl4" runat="server" Text='<%#Eval("SalaryType.PayScaleMin")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="tb_PayScale" runat="server" Text='<%#Eval("SalaryType.PayScaleMin")%>' ReadOnly="true"></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            </Columns>
                           
                        </asp:GridView>

End If
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you saying that test don't work

   Eval("JobTypeID")
   Eval("JobTitle")

but, these do?
   
   Eval("SalaryType.PayGrade")
   Eval("SalaryType.PayScaleMin")
Avatar of sk1922
sk1922

ASKER

actually the opposite.

The first two Evals are unique.  The bottom two are not.  It always loops only the last items in the list.  

My SalaryType is actually a container class and in my request class I have a property that allows for a list of positions...

Position
--- Salary
--- other properties here...

PositionRequest

-- PositionList(Of Position)
I am not sure what "loops only the last items in the list" means...are you saying that there are problems with the rows, or columns?

Can you show me how the GridView is rendered?
Avatar of sk1922

ASKER

Hi there,

I can't at the moment as the code is setup on another dev machine I can't access right away.
But basically, here's what happens...

Say you have a 4 col, 3 row gridview... that binds to an empty DataTable and then I submit 3 unique records

4 cols - set up as:
--------------
Job Type | Job Title | Pay Grade | Pay Scale


3 rows - entered values:
--------------
row 1 : Manager | Some Cool Manager | 8 | 10 - 20K
row 2 : Asst. | Some Neat Asst. | 4 | 2 - 4K
row 3 : Specialist | Some Nerdy Spec | 6 | 9 - 12K

Now, when I click submit, I am looping through all the rows using For Each gv.....

In my db, the values stored are consistent with what was entered in the GridView rows.  I also stuff each row in a List(Of Positions) within the For Each and then bind that same list back out to my GV.  OnBind, all rows bind like this:

3 rows - entered values:
--------------
row 1 : Manager | Some Cool Manager | 6 | 9 - 12K
row 2 : Asst. | Some Neat Asst. | 6 | 9 - 12K
row 3 : Specialist | Some Nerdy Spec | 6 | 9 - 12K

Does this help?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sk1922

ASKER

Ahhhhh,  I think I have it outside the loop..... I need to move it into the loop like this...
For Each row As GridViewRow In gv_newJobs.Rows

                   Dim oSalary As New Salary()  = Nothing

                    Dim ddl1 As DropDownList = DirectCast(row.FindControl("ddl_jobType"), DropDownList)
                    Dim tb1 As String = DirectCast(row.FindControl("tb_JobTitle"), TextBox).Text
                    Dim tb2 As DropDownList = DirectCast(row.FindControl("ddl_payGrade"), DropDownList)
                    Dim tb3 As String = DirectCast(row.FindControl("tb_SalaryRange"), TextBox).Text

                    'test:  check position list for regular employees
                    oSalary.PayGrade = tb2.SelectedValue
                    oSalary.PayScaleMin = tb3
                    oPositionRequest.RegularPositionPositionList.Add(New Position(ddl1.SelectedValue, tb1, oSalary))

                    Session("objPositionRequest") = oPositionRequest

                Next

Open in new window

This:


Dim oSalary As New Salary()  = Nothing

should be this:


Dim oSalary As New Salary()
Avatar of sk1922

ASKER

Got it.  I just ran it by creating a similar project and it worked!  Can't believe I missed that! Thanks TheLearnedOne.