This is a migrated thread and some comments may be shown as answers.

remove a row from radgrid

2 Answers 306 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Myriam
Top achievements
Rank 1
Myriam asked on 12 Oct 2011, 01:35 PM
Hello
I have a Radgrid in a webform that contain only one combobox in each row. That combo box contain city. It is used to calculate how many kilometers it has between each city. (Like google). I am able to add line dynamicly and remove line too. When I remove line, it's always the last row that I'm taking off. And after, I rebind my datagrid. The thing is, all my combobox before lose there value the user selected. It put back the default value.
How can I keep the values the user selected in others combobox?
Here is my code:
<telerik:RadGrid 
       ID="RadGrid2" 
       Skin="Vista" 
       runat="server" 
       ShowFooter="true" 
       CommandItemStyle-HorizontalAlign="Center" Width="200px">  
                                   
       <MasterTableView 
            ShowFooter="true" 
            CommandItemDisplay="bottom" 
            EditMode="InPlace" >  
            <CommandItemTemplate>
                <asp:LinkButton ID="LinkButton1" Visible="false" CommandName="CalculDistance" Runat="server" CssClass="TexteBlanc16">Calculer</asp:LinkButton>
            </CommandItemTemplate>
            <Columns>                     
                 <telerik:GridTemplateColumn UniqueName="CodePostal" HeaderText="Destinations">  
                        <ItemTemplate>  
                                 <asp:DropDownList ID="LstCodePostal" runat="server"></asp:DropDownList>
                         </ItemTemplate>  
                  </telerik:GridTemplateColumn>  
            </Columns>  
      </MasterTableView>  
</telerik:RadGrid
<asp:Panel ID="Panel1" runat="server">
      <asp:LinkButton ID="LKBtnAjouterDestination" runat="server">Ajouter une destination</asp:LinkButton>
      <asp:LinkButton ID="LKBtnEnleverDestination" runat="server">Enlever une destination</asp:LinkButton>
 </asp:Panel>


Public dt As DataTable 
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
      If Not IsPostBack = True Then 
          dt = New DataTable() 
          dt.Columns.Add(New DataColumn("RowNumber", GetType(String))) 
          AjouterLigne(dt) 
      End If 
    
 Private Sub LKBtnAjouterDestination_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LKBtnAjouterDestination.Click 
        Dim countitem As Integer 
        Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable) 
    
        countitem = RadGrid2.Items.Count 
        Dim cpt As Integer = 0 
        For Each Item As GridDataItem In RadGrid2.Items 
    
            Dim Lstcode As DropDownList = DirectCast(Item("CodePostal").FindControl("LstCodePostal"), DropDownList) 
            If Not (Lstcode.Text Is Nothing) And Lstcode.Text <> "" Then 
                liste.Add(Lstcode.Text) 
            End If 
    
        Next 
        ViewState("dt") = AjouterLigne(dt) 
        listcount = liste.Count 
    
        RadGrid2.Rebind() 
    
        While cpt < listcount
            Dim Lstcode As DropDownList = DirectCast(RadGrid2.Items(cpt)("CodePostal").FindControl("LstCodePostal"), DropDownList) 
            Lstcode.Text = liste.Item(cpt) 
            cpt = cpt + 1 
        End While 
    End Sub 
    
    Private Sub LKBtnEnleverDestination_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LKBtnEnleverDestination.Click 
        Dim countitem As Integer 
        Dim dt As DataTable = DirectCast(ViewState("dt"), DataTable) 
    
        countitem = RadGrid2.Items.Count 
    
        ViewState("dt") = EnleverLigne(dt, countitem) 
        listcount = liste.Count 
    
        RadGrid2.Rebind() 
    
    End Sub 
    
    Public Function AjouterLigne(ByVal dt As DataTable) As DataTable 
        ' method to create row  
        Dim dr As DataRow = dt.NewRow() 
        dr("RowNumber") = "" 
        dt.Rows.Add(dr) 
        Return dt 
    End Function 
    
    Public Function EnleverLigne(ByVal dt As DataTable, ByVal IndexDernier As Integer) As DataTable 
        ' method to delete row  
        If IndexDernier > 0 Then 
            dt.Rows(IndexDernier - 1).Delete() 
        End If 
        Return dt 
    End Function 
    
    Private Sub RadGrid2_ColumnCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles RadGrid2.ColumnCreated 
        If (e.Column.UniqueName = "RowNumber") Then 
            e.Column.Visible = False 
        End If 
    End Sub 
    
    Private Sub RadGrid2_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemDataBound 
        Dim cpt As Integer = 0 
        Do While cpt < RadGrid2.Items.Count 
            'Dim Lstcode As DropDownList = DirectCast(Item("CodePostal").FindControl("LstCodePostal"), DropDownList) 
            Dim Lstcode As DropDownList = DirectCast(RadGrid2.Items(cpt)("CodePostal").FindControl("LstCodePostal"), DropDownList) 
            Lstcode.DataSource = DsVilleCodePostal 
            Lstcode.DataTextField = "VILLE" 
            Lstcode.DataValueField = "CODE_POSTAL" 
            Lstcode.DataBind() 
            Lstcode.SelectedValue = "G5Y7R7" 
            cpt = cpt + 1 
        Loop 
    
           
    End Sub 
    
    Private Sub RadGrid2_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid2.NeedDataSource 
        If Not IsPostBack Then 
            dt = AjouterLigne(dt) 
            ' call the method to create row  
            ViewState("dt") = dt 
        End If 
        dt = DirectCast(ViewState("dt"), DataTable) 
        RadGrid2.DataSource = dt 
           
    End Sub 
    
  Private Sub RadGrid2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid2.PreRender 
        RadGrid2.MasterTableView.RenderColumns(1).Display = False 
    End Sub


Thank you
Myriam

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 12 Oct 2011, 07:17 PM
Hello,

This thing was not automatically managed due to stateless issue of browser.

solution 1 :
after clicked on delete and before perform delete operation you can save combobox state in session/viewstate.
after performed the delete operation it automatically called rebind event so
in itemDataBound event again set combobox value  using session/viewstate.

solution 2:
after the changing the value of combobox you can save their value in sesssion/viewstate
after performed the delete operation it automatically called rebind event so
in itemDataBound event again set combobox value  using session/viewstate.

Note : solution 1 is the best choice to achieve your requirement. 

let me know if any concern.

Thanks,
Jayesh Goyani
0
Myriam
Top achievements
Rank 1
answered on 13 Oct 2011, 04:21 PM
Thank you for the reply.
Can you tell me how to put it in session/viewstate? I don't know what you mean.. sorry..
Thank you
Myriam
Tags
Grid
Asked by
Myriam
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Myriam
Top achievements
Rank 1
Share this question
or