Dynamically add Rows in ASP.Net GridView inside Repeater Control using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

Hello,

I am using Gridview inside a repeater control to select items for billing. 

but when I try to add new row it says: "Object reference not set to an instance of an object."

How to add new row to gridview inside repeater control

Here is my c# code for the same 

    private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        GridView gvItems = (GridView)rptrTables.FindControl("grdOrder");
        //GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[1].FindControl("drpItemName");
                    TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[2].FindControl("lblUnitPrice");
                    TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[3].FindControl("lblQuantity");
                    TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[4].FindControl("lblLineTotal");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["ItemName"] = box1.SelectedItem.ToString();
                    dtCurrentTable.Rows[i - 1]["UnitPrice"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Quantity"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["LineTotal"] = box4.Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;
                gvItems.DataSource = dtCurrentTable;
                gvItems.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }

 

 

<div class="card-body">
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <asp:gridview id="grdOrder" runat="server" autogeneratecolumns="false" allowpaging="true"
                    cssclass="table table-bordered table-hover table-responsive" gridlines="None"
                    pagesize="10" onrowdatabound="gvRowDataBound" showfooter="true">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />
                        <asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />
                        <asp:TemplateField HeaderText="Item Name">
                            <ItemTemplate>
                                <asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Price">
                            <ItemTemplate>
                                <asp:TextBox ID="lblUnitPrice" runat="server" CssClass="form-control" Text='<%# Eval("UnitPrice")%>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Qty">
                            <ItemTemplate>
                                <asp:TextBox ID="lblQuantity" runat="server" TextMode="Number" CssClass="form-control" Text='<%# Eval("Quantity")%>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>                                                
                        <asp:TemplateField HeaderText="Line Total">
                            <ItemTemplate>
                                <asp:TextBox ID="lblLineTotal" runat="server" CssClass="form-control" Text="0"></asp:TextBox>
                            </ItemTemplate>
                            <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" />
                        </FooterTemplate>                                                    
                        </asp:TemplateField>                                         
                    </Columns>
                </asp:gridview>
            </div>
        </div>
    </div>
</div>
<div class="card-footer">
    <div class="pull-right">
        <asp:button id="btnSubmit" runat="server" causesvalidation="false" tabindex="2" cssclass="btn btn-primary"
            text="Print KOT" />
        <asp:button id="Button1" runat="server" causesvalidation="false" tabindex="2" cssclass="btn btn-primary"
            text="Print Final Bill" />
        <asp:button id="Button2" runat="server" causesvalidation="false" tabindex="2" cssclass="btn btn-primary"
            text="Complete Order" />
    </div>
</div>

 

Posted 5 months ago Modified on 5 months ago

Hi jmprateek,

Refer below sample.

HTML

<asp:Repeater ID="rptrTables" runat="server" OnItemDataBound="rptrTables_ItemDataBound">
    <ItemTemplate>
        <div class="col-md-6">
            <div class="card card-info">
                <div class="card-header with-border">
                    <h3 class="card-title">
                        <asp:Label ID="lblTableNumber" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label></h3>
                    <div class="card-tools">
                        <button type="button" class="btn btn-tool" data-widget="collapse">
                            <i class="fa fa-minus"></i>
                        </button>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true"
                                    CssClass="table table-bordered table-hover table-responsive" GridLines="None"
                                    PageSize="10" ShowFooter="true" OnRowDataBound="gvRowDataBound">
                                    <Columns>
                                        <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash"
                                            DeleteText="" HeaderText="Remove" />
                                        <asp:TemplateField HeaderText="Column1" ItemStyle-Wrap="false" HeaderStyle-Wrap="false">
                                            <ItemTemplate>
                                                <asp:DropDownList ID="drpItemname" runat="server" Width="150px" CssClass="form-control select2">
                                                </asp:DropDownList>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Price">
                                            <ItemTemplate>
                                                <asp:TextBox ID="lblUnitPrice" runat="server" CssClass="form-control" Text='<%# Eval("Column2")%>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Qty">
                                            <ItemTemplate>
                                                <asp:TextBox ID="lblQuantity" runat="server" CssClass="form-control" Text='<%# Eval("Column3")%>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Line Total">
                                            <ItemTemplate>
                                                <asp:TextBox ID="lblLineTotal" runat="server" CssClass="form-control" Text='<%# Eval("Column4")%>'></asp:TextBox>
                                            </ItemTemplate>
                                            <FooterStyle HorizontalAlign="Right" />
                                            <FooterTemplate>
                                                <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" />
                                            </FooterTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="card-footer">
                    <div class="pull-right">
                        <asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary"
                            Text="Print KOT" />
                        <asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary"
                            Text="Print Final Bill" />
                        <asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary"
                            Text="Complete Order" />
                    </div>
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

[Serializable]
public class RptDetails
{
    public string CustomerId { get; set; }
    public DataTable DtDetails { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("CustomerID", typeof(string)));
        dt.Rows.Add(1);
        dt.Rows.Add(2);
        rptrTables.DataSource = dt;
        rptrTables.DataBind();
    }
}

private ArrayList GetDummyData()
{
    ArrayList arr = new ArrayList();
    arr.Add(new ListItem("Item1", "1"));
    arr.Add(new ListItem("Item2", "2"));
    arr.Add(new ListItem("Item3", "3"));
    arr.Add(new ListItem("Item4", "4"));
    arr.Add(new ListItem("Item5", "5"));
    return arr;
}

private void FillDropDownList(DropDownList ddl)
{
    ArrayList arr = GetDummyData();
    foreach (ListItem item in arr)
    {
        ddl.Items.Add(item);
    }
}

protected void rptrTables_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        GridView gvOrders = e.Item.FindControl("grdOrder") as GridView;
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Rows.Add();
        List<RptDetails> rpt = new List<RptDetails>();
        rpt.Add(new RptDetails { DtDetails = dt });
        ViewState["RptDetails"] = rpt;
        gvOrders.DataSource = rpt[0].DtDetails;
        gvOrders.DataBind();
    }
}

protected void gvRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl1 = (DropDownList)e.Row.Cells[1].FindControl("drpItemname");
        FillDropDownList(ddl1);
    }
}

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["RptDetails"] != null)
    {
        List<RptDetails> rpt1 = (List<RptDetails>)ViewState["RptDetails"];
        DataTable dt = rpt1[0].DtDetails;
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < rptrTables.Items.Count; j++)
                {
                    string id = (rptrTables.Items[j].FindControl("lblTableNumber") as Label).Text;
                    if (id == rpt1[0].CustomerId)
                    {
                        GridView gvOrders = rptrTables.Items[j].FindControl("grdOrder") as GridView;
                        DropDownList ddl1 = (DropDownList)gvOrders.Rows[rowIndex].Cells[1].FindControl("drpItemname");
                        TextBox txt1 = (TextBox)gvOrders.Rows[rowIndex].Cells[2].FindControl("lblUnitPrice");
                        TextBox txt2 = (TextBox)gvOrders.Rows[rowIndex].Cells[3].FindControl("lblQuantity");
                        TextBox txt3 = (TextBox)gvOrders.Rows[rowIndex].Cells[4].FindControl("lblLineTotal");
                        if (i < dt.Rows.Count - 1)
                        {
                            ddl1.ClearSelection();
                            if (ddl1.Items.FindByText(dt.Rows[i]["Column1"].ToString()) != null)
                            {
                                ddl1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;
                            }
                        }
                    }
                }
                rowIndex++;
            }
        }
    }
}

protected void AddItem(object sender, EventArgs e)
{
    Button btn = (sender) as Button;
    GridView gvOrders = (GridView)(btn.NamingContainer as GridViewRow).NamingContainer;
    string id = ((gvOrders.NamingContainer as RepeaterItem).FindControl("lblTableNumber") as Label).Text;
    List<RptDetails> rpt = new List<RptDetails>();

    if (ViewState["RptDetails"] != null)
    {
        List<RptDetails> rpt1 = (List<RptDetails>)ViewState["RptDetails"];
        var q = rpt1.Find(x => x.CustomerId == id);
        if (q == null)
        {
            DataTable dtCurrentTable = rpt1[0].DtDetails.Clone();
            DataRow drCurrentRow = dtCurrentTable.NewRow();
            dtCurrentTable.Rows.Add(drCurrentRow);
            for (int j = 0; j < rptrTables.Items.Count - 1; j++)
            {
                for (int i = 0; i < gvOrders.Rows.Count; i++)
                {
                    DropDownList ddl1 = (DropDownList)gvOrders.Rows[i].Cells[1].FindControl("drpItemname");
                    TextBox text1 = (TextBox)gvOrders.Rows[i].Cells[2].FindControl("lblUnitPrice");
                    TextBox text2 = (TextBox)gvOrders.Rows[i].Cells[3].FindControl("lblQuantity");
                    TextBox text3 = (TextBox)gvOrders.Rows[i].Cells[4].FindControl("lblLineTotal");

                    dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
                    dtCurrentTable.Rows[i]["Column2"] = text1.Text;
                    dtCurrentTable.Rows[i]["Column3"] = text2.Text;
                    dtCurrentTable.Rows[i]["Column4"] = text3.Text;
                }
                rpt.Add(new RptDetails { CustomerId = id, DtDetails = dtCurrentTable });
                dtCurrentTable.Rows.Add();
            }
        }
        else
        {
            DataTable dt = q.DtDetails;
            for (int j = 0; j < rptrTables.Items.Count - 1; j++)
            {
                for (int i = 0; i < gvOrders.Rows.Count; i++)
                {
                    DataRow drCurrentRow = dt.NewRow();
                    DropDownList ddl1 = (DropDownList)gvOrders.Rows[i].Cells[1].FindControl("drpItemname");
                    TextBox text1 = (TextBox)gvOrders.Rows[i].Cells[2].FindControl("lblUnitPrice");
                    TextBox text2 = (TextBox)gvOrders.Rows[i].Cells[3].FindControl("lblQuantity");
                    TextBox text3 = (TextBox)gvOrders.Rows[i].Cells[4].FindControl("lblLineTotal");

                    dt.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
                    dt.Rows[i]["Column2"] = text1.Text;
                    dt.Rows[i]["Column3"] = text2.Text;
                    dt.Rows[i]["Column4"] = text3.Text;
                }
                rpt.Add(new RptDetails { CustomerId = id, DtDetails = dt });
                dt.Rows.Add();
            }
        }
        ViewState["RptDetails"] = rpt;
        gvOrders.DataSource = rpt[0].DtDetails;
        gvOrders.DataBind();
    }

    else
    {
        Response.Write("ViewState is null");
    }

    SetPreviousData();
}

protected void Save(object sender, EventArgs e)
{
    string message = "";
    foreach (RepeaterItem item in rptrTables.Items)
    {
        GridView gvProduct = (GridView)item.FindControl("grdOrder");
        for (int i = 0; i < gvProduct.Rows.Count - 1; i++)
        {
            DropDownList ddlItem = gvProduct.Rows[i].FindControl("drpItemname") as DropDownList;
            TextBox txtUnitPrice = gvProduct.Rows[i].FindControl("lblUnitPrice") as TextBox;
            TextBox txtQuantity = gvProduct.Rows[i].FindControl("lblQuantity") as TextBox;
            TextBox txtLineTotal = gvProduct.Rows[i].FindControl("lblLineTotal") as TextBox;
            message += ddlItem.SelectedItem.Text + " " + txtUnitPrice.Text + " " + txtQuantity.Text + " " + txtLineTotal.Text + "\\n";
        }
    }
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + message + "');", true);
}

VB.Net

<Serializable()>
Public Class RptDetails
    Public Property CustomerId As String
    Public Property DtDetails As DataTable
End Class

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add(New DataColumn("CustomerID", GetType(String)))
        dt.Rows.Add(1)
        dt.Rows.Add(2)
        rptrTables.DataSource = dt
        rptrTables.DataBind()
    End If
End Sub

Private Function GetDummyData() As ArrayList
    Dim arr As ArrayList = New ArrayList()
    arr.Add(New ListItem("Item1", "1"))
    arr.Add(New ListItem("Item2", "2"))
    arr.Add(New ListItem("Item3", "3"))
    arr.Add(New ListItem("Item4", "4"))
    arr.Add(New ListItem("Item5", "5"))
    Return arr
End Function

Private Sub FillDropDownList(ByVal ddl As DropDownList)
    Dim arr As ArrayList = GetDummyData()

    For Each item As ListItem In arr
        ddl.Items.Add(item)
    Next
End Sub

Protected Sub rptrTables_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim gvOrders As GridView = TryCast(e.Item.FindControl("grdOrder"), GridView)
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add(New DataColumn("Column1", GetType(String)))
        dt.Columns.Add(New DataColumn("Column2", GetType(String)))
        dt.Columns.Add(New DataColumn("Column3", GetType(String)))
        dt.Columns.Add(New DataColumn("Column4", GetType(String)))
        dt.Rows.Add()
        Dim rpt As List(Of RptDetails) = New List(Of RptDetails)()
        rpt.Add(New RptDetails With {
            .DtDetails = dt
        })
        ViewState("RptDetails") = rpt
        gvOrders.DataSource = rpt(0).DtDetails
        gvOrders.DataBind()
    End If
End Sub

Protected Sub gvRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ddl1 As DropDownList = CType(e.Row.Cells(1).FindControl("drpItemname"), DropDownList)
        FillDropDownList(ddl1)
    End If
End Sub

Private Sub SetPreviousData()
    Dim rowIndex As Integer = 0
    If ViewState("RptDetails") IsNot Nothing Then
        Dim rpt1 As List(Of RptDetails) = CType(ViewState("RptDetails"), List(Of RptDetails))
        Dim dt As DataTable = rpt1(0).DtDetails
        If dt.Rows.Count > 0 Then

            For i As Integer = 0 To dt.Rows.Count - 1
                For j As Integer = 0 To rptrTables.Items.Count - 1
                    Dim id As String = (TryCast(rptrTables.Items(j).FindControl("lblTableNumber"), Label)).Text
                    If id = rpt1(0).CustomerId Then
                        Dim gvOrders As GridView = TryCast(rptrTables.Items(j).FindControl("grdOrder"), GridView)
                        Dim ddl1 As DropDownList = CType(gvOrders.Rows(rowIndex).Cells(1).FindControl("drpItemname"), DropDownList)
                        Dim txt1 As TextBox = CType(gvOrders.Rows(rowIndex).Cells(2).FindControl("lblUnitPrice"), TextBox)
                        Dim txt2 As TextBox = CType(gvOrders.Rows(rowIndex).Cells(3).FindControl("lblQuantity"), TextBox)
                        Dim txt3 As TextBox = CType(gvOrders.Rows(rowIndex).Cells(4).FindControl("lblLineTotal"), TextBox)

                        If i < dt.Rows.Count - 1 Then
                            ddl1.ClearSelection()

                            If ddl1.Items.FindByText(dt.Rows(i)("Column1").ToString()) IsNot Nothing Then
                                ddl1.Items.FindByText(dt.Rows(i)("Column1").ToString()).Selected = True
                            End If
                        End If
                    End If
                Next

                rowIndex += 1
            Next
        End If
    End If
End Sub

Protected Sub AddItem(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = TryCast((sender), Button)
    Dim gvOrders As GridView = CType((TryCast(btn.NamingContainer, GridViewRow)).NamingContainer, GridView)
    Dim id As String = (TryCast((TryCast(gvOrders.NamingContainer, RepeaterItem)).FindControl("lblTableNumber"), Label)).Text
    Dim rpt As List(Of RptDetails) = New List(Of RptDetails)()

    If ViewState("RptDetails") IsNot Nothing Then
        Dim rpt1 As List(Of RptDetails) = CType(ViewState("RptDetails"), List(Of RptDetails))
        Dim q = rpt1.Find(Function(x) x.CustomerId = id)

        If q Is Nothing Then
            Dim dtCurrentTable As DataTable = rpt1(0).DtDetails.Clone()
            Dim drCurrentRow As DataRow = dtCurrentTable.NewRow()
            dtCurrentTable.Rows.Add(drCurrentRow)
            For j As Integer = 0 To rptrTables.Items.Count - 1 - 1
                For i As Integer = 0 To gvOrders.Rows.Count - 1
                    Dim ddl1 As DropDownList = CType(gvOrders.Rows(i).Cells(1).FindControl("drpItemname"), DropDownList)
                    Dim text1 As TextBox = CType(gvOrders.Rows(i).Cells(2).FindControl("lblUnitPrice"), TextBox)
                    Dim text2 As TextBox = CType(gvOrders.Rows(i).Cells(3).FindControl("lblQuantity"), TextBox)
                    Dim text3 As TextBox = CType(gvOrders.Rows(i).Cells(4).FindControl("lblLineTotal"), TextBox)
                    dtCurrentTable.Rows(i)("Column1") = ddl1.SelectedItem.Text
                    dtCurrentTable.Rows(i)("Column2") = text1.Text
                    dtCurrentTable.Rows(i)("Column3") = text2.Text
                    dtCurrentTable.Rows(i)("Column4") = text3.Text
                Next

                rpt.Add(New RptDetails With {
                    .CustomerId = id,
                    .DtDetails = dtCurrentTable
                })
                dtCurrentTable.Rows.Add()
            Next
        Else
            Dim dt As DataTable = q.DtDetails
            For j As Integer = 0 To rptrTables.Items.Count - 1 - 1
                For i As Integer = 0 To gvOrders.Rows.Count - 1
                    Dim drCurrentRow As DataRow = dt.NewRow()
                    Dim ddl1 As DropDownList = CType(gvOrders.Rows(i).Cells(1).FindControl("drpItemname"), DropDownList)
                    Dim text1 As TextBox = CType(gvOrders.Rows(i).Cells(2).FindControl("lblUnitPrice"), TextBox)
                    Dim text2 As TextBox = CType(gvOrders.Rows(i).Cells(3).FindControl("lblQuantity"), TextBox)
                    Dim text3 As TextBox = CType(gvOrders.Rows(i).Cells(4).FindControl("lblLineTotal"), TextBox)
                    dt.Rows(i)("Column1") = ddl1.SelectedItem.Text
                    dt.Rows(i)("Column2") = text1.Text
                    dt.Rows(i)("Column3") = text2.Text
                    dt.Rows(i)("Column4") = text3.Text
                Next
                rpt.Add(New RptDetails With {
                    .CustomerId = id,
                    .DtDetails = dt
                })
                dt.Rows.Add()
            Next
        End If
        ViewState("RptDetails") = rpt
        gvOrders.DataSource = rpt(0).DtDetails
        gvOrders.DataBind()
    Else
        Response.Write("ViewState is null")
    End If
    SetPreviousData()
End Sub

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim message As String = ""
    For Each item As RepeaterItem In rptrTables.Items
        Dim gvProduct As GridView = CType(item.FindControl("grdOrder"), GridView)
        For i As Integer = 0 To gvProduct.Rows.Count - 1 - 1
            Dim ddlItem As DropDownList = TryCast(gvProduct.Rows(i).FindControl("drpItemname"), DropDownList)
            Dim txtUnitPrice As TextBox = TryCast(gvProduct.Rows(i).FindControl("lblUnitPrice"), TextBox)
            Dim txtQuantity As TextBox = TryCast(gvProduct.Rows(i).FindControl("lblQuantity"), TextBox)
            Dim txtLineTotal As TextBox = TryCast(gvProduct.Rows(i).FindControl("lblLineTotal"), TextBox)
            message += ddlItem.SelectedItem.Text & " " + txtUnitPrice.Text & " " + txtQuantity.Text & " " + txtLineTotal.Text & "\n"
        Next
    Next
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Message", "alert('" & message & "');", True)
End Sub

Screenshot