locked
dropdownlist.selectedvalue not saved inside grideview RRS feed

  • Question

  • User66371569 posted

    I have grideview  

    <asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" style="font-weight: 700" Width="754px">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="select" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="StatusID" style="font-weight: 700; font-size: medium">
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#94BE10" Font-Bold="True" Font-Size="14pt" ForeColor="White" />
    <PagerStyle BorderColor="#8CAC21" BorderStyle="Solid" ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="White" BorderColor="#99CC00" Font-Size="13pt" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

    has one dropdownlist            if have 3 rows     when I change   dropdownlist  for fist row   

    and press save button     its saves  last  row value for  the 3 rows 

    How can fix to let  grideview check dropdownlist for each row and save

    my code for save button

    For Each gvRow2 As GridViewRow In GridView2.Rows
    Dim conTest As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("xxx").ToString())

    Dim dropdown As DropDownList = gvRow2.FindControl("DropDownList2")


    conTest.Open()


    Dim cc As New SqlCommand("update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(4).Text & "'", conTest)


    cc.ExecuteNonQuery()
    Next

    conTest.close()

    Sunday, September 30, 2018 3:35 PM

Answers

  • User283571144 posted

    Hi thepast,

    I suggest you could try to use  Response.Write to write the sql command to make sure it geneated the right query.

    Like below:

    Inside the Button1_Click event.

                Dim re As String = "update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(2).Text & "'"
                Response.Write(" Result : " + re + "<br/>")

    Here I created a test demo on my side, it generate the right query:

    Page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DDLWithGridview.aspx.vb" Inherits="VBWebform.DDLWithGridview" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Style="font-weight: 700" Width="754px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" CssClass="select" DataSourceID="SqlDataSource2" DataTextField="BookId" DataValueField="BookId" Style="font-weight: 700; font-size: medium">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <HeaderStyle BackColor="#94BE10" Font-Bold="True" Font-Size="14pt" ForeColor="White" />
                    <PagerStyle BorderColor="#8CAC21" BorderStyle="Solid" ForeColor="#8C4510" HorizontalAlign="Center" />
                    <RowStyle BackColor="White" BorderColor="#99CC00" Font-Size="13pt" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FFF1D4" />
                    <SortedAscendingHeaderStyle BackColor="#B95C30" />
                    <SortedDescendingCellStyle BackColor="#F1E5CE" />
                    <SortedDescendingHeaderStyle BackColor="#93451F" />
                </asp:GridView>
    
    
    
    
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 <br />
                <br />
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-VbIdentity-20180507035628ConnectionString %>" SelectCommand="SELECT * FROM [Books]"></asp:SqlDataSource>
    
                <br />
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

        Protected Sub Button1_Click(sender As Object, e As EventArgs)
            'Dim conTest As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("xxx").ToString())
            For Each gvRow2 As GridViewRow In GridView2.Rows
                Dim dropdown As DropDownList = gvRow2.FindControl("DropDownList2")
                Dim re As String = "update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(2).Text & "'"
                Response.Write(" Result : " + re + "<br/>")
    
                ' conTest.Open()
                'Dim cc As New SqlCommand("update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(4).Text & "'", conTest)
                ' cc.ExecuteNonQuery()
            Next
    
            'conTest.close()
        End Sub
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 2, 2018 5:53 AM

All replies

  • User283571144 posted

    Hi thepast,

    and press save button     its saves  last  row value for  the 3 rows 

    How can fix to let  grideview check dropdownlist for each row and save

    According to your codes and description, I couldn't understand your issue clearly.

    Could you pleas tell me what do you mean about "its  saves last row value for the 3 rows"?It just update last row's value?

    I suggest you could try to set breakpoint at the SqlCommand line to make sure the "dropdown.SelectedValue.ToString" and the "gvRow2.Cells(4).Text " has get the right value.

    More details about how to use breakpoint you could refer to below article:

    https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017 

    Besides,I found you have some issue with your button clieck event. I found the conTest is inside foreach loop, but you call "conTest.close()" outside the loop, this will cause obejct not found issue.

    I suggest you could modify as below:

    conTest.Open()
    For Each gvRow2 As GridViewRow In GridView2.Rows
    Dim conTest As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("xxx").ToString())
    Dim dropdown As DropDownList = gvRow2.FindControl("DropDownList2")
    Dim cc As New SqlCommand("update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(4).Text & "'", conTest)
    cc.ExecuteNonQuery()
    Next
    
    conTest.close()

    Best Regards,

    Brando

    Monday, October 1, 2018 5:33 AM
  • User66371569 posted

    I will clear it for you

    for example    I have   gridview   4 rows      contain  Template filed    dropdownlist                             first row        I select  dropdown  1     second    2    third 3   forth 4

    when I save   it comes   4  for all rows in dropdownlist column   in database  

    Monday, October 1, 2018 5:53 AM
  • User283571144 posted

    Hi thepast,

    I suggest you could try to use  Response.Write to write the sql command to make sure it geneated the right query.

    Like below:

    Inside the Button1_Click event.

                Dim re As String = "update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(2).Text & "'"
                Response.Write(" Result : " + re + "<br/>")

    Here I created a test demo on my side, it generate the right query:

    Page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DDLWithGridview.aspx.vb" Inherits="VBWebform.DDLWithGridview" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Style="font-weight: 700" Width="754px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" CssClass="select" DataSourceID="SqlDataSource2" DataTextField="BookId" DataValueField="BookId" Style="font-weight: 700; font-size: medium">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <HeaderStyle BackColor="#94BE10" Font-Bold="True" Font-Size="14pt" ForeColor="White" />
                    <PagerStyle BorderColor="#8CAC21" BorderStyle="Solid" ForeColor="#8C4510" HorizontalAlign="Center" />
                    <RowStyle BackColor="White" BorderColor="#99CC00" Font-Size="13pt" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#FFF1D4" />
                    <SortedAscendingHeaderStyle BackColor="#B95C30" />
                    <SortedDescendingCellStyle BackColor="#F1E5CE" />
                    <SortedDescendingHeaderStyle BackColor="#93451F" />
                </asp:GridView>
    
    
    
    
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 <br />
                <br />
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-VbIdentity-20180507035628ConnectionString %>" SelectCommand="SELECT * FROM [Books]"></asp:SqlDataSource>
    
                <br />
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

        Protected Sub Button1_Click(sender As Object, e As EventArgs)
            'Dim conTest As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("xxx").ToString())
            For Each gvRow2 As GridViewRow In GridView2.Rows
                Dim dropdown As DropDownList = gvRow2.FindControl("DropDownList2")
                Dim re As String = "update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(2).Text & "'"
                Response.Write(" Result : " + re + "<br/>")
    
                ' conTest.Open()
                'Dim cc As New SqlCommand("update ss set endeffect=getdate(),statusid='" & dropdown.SelectedValue.ToString & "' Where id= '" & gvRow2.Cells(4).Text & "'", conTest)
                ' cc.ExecuteNonQuery()
            Next
    
            'conTest.close()
        End Sub
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 2, 2018 5:53 AM
  • User66371569 posted
    Thank u so much My problem was that i run code in page load so i put it in button click to avoid posrback then i created function run this button on click when page load for first time


    thank u so much for your reply
    Wednesday, October 3, 2018 7:25 PM