Reordering Dynamically generated Gridview rows which is inside a listview row
Hie

   I have gridviews inside each single row of a listview which are being generated dynamically. I need to reorder the rows(move the rows up and down) inside the dynamically generated gridviews, please help me. I can do it either by using java script or using up and down buttons but how to get the id of a dynamically generated gridview ?? i dont get it... 

I m posting my code for clear reference... 

        <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </cc1:ToolkitScriptManager>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:practiseConnectionString %>"
            SelectCommand="SELECT [PAB_ASMBLY_TYPE_ID] FROM [table1]"></asp:SqlDataSource>
        <br />
        <br />
        <asp:ListView ID="ListView2" runat="server" DataKeyNames="PAB_ASMBLY_TYPE_ID" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <tr style="background-color: #E0FFFF; color: #333333;">
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' Visible="false" />
                        <asp:LinkButton CausesValidation="false" ID="LinkButton2" runat="server">+</asp:LinkButton>
                        <asp:LinkButton CausesValidation="false" ID="LinkButton3" runat="server">-</asp:LinkButton>
                        <asp:Label ID="Label3" runat="server" Text="panel"></asp:Label>
                        <asp:Panel ID="Panel1" runat="server">
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:GridView ID="GridView1" DataKeyNames="QUE_ITEM_ID" runat="Server" AutoGenerateColumns="False"
                                        OnRowCommand="GridView1_RowCommand">
                                        <Columns>
                                            <asp:BoundField DataField="QUE_ID" HeaderText="QUE_ID" />
                                            <asp:BoundField AccessibleHeaderText="QUE_ITEM_ID" DataField="QUE_ITEM_ID" HeaderText="QUE_ITEM_ID" />
                                            <asp:BoundField AccessibleHeaderText="PAB_ASMBLY_TYPE_ID" DataField="PAB_ASMBLY_TYPE_ID"
                                                HeaderText="PAB_ASMBLY_TYPE_ID" />
                                            <asp:BoundField AccessibleHeaderText="PV_OBJ_DESC_TXT" DataField="PV_OBJ_DESC_TXT"
                                                HeaderText="PV_OBJ_DESC_TXT" />
                                            <asp:TemplateField HeaderText="Move">
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="ImgBtnUp" runat="server" ToolTip="Move Up" ImageUrl="~/Images/up.png"
                                                        CommandName="MoveUp" CommandArgument='<%# Eval("QUE_ITEM_ID") %>' />
                                                    <asp:ImageButton ID="ImgBtnDown" runat="server" ToolTip="Move Down" ImageUrl="~/Images/down.png"
                                                        CommandName="MoveDown" CommandArgument='<%# Eval("QUE_ITEM_ID") %>' />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </asp:Panel>
                        <cc1:CollapsiblePanelExtender ExpandControlID="LinkButton2" CollapseControlID="LinkButton3"
                            ID="CollapsiblePanelExtender1" TargetControlID="Panel1" runat="server" TextLabelID="Label3"
                            ExpandedText="collapse" CollapsedText="expand" SuppressPostBack="True" Collapsed="True">
                        </cc1:CollapsiblePanelExtender>
                    </td>
                </tr>
            </ItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse;
                    border-color: #999999; border-style: none; border-width: 1px;">
                    <tr>
                        <td>
                            No data was returned.
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                    </td>
                    <td>
                        <asp:TextBox ID="PAB_ASMBLY_TYPE_IDTextBox" runat="server" Text='<%# Bind("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;
                                border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;
                                font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr runat="server" style="background-color: #E0FFFF; color: #333333;">
                                    <th runat="server">
                                        PAB_ASMBLY_TYPE_ID
                                    </th>
                                </tr>
                                <tr id="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif;
                            color: #FFFFFF">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <EditItemTemplate>
                <tr style="background-color: #999999;">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                    </td>
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel1" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <SelectedItemTemplate>
                <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;">
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>

  
and here is my code behind...


Imports System.Data.SqlClient
Partial Public Class WebForm3
    Inherits System.Web.UI.Page
    Shared dt As DataTable



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BindGridViewView()
        End If
    End Sub

    Private Sub ListView2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView2.ItemDataBound
        If (e.Item.ItemType = ListViewItemType.DataItem) Then

            Dim l As Label = CType(e.Item.FindControl("Label1"), Label)
            Dim gv As GridView = CType(e.Item.FindControl("GridView1"), GridView)
            Dim tb As TextBox = CType(e.Item.FindControl("TextBox1"), TextBox)


            Dim lbl As Label = DirectCast(e.Item.FindControl("Label1"), Label)
            Dim str As String = lbl.Text





            Dim i As Int32
            i = Convert.ToInt32("" & str & "")

            Dim connstr As String = "Data Source=PC157779;Initial Catalog=practise;User Id=sa;Password=password-1;"
            Dim commstr As String = "select * from table2 where PAB_ASMBLY_TYPE_ID=" & i & ""
            Dim da As SqlDataAdapter = New SqlDataAdapter(commstr, connstr)
            Dim dt1 As DataTable = New DataTable
            da.Fill(dt1)

            gv.DataSource = dt1
            gv.DataBind()

            Session("strTemp") = dt1

            Exit Sub




        End If
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

        '   Dim pnl As Panel = CType(Me.ListView1.FindControl("pnl1"), Panel)
        Dim GridView1 As GridView = CType(sender, GridView)

        dt = TryCast(Session("strTemp"), DataTable)
        Dim rowID As Int32 = Integer.Parse(e.CommandArgument.ToString())
        Dim rowIndex As Int32 = 0

        Dim rowToBeMoved As DataRow = dt.NewRow()
        'create a copy of the row to be moved
        For i As Int16 = 0 To dt.Rows.Count - 1
            If dt.Rows(i)("QUE_ITEM_ID").Equals(rowID) Then
                rowIndex = i
                rowToBeMoved("QUE_ID") = dt.Rows(i)("QUE_ID")
                rowToBeMoved("QUE_ITEM_ID") = dt.Rows(i)("QUE_ITEM_ID")
                rowToBeMoved("PAB_ASMBLY_TYPE_ID") = dt.Rows(i)("PAB_ASMBLY_TYPE_ID")
                rowToBeMoved("PV_OBJ_DESC_TXT") = dt.Rows(i)("PV_OBJ_DESC_TXT")
                Exit For
            End If
        Next

        Select Case e.CommandName
            Case "MoveUp"
                If rowIndex > 0 Then
                    'delete the selected row
                    dt.Rows(rowIndex).Delete()
                    dt.AcceptChanges()
                    'add the rowToBeMoved
                    dt.Rows.InsertAt(rowToBeMoved, rowIndex - 1)
                    dt.AcceptChanges()
                    GridView1.SelectedIndex = rowIndex - 1
                    BindGridViewView()
                End If
                Exit Select
            Case "MoveDown"
                If rowIndex < dt.Rows.Count - 1 Then
                    dt.Rows(rowIndex).Delete()
                    dt.AcceptChanges()
                    dt.Rows.InsertAt(rowToBeMoved, rowIndex + 1)
                    dt.AcceptChanges()
                    GridView1.SelectedIndex = rowIndex + 1
                    BindGridViewView()

                End If
                Exit Select
        End Select

        Session("strTemp") = dt
    End Sub

    Private Sub BindGridViewView()


        Dim gv As GridView = CType(ListView2.FindControl(GridView1), GridView)

        If Session("strTemp") IsNot Nothing Then
            gv.DataSource = TryCast(Session("strTemp"), DataTable)
            gv.DataBind()

        End If

    End Sub


End Class





Reordering Dynamically generated Gridview rows which is inside a listview row
===

Hie

   I have gridviews inside each single row of a listview which are being generated dynamically. I need to reorder the rows(move the rows up and down) inside the dynamically generated gridviews, please help me. I can do it either by using java script or using up and down buttons but how to get the id of a dynamically generated gridview ?? i dont get it... 

I m posting my code for clear reference... 

        <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </cc1:ToolkitScriptManager>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:practiseConnectionString %>"
            SelectCommand="SELECT [PAB_ASMBLY_TYPE_ID] FROM [table1]"></asp:SqlDataSource>
        <br />
        <br />
        <asp:ListView ID="ListView2" runat="server" DataKeyNames="PAB_ASMBLY_TYPE_ID" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <tr style="background-color: #E0FFFF; color: #333333;">
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' Visible="false" />
                        <asp:LinkButton CausesValidation="false" ID="LinkButton2" runat="server">+</asp:LinkButton>
                        <asp:LinkButton CausesValidation="false" ID="LinkButton3" runat="server">-</asp:LinkButton>
                        <asp:Label ID="Label3" runat="server" Text="panel"></asp:Label>
                        <asp:Panel ID="Panel1" runat="server">
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:GridView ID="GridView1" DataKeyNames="QUE_ITEM_ID" runat="Server" AutoGenerateColumns="False"
                                        OnRowCommand="GridView1_RowCommand">
                                        <Columns>
                                            <asp:BoundField DataField="QUE_ID" HeaderText="QUE_ID" />
                                            <asp:BoundField AccessibleHeaderText="QUE_ITEM_ID" DataField="QUE_ITEM_ID" HeaderText="QUE_ITEM_ID" />
                                            <asp:BoundField AccessibleHeaderText="PAB_ASMBLY_TYPE_ID" DataField="PAB_ASMBLY_TYPE_ID"
                                                HeaderText="PAB_ASMBLY_TYPE_ID" />
                                            <asp:BoundField AccessibleHeaderText="PV_OBJ_DESC_TXT" DataField="PV_OBJ_DESC_TXT"
                                                HeaderText="PV_OBJ_DESC_TXT" />
                                            <asp:TemplateField HeaderText="Move">
                                                <ItemTemplate>
                                                    <asp:ImageButton ID="ImgBtnUp" runat="server" ToolTip="Move Up" ImageUrl="~/Images/up.png"
                                                        CommandName="MoveUp" CommandArgument='<%# Eval("QUE_ITEM_ID") %>' />
                                                    <asp:ImageButton ID="ImgBtnDown" runat="server" ToolTip="Move Down" ImageUrl="~/Images/down.png"
                                                        CommandName="MoveDown" CommandArgument='<%# Eval("QUE_ITEM_ID") %>' />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </asp:Panel>
                        <cc1:CollapsiblePanelExtender ExpandControlID="LinkButton2" CollapseControlID="LinkButton3"
                            ID="CollapsiblePanelExtender1" TargetControlID="Panel1" runat="server" TextLabelID="Label3"
                            ExpandedText="collapse" CollapsedText="expand" SuppressPostBack="True" Collapsed="True">
                        </cc1:CollapsiblePanelExtender>
                    </td>
                </tr>
            </ItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse;
                    border-color: #999999; border-style: none; border-width: 1px;">
                    <tr>
                        <td>
                            No data was returned.
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                    </td>
                    <td>
                        <asp:TextBox ID="PAB_ASMBLY_TYPE_IDTextBox" runat="server" Text='<%# Bind("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;
                                border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;
                                font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr runat="server" style="background-color: #E0FFFF; color: #333333;">
                                    <th runat="server">
                                        PAB_ASMBLY_TYPE_ID
                                    </th>
                                </tr>
                                <tr id="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif;
                            color: #FFFFFF">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <EditItemTemplate>
                <tr style="background-color: #999999;">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                    </td>
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel1" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <SelectedItemTemplate>
                <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;">
                    <td>
                        <asp:Label ID="PAB_ASMBLY_TYPE_IDLabel" runat="server" Text='<%# Eval("PAB_ASMBLY_TYPE_ID") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>

  
and here is my code behind...


Imports System.Data.SqlClient
Partial Public Class WebForm3
    Inherits System.Web.UI.Page
    Shared dt As DataTable



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BindGridViewView()
        End If
    End Sub

    Private Sub ListView2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView2.ItemDataBound
        If (e.Item.ItemType = ListViewItemType.DataItem) Then

            Dim l As Label = CType(e.Item.FindControl("Label1"), Label)
            Dim gv As GridView = CType(e.Item.FindControl("GridView1"), GridView)
            Dim tb As TextBox = CType(e.Item.FindControl("TextBox1"), TextBox)


            Dim lbl As Label = DirectCast(e.Item.FindControl("Label1"), Label)
            Dim str As String = lbl.Text





            Dim i As Int32
            i = Convert.ToInt32("" & str & "")

            Dim connstr As String = "Data Source=PC157779;Initial Catalog=practise;User Id=sa;Password=password-1;"
            Dim commstr As String = "select * from table2 where PAB_ASMBLY_TYPE_ID=" & i & ""
            Dim da As SqlDataAdapter = New SqlDataAdapter(commstr, connstr)
            Dim dt1 As DataTable = New DataTable
            da.Fill(dt1)

            gv.DataSource = dt1
            gv.DataBind()

            Session("strTemp") = dt1

            Exit Sub




        End If
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

        '   Dim pnl As Panel = CType(Me.ListView1.FindControl("pnl1"), Panel)
        Dim GridView1 As GridView = CType(sender, GridView)

        dt = TryCast(Session("strTemp"), DataTable)
        Dim rowID As Int32 = Integer.Parse(e.CommandArgument.ToString())
        Dim rowIndex As Int32 = 0

        Dim rowToBeMoved As DataRow = dt.NewRow()
        'create a copy of the row to be moved
        For i As Int16 = 0 To dt.Rows.Count - 1
            If dt.Rows(i)("QUE_ITEM_ID").Equals(rowID) Then
                rowIndex = i
                rowToBeMoved("QUE_ID") = dt.Rows(i)("QUE_ID")
                rowToBeMoved("QUE_ITEM_ID") = dt.Rows(i)("QUE_ITEM_ID")
                rowToBeMoved("PAB_ASMBLY_TYPE_ID") = dt.Rows(i)("PAB_ASMBLY_TYPE_ID")
                rowToBeMoved("PV_OBJ_DESC_TXT") = dt.Rows(i)("PV_OBJ_DESC_TXT")
                Exit For
            End If
        Next

        Select Case e.CommandName
            Case "MoveUp"
                If rowIndex > 0 Then
                    'delete the selected row
                    dt.Rows(rowIndex).Delete()
                    dt.AcceptChanges()
                    'add the rowToBeMoved
                    dt.Rows.InsertAt(rowToBeMoved, rowIndex - 1)
                    dt.AcceptChanges()
                    GridView1.SelectedIndex = rowIndex - 1
                    BindGridViewView()
                End If
                Exit Select
            Case "MoveDown"
                If rowIndex < dt.Rows.Count - 1 Then
                    dt.Rows(rowIndex).Delete()
                    dt.AcceptChanges()
                    dt.Rows.InsertAt(rowToBeMoved, rowIndex + 1)
                    dt.AcceptChanges()
                    GridView1.SelectedIndex = rowIndex + 1
                    BindGridViewView()

                End If
                Exit Select
        End Select

        Session("strTemp") = dt
    End Sub

    Private Sub BindGridViewView()


        Dim gv As GridView = CType(ListView2.FindControl(GridView1), GridView)

        If Session("strTemp") IsNot Nothing Then
            gv.DataSource = TryCast(Session("strTemp"), DataTable)
            gv.DataBind()

        End If

    End Sub


End Class




