Click here to Skip to main content
15,996,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai i am using Gridview in the gride view i am Bounding values 3 Columns , one column is the text box.and i have one check box if the Check box is checked , I want to copy all the Values into another grid view below is my code , but hear is only Bounded columns value are copy new grid view the text box columns is not copy , Please Help .
XML
<asp:GridView ID="GRVItem" DataKeyNames="ItemCode,ItemName" runat="server" Font-Names="Calibri"
                        Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" AllowPaging="True" PageSize="5"
                        ShowHeader="true" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
                        Width="1000px" padding-top=" 70px;" AutoGenerateColumns="false" >
                        <HeaderStyle BorderColor="Gray" BorderStyle="Solid" Font-Bold="true" BackColor="#76C3FD" />
                        <Columns>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckChanged" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox_CheckChanged" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField HeaderText="Item Code" DataField="ItemCode"  ReadOnly="true"/>
                            <asp:BoundField HeaderText="Item Name" DataField="ItemName" ReadOnly="true" />
                            <asp:BoundField HeaderText="In Stack" DataField="OnHand" ReadOnly="true" />
                            <asp:TemplateField HeaderText="Quantity">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtQuantity" runat="server" />
                                </EditItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>



Code is

VB
Protected Sub CheckBox_CheckChanged(ByVal sender As Object, ByVal e As EventArgs)
       GetSelectedRows()
       BindSecondGrid()
   End Sub
   Protected Sub BindSecondGrid()
       Dim dt As DataTable = DirectCast(ViewState("GetRecords"), DataTable)
       GridView1.DataSource = dt
       GridView1.DataBind()
   End Sub
   Private Sub GetSelectedRows()
       Dim dt As DataTable
       If ViewState("GetRecords") IsNot Nothing Then
           dt = DirectCast(ViewState("GetRecords"), DataTable)
       Else
           dt = CreateDataTable()
       End If
       For i As Integer = 0 To GRVItem.Rows.Count - 1
           Dim chk As CheckBox = DirectCast(GRVItem.Rows(i).Cells(0).FindControl("chk"), CheckBox)
           If chk.Checked Then
               dt = AddRow(GRVItem.Rows(i), dt)
           Else
               dt = RemoveRow(GRVItem.Rows(i), dt)
           End If
       Next
       ViewState("GetRecords") = dt
   End Sub

   Private Function AddRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
       Dim dr As DataRow() = dt.Select("ItemCode = '" _
                                   & gvRow.Cells(1).Text & "'")
       If dr.Length <= 0 Then
           dt.Rows.Add()
           dt.Rows(dt.Rows.Count - 1)("ItemCode") = gvRow.Cells(1).Text
           dt.Rows(dt.Rows.Count - 1)("ItemName") = gvRow.Cells(2).Text
          
           dt.Rows(dt.Rows.Count - 1)("Quantity") = gvRow.Cells(4).Text
           ' Hear is not copy the Value of Quantity .this is the textbox in gridview
           dt.AcceptChanges()
       End If
       Return dt
   End Function
   Private Function RemoveRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
       Dim dr As DataRow() = dt.Select("ItemCode = '" _
                                   & gvRow.Cells(1).Text & "'")
       If dr.Length > 0 Then
           dt.Rows.Remove(dr(0))
           dt.AcceptChanges()
       End If
       Return dt
   End Function
   Private Function CreateDataTable() As DataTable
       Dim dt As New DataTable()
       dt.Columns.Add("ItemCode")
       dt.Columns.Add("ItemName")
       dt.Columns.Add("Quantity")
       dt.Columns.Add("Price")
       dt.AcceptChanges()
       Return dt
   End Function
Posted
Updated 23-Aug-13 1:02am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900