• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Gridview row edit only works properly for odd rows

So I've created modified the edit mode of my gridview to include dropdown lists, and bound the data programmatically using the rowdatabound event. The odd rows work fine, but the even rows do not retain the selected values in the dropdown lists when I click edit, they revert back to the top values in each dropdown list.

From the research I've done it seems as if I need to modify the if statement in my rowdatabound event, but I haven't quite found the proper code fo rthe if statement (possibly because all the examples I've seen are in C# and I'm using VB). Here's the code for my rowdatabound event:

    Sub gvConsultant_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        ' Check for a row in edit mode.
        If (e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Alternate)) Then

            ' Preselect the DropDownList control with the state value
            ' for the current row.

            ' Retrieve the underlying data item. In this example
            ' the underlying data item is a DataRowView object.
            Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)

            ' Retrieve the state value for the current row.
         
            Dim ProjectType As String = rowView("ProjectType").ToString()
            Dim BuildingName As String = rowView("BuildingName").ToString()
            Dim Remedy As String = rowView("Remedy").ToString()
            Dim ComplexityFactor As String = rowView("ComplexityFactor").ToString()
            Dim BuildingCategory As String = rowView("BuildingCategory").ToString()
            Dim RecommendedTimeframe As String = rowView("RecommendedTimeframe").ToString()
            Dim FurtherStudyRequired As String = rowView("FurtherStudyRequired").ToString()
            Dim MajorSystem As String = rowView("MajorSystem").ToString()
            Dim Subsystem As String = rowView("Subsystem").ToString()
           
            ' Retrieve the DropDownList control from the current row.
            Dim list As DropDownList = CType(e.Row.FindControl("ddlProjectType"), DropDownList)
            Dim list2 As DropDownList = CType(e.Row.FindControl("ddlBuildingName"), DropDownList)
            Dim list3 As DropDownList = CType(e.Row.FindControl("ddlRemedy"), DropDownList)
            Dim list4 As DropDownList = CType(e.Row.FindControl("ddlComplexity"), DropDownList)
            Dim list5 As DropDownList = CType(e.Row.FindControl("ddlBuildingCategory"), DropDownList)
            Dim list6 As DropDownList = CType(e.Row.FindControl("ddlRecommendedTimeframe"), DropDownList)
            Dim list7 As DropDownList = CType(e.Row.FindControl("ddlFStudy"), DropDownList)
            Dim list8 As DropDownList = CType(e.Row.FindControl("ddlMajor"), DropDownList)
            Dim list9 As DropDownList = CType(e.Row.FindControl("ddlSubsystem"), DropDownList)
           
            ' Find the ListItem object in the DropDownList control with the
            ' state value and select the item.
            Dim item As ListItem = list.Items.FindByText("ProjectType")
            Dim item2 As ListItem = list2.Items.FindByText("BuildingName")
            Dim item3 As ListItem = list3.Items.FindByText("Remedy")
            Dim item4 As ListItem = list4.Items.FindByText("ComplexityFactor")
            Dim item5 As ListItem = list5.Items.FindByText("BuildingCategory")
            Dim item6 As ListItem = list6.Items.FindByText("RecommendedTimeframe")
            Dim item7 As ListItem = list7.Items.FindByText("FurtherStudyRequired")
            Dim item8 As ListItem = list8.Items.FindByText("MajorSystem")
            Dim item9 As ListItem = list9.Items.FindByText("Subsystem")
           
            list.SelectedIndex = list.Items.IndexOf(item)
            list2.SelectedIndex = list2.Items.IndexOf(item2)
            list3.SelectedIndex = list3.Items.IndexOf(item3)
            list4.SelectedIndex = list4.Items.IndexOf(item4)
            list5.SelectedIndex = list5.Items.IndexOf(item5)
            list6.SelectedIndex = list6.Items.IndexOf(item6)
            list7.SelectedIndex = list7.Items.IndexOf(item7)
            list8.SelectedIndex = list8.Items.IndexOf(item8)
            list9.SelectedIndex = list9.Items.IndexOf(item9)
        End If

As I said, the dropdown lists in the ODD rowspopulate properly with the selected values matching those contained in the database, but the even rows revert back to the top values in the dropdown lists.

Here's the code for some of the dropdown lists in the gridview, for good measure. There are quite a few and th code is rather lengthy so I've omitted some for the sake of readability.

                   <asp:TemplateField HeaderText="Building/Location" SortExpression="BuildingName">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlBuildingName" runat="server"
                            DataTextField="BuildingName"
                            DataValueField="BuildingName" SelectedValue='<%# Bind("BuildingName") %>'>
                            <asp:ListItem >Rogers Hall</asp:ListItem>
                            <asp:ListItem>Jacobs Academic Building</asp:ListItem>
                            <asp:ListItem>Jacobs Administration Building</asp:ListItem>
                            <asp:ListItem>Civil Engineering Building</asp:ListItem>
                            <asp:ListItem>Dibner Library</asp:ListItem>
                            <asp:ListItem>Othmer Residence Hall</asp:ListItem>
                            <asp:ListItem>Wunsch Hall</asp:ListItem>
                            <asp:ListItem>Grounds</asp:ListItem>
                            <asp:ListItem>Entire Campus</asp:ListItem>
                            <asp:ListItem>Other</asp:ListItem>
                        </asp:DropDownList>
                       
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("BuildingName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>      
                  <asp:BoundField DataField="DetailLocation" HeaderText="Detail Location"
                    SortExpression="DetailLocation" />
                      <asp:TemplateField HeaderText="MajorSystem" SortExpression="MajorSystem">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlMajor" runat="server"
                            DataTextField="MajorSystem" DataValueField="MajorSystem"
                            SelectedValue='<%# Eval("MajorSystem") %>'>
                           
                            <asp:ListItem>Disabled Accessibility</asp:ListItem>
                            <asp:ListItem>Campus Grounds</asp:ListItem>
                            <asp:ListItem>Doors</asp:ListItem>
                            <asp:ListItem>Eelectrical</asp:ListItem>
                            <asp:ListItem>Energy Conservation</asp:ListItem>
                            <asp:ListItem>Environmental Health & Safety</asp:ListItem>
                            <asp:ListItem>Exterior Walls</asp:ListItem>
                            <asp:ListItem>Fire Detection</asp:ListItem>
                            <asp:ListItem>Fire Protection</asp:ListItem>
                            <asp:ListItem>HVAC</asp:ListItem>
                            <asp:ListItem>Interiors</asp:ListItem>
                            <asp:ListItem>Other Building Systems</asp:ListItem>
                            <asp:ListItem>Plumbing</asp:ListItem>
                            <asp:ListItem>Roof System</asp:ListItem>
                            <asp:ListItem>Security</asp:ListItem>
                            <asp:ListItem>Special Construction System</asp:ListItem>
                            <asp:ListItem>Structure</asp:ListItem>
                            <asp:ListItem>Sustainibility Initiatives</asp:ListItem>
                            <asp:ListItem>Vertical Transportation</asp:ListItem>
                            <asp:ListItem>Windows</asp:ListItem>
                           
                        </asp:DropDownList>
                           
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("MajorSystem") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Subsystem" SortExpression="Subsystem">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlSubsystem" runat="server"
                            DataTextField="name"
                            DataValueField="name" SelectedValue='<%# Eval("Subsystem") %>'>
                           
                                <asp:ListItem>Building Approach and Entry</asp:ListItem>
                                <asp:ListItem>Public Corridors</asp:ListItem>
                                <asp:ListItem>Elevators</asp:ListItem>
                                <asp:ListItem>Places of Assembly</asp:ListItem>
                                <asp:ListItem>Toilet Rooms</asp:ListItem>
                                <asp:ListItem>Other</asp:ListItem>
                                <asp:ListItem>Disabled Accessibility</asp:ListItem>
                                <asp:ListItem>Exterior Lighting</asp:ListItem>
                                <asp:ListItem>Landscaping and Plantscaping</asp:ListItem>
                                <asp:ListItem>Loading/Unloading Trucks</asp:ListItem>
                                <asp:ListItem>Parking</asp:ListItem>
                                <asp:ListItem>Plazas</asp:ListItem>
                                <asp:ListItem>Site Furnishings and Art</asp:ListItem>
                                <asp:ListItem>Special Construction</asp:ListItem>
                                <asp:ListItem>Specialties</asp:ListItem>
                                <asp:ListItem>Storm Damage</asp:ListItem>
                                <asp:ListItem>Walkways</asp:ListItem>
                                <asp:ListItem>Interior</asp:ListItem>
                                <asp:ListItem>Exterior</asp:ListItem>
                                <asp:ListItem>Power Distribution</asp:ListItem>
                                <asp:ListItem>Grounding System</asp:ListItem>
                                <asp:ListItem>Wiring and Raceway System</asp:ListItem>
                                <asp:ListItem>Lighting</asp:ListItem>
                                <asp:ListItem>Emergency Lighing System</asp:ListItem>
                                <asp:ListItem>Lighting Protection System</asp:ListItem>
                                <asp:ListItem>Raised Floor Systems</asp:ListItem>
                                <asp:ListItem>Emergency Power Service</asp:ListItem>
                                <asp:ListItem>Special Systems(UPS...)</asp:ListItem>
                                <asp:ListItem>Facade</asp:ListItem>
                                <asp:ListItem>Appurtenances</asp:ListItem>
                                <asp:ListItem>Fire Escapes</asp:ListItem>
                                <asp:ListItem>Sprinkler System</asp:ListItem>
                                <asp:ListItem>Standpipe System</asp:ListItem>
                                <asp:ListItem>Fire Water Supply/Storage</asp:ListItem>
                                <asp:ListItem>Dry Chemical System</asp:ListItem>
                                <asp:ListItem>Special Systems(showers..)</asp:ListItem>
                                <asp:ListItem>Extinguishers</asp:ListItem>
                                <asp:ListItem>Building Management Systems</asp:ListItem>
                                <asp:ListItem>Heat Generating System</asp:ListItem>
                                <asp:ListItem>Heat Distribution</asp:ListItem>
                                <asp:ListItem>Air Conditioning Generating Systems</asp:ListItem>
                               <asp:ListItem>Air Distribution Systems</asp:ListItem>
                                <asp:ListItem>Ventilation Systems</asp:ListItem>
                                <asp:ListItem>Exhaust Systems</asp:ListItem>
                                <asp:ListItem>Special Systems</asp:ListItem>
                                <asp:ListItem>Mechanical Rooms</asp:ListItem>
                                <asp:ListItem>Flooring</asp:ListItem>
                                <asp:ListItem>Walls</asp:ListItem>
                                <asp:ListItem>Ceilings</asp:ListItem>
                                <asp:ListItem>Base</asp:ListItem>
                                <asp:ListItem>Compressed Air</asp:ListItem>
                                <asp:ListItem>Acid Neutrilization</asp:ListItem>
                                <asp:ListItem>Natural Gas</asp:ListItem>
                                <asp:ListItem>Cold Water System</asp:ListItem>
                                <asp:ListItem>Hot Water System</asp:ListItem>
                                <asp:ListItem>Waste Water Systems</asp:ListItem>
                                <asp:ListItem>Sanitary, Sewer and Storm Systems</asp:ListItem>
                                <asp:ListItem>Other Systems (Process water, acid waste..)</asp:ListItem>
                                <asp:ListItem>Parapets</asp:ListItem>
                                <asp:ListItem>Roof Surface</asp:ListItem>
                                <asp:ListItem>Bulkheads and Other Elements</asp:ListItem>
                                <asp:ListItem>Foundation</asp:ListItem>
                                <asp:ListItem>Substructure</asp:ListItem>
                                <asp:ListItem>Superstructure</asp:ListItem>
                                <asp:ListItem>Elevator Operating Systems</asp:ListItem>
                                <asp:ListItem>Elevator Cabs</asp:ListItem>
                                <asp:ListItem>Lifts</asp:ListItem>
                                <asp:ListItem>Elevator Equipment Rooms</asp:ListItem>
                               
                        </asp:DropDownList>
 
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("Subsystem">'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>    
                        <asp:BoundField DataField="BuildingSystemDeficiency"
                    HeaderText="Bld./System Field Deficiency"
                    SortExpression="BuildingSystemDeficiency" />  
                <asp:BoundField DataField="ProjectRecommendation"
                    HeaderText="Short Project Recommendation" SortExpression="ProjectRecommendation" />
                <asp:BoundField DataField="DetailedProjectRecommendation"
                    HeaderText="Detailed Project Recommendation"
                    SortExpression="DetailedProjectRecommendation" />
                  <asp:TemplateField HeaderText="Remedy" SortExpression="Remedy" ItemStyle-HorizontalAlign="Left">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlRemedy" runat="server"
                            DataTextField="Remedy" DataValueField="Remedy"
                            SelectedValue='<%# Eval("Remedy") %>'>
                           
                            <asp:ListItem>Patch/Repair</asp:ListItem>
                            <asp:ListItem>Clean/Repair</asp:ListItem>
                            <asp:ListItem>Clean/Refinish</asp:ListItem>
                            <asp:ListItem>Correct/Repair</asp:ListItem>
                            <asp:ListItem>Repair/Replace</asp:ListItem>
                            <asp:ListItem>Remove/Replace</asp:ListItem>
                            <asp:ListItem>Replace - Not in Kind</asp:ListItem>
                            <asp:ListItem>Modify/Upgrade</asp:ListItem>
                            <asp:ListItem>Retrofit/Upgrade</asp:ListItem>
                            <asp:ListItem>Install</asp:ListItem>
                            <asp:ListItem>Abandon/Remove</asp:ListItem>
                            <asp:ListItem>Test</asp:ListItem>
                        </asp:DropDownList>
                       
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Remedy") %>'></asp:Label>
                    </ItemTemplate>

<ItemStyle HorizontalAlign="Left"></ItemStyle>
                </asp:TemplateField>  





0
asphaltninja
Asked:
asphaltninja
  • 4
  • 3
1 Solution
 
Dustin HopkinsCommented:
Change the if statement:
  If (e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Alternate)) Then

To the statement below


Hope this helps,
Dustin
If e.Row.RowState = DataControlRowState.Edit Or _
                 (e.Row.RowState = (DataControlRowState.Alternate Or DataControlRowState.Edit)) Then

Open in new window

0
 
asphaltninjaAuthor Commented:
When I try that clause, It doesn't work for any rows at all. Should I be using a FOR EACH statement to do this?
0
 
Dustin HopkinsCommented:
Also you may want to add an extra if that encompasses anything you do in rows like so:

Now have you tried setting a breakpoint to see if the area is getting called and if there are any kindve errors?
 If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowState = DataControlRowState.Edit Or _ 
                 (e.Row.RowState = (DataControlRowState.Alternate Or DataControlRowState.Edit)) Then

stuff to do in edit

ELSE

 stuf to do outside of edit

end if
end if

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
asphaltninjaAuthor Commented:
Tried that to no avail. If it means anything, when I simply use
If e.Row.RowState = (DataControlRowState.Edit)
It works for the even rows and when I use
If e.Row.RowState = (DataControlRowState.Edit Or DataControlRowState.Alternate)
It works for odd rows

Thanks for all of your help.
0
 
Dustin HopkinsCommented:
Hmm lets try this below, as it should work. Also in case it helps, here is a similar question http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23408704.html



 If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowIndex = gvConsultant.EditIndex Then


0
 
asphaltninjaAuthor Commented:
I figured out the problem.

I went back to my original If clause:
If ((e.Row.RowState = (DataControlRowState.Edit)) Or (e.Row.RowState = (DataControlRowState.Alternate))) Then

which was throwing a nullrefeence exception for this block of code:

            Dim ProjectType As String = rowView("ProjectType").ToString()
            Dim BuildingName As String = rowView("BuildingName").ToString()
            Dim Remedy As String = rowView("Remedy").ToString()
            Dim ComplexityFactor As String = rowView("ComplexityFactor").ToString()
            Dim BuildingCategory As String = rowView("BuildingCategory").ToString()
            Dim RecommendedTimeframe As String = rowView("RecommendedTimeframe").ToString()
            Dim FurtherStudyRequired As String = rowView("FurtherStudyRequired").ToString()
            Dim MajorSystem As String = rowView("MajorSystem").ToString()
            Dim Subsystem As String = rowView("Subsystem").ToString()

After some head scratching, I handled the exception this way:

         Dim ProjectType As String
            If Not ProjectType Is Nothing Then
                ProjectType = rowView("ProjectType").ToString()
            End If

For all of the String variables, and now it works. Thanks for all of your help. I was able to figure out that it was the nullrefernce exception causing the problem by trying out the diffferent if clauses that you proposed.

0
 
asphaltninjaAuthor Commented:
The solution led me in the right direction, making it much easier to figure out where I went wrong with my code.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now