locked
In gridView After adding New Row Dropdownlist of Previous row looses its Selected Item but i want that in Previous Row keep selected RRS feed

  • Question

  • User1403110688 posted

    My code is

    <asp:TemplateField HeaderText="Product Name">
    <ItemTemplate>
    <asp:Label ID="lbldropname" runat="server" Visible = "false" Text='<% # Bind("pname") %>' />
    <asp:DropDownList ID="droppname" runat="server" OnSelectedIndexChanged="droppname_OnSelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>

    Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)


    If (e.Row.RowType = DataControlRowType.DataRow) Then

    Dim droppname As DropDownList = DirectCast(e.Row.FindControl("droppname"), DropDownList)


    droppname.DataSource = GetData("SELECT DISTINCT pname from product")
    droppname.DataTextField = "pname"
    droppname.DataValueField = "pname"

    droppname.DataBind()

    droppname.Items.Insert(0, New ListItem("Please select"))

    End If

    End Sub

    Private Sub AddNewRowToGrid()

    Dim rowIndex As Integer = 0

    If ViewState("CurrentTable") IsNot Nothing Then
    Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
    Dim drCurrentRow As DataRow = Nothing

    If dtCurrentTable.Rows.Count > 0 Then
    For i As Integer = 1 To dtCurrentTable.Rows.Count Step i + 1

    Dim box1 As DropDownList = DirectCast(GridView1.Rows(rowIndex).Cells(0).FindControl("droppname"), DropDownList)
    Dim box2 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("Lbl2"), Label)
    Dim box3 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtbatch"), TextBox)
    Dim box4 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtrate"), TextBox)
    Dim box5 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(3).FindControl("txtquant"), TextBox)
    Dim box6 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(4).FindControl("Lbltotal"), Label)
    drCurrentRow = dtCurrentTable.NewRow()
    drCurrentRow("Row") = i + 1
    dtCurrentTable.Rows(i - 1)("pname") = box1.Text
    dtCurrentTable.Rows(i - 1)("pcode") = box2.Text
    dtCurrentTable.Rows(i - 1)("Column3") = box3.Text
    dtCurrentTable.Rows(i - 1)("Column4") = box4.Text
    dtCurrentTable.Rows(i - 1)("Column5") = box5.Text
    dtCurrentTable.Rows(i - 1)("Column6") = box5.Text
    rowIndex += 1
    Next

    dtCurrentTable.Rows.Add(drCurrentRow)
    ViewState("CurrentTable") = dtCurrentTable
    GridView1.DataSource = dtCurrentTable
    GridView1.DataBind()

    End If
    Else
    Response.Write("ViewState is null")
    End If
    End Sub

    Sunday, May 21, 2017 6:04 PM

Answers

  • User-271186128 posted

    Hi iampoojaarora,

    Do you call AddNewRowToGrid by using the button click event?
    When you click a button, the page is refreshed.
    You need to set the data to your controls after you add a row.
    And please try to refer to the following code and use SelectedIndex as the value of DropDownList.
    For example:

    Private Sub AddNewRowToGrid()
            Dim rowIndex As Integer = 0
            If ViewState("CurrentTable") IsNot Nothing Then
                Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
                Dim drCurrentRow As DataRow = Nothing
                If dtCurrentTable.Rows.Count > 0 Then
                    For i As Integer = 1 To dtCurrentTable.Rows.Count Step i + 1
                        Dim box1 As DropDownList = DirectCast(GridView1.Rows(rowIndex).Cells(0).FindControl("droppname"), DropDownList)
                        Dim box2 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("Lbl2"), Label)
                        Dim box3 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtbatch"), TextBox)
                        Dim box4 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtrate"), TextBox)
                        Dim box5 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(3).FindControl("txtquant"), TextBox)
                        Dim box6 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(4).FindControl("Lbltotal"), Label)
                        drCurrentRow = dtCurrentTable.NewRow()
                        drCurrentRow("Row") = i + 1
                        dtCurrentTable.Rows(i - 1)("pname") = box1.SelectedIndex
                        dtCurrentTable.Rows(i - 1)("pcode") = box2.Text
                        dtCurrentTable.Rows(i - 1)("Column3") = box3.Text
                        dtCurrentTable.Rows(i - 1)("Column4") = box4.Text
                        dtCurrentTable.Rows(i - 1)("Column5") = box5.Text
                        dtCurrentTable.Rows(i - 1)("Column6") = box5.Text
                        rowIndex += 1
                    Next
                    dtCurrentTable.Rows.Add(drCurrentRow)
                    ViewState("CurrentTable") = dtCurrentTable
                    GridView1.DataSource = dtCurrentTable
                    GridView1.DataBind()
                End If
               SetPreviousData()
            Else
                Response.Write("ViewState is null")
            End If
        End Sub
    
        Private Sub SetPreviousData()
            Dim rowIndex As Integer = 0
            If ViewState("CurrentTable") IsNot Nothing Then
                Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
                If dt.Rows.Count > 0 Then
                    For i As Integer = 0 To dt.Rows.Count - 2
                        Dim box1 As DropDownList = DirectCast(GridView1.Rows(rowIndex).Cells(0).FindControl("droppname"), DropDownList)
                        Dim box2 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(1).FindControl("Lbl2"), Label)
                        Dim box3 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtbatch"), TextBox)
                        Dim box4 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(2).FindControl("txtrate"), TextBox)
                        Dim box5 As TextBox = DirectCast(GridView1.Rows(rowIndex).Cells(3).FindControl("txtquant"), TextBox)
                        Dim box6 As Label = DirectCast(GridView1.Rows(rowIndex).Cells(4).FindControl("Lbltotal"), Label)
    
                        box1.SelectedIndex = Convert.ToInt32(dt.Rows(i)("pname"))
                        box2.Text = dt.Rows(i)("pcode").ToString()
                        box3.Text = dt.Rows(i)("Column3").ToString()
                        box4.Text = dt.Rows(i)("Column4").ToString()
                        box5.Text = dt.Rows(i)("Column5").ToString()
                        box6.Text = dt.Rows(i)("Column6").ToString()
    
                        rowIndex += 1
                    Next
                End If
            End If
        End Sub
    

    And the result is:

    If you want to use the DropDownList selected Value or Text value, you could use the FindByValue or FindByText method to find the DropDownList Item, and change the Selected property to true. Code as below:

                    ListItem item = DropDownList2.Items.FindByText("AAA");
                    if (item != null)
                        item.Selected = true;

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 22, 2017 5:47 AM