Update gridview before sending data to Excel

Hi Experts,

on the screen In a grid view all the parts are displayed. I have a button. When I click it I have to generate a purchase order and send it to Excel.  These are the steps I am doing.

1. select the parts on the gridview you want to send.
2.create a data table to move all the selected parts.
3.Generate a purchase order number.
4.Update the gridview  with purchase order number if it is selected for the purchase order.
5.Send the contents of the created data table to excel.

Before I send data to Excel I need to update the gridview. I am able to send data to excel. But unable to update the gridview before I send data.

I hope I explained the properly. Please help!!

This is my code.

 Protected Sub cmdGeneratePO_Click(sender As Object, e As EventArgs) Handles cmdGeneratePO.Click
            Dim JobNo As String = ddlJob.SelectedItem.Value
            Dim PONumber As String = Generate_PO_Number()
            Dim dt As New DataTable()
            dt = Make_DataTableFor_PO_RFQ()
            If dt.Rows.Count = 0 Then
                MessageBox("Please select the parts", lblMessage)
                Exit Sub
            End If

            Dim totBOMCost As Double
            lblTotalCost.Text = ""
            If dt.Rows.Count > 0 Then
                For Each row As DataRow In dt.Rows
                    Dim cost As Double = row("TotalCost")
                    totBOMCost = totBOMCost + cost
            End If

            Dim Cust_Name As String = ddlCustomer.SelectedItem.Text
            Dim Bom_Name As String = ddlBOM.SelectedItem.Text
            ''Update the PO number in the DB
            Update_PONum_BOM(dt, PONumber)
            'Fill the grid
            Dim Sql As String = " SELECT Parts.partID, Parts.ManufacturerPartNo as ManuPartNo,                            Parts.LastOrderedCost,Parts.PartDescription as Description, " & _
                            " MANUFACTURER.ManuName as Manufacturer, bom.BomMstID, bom.QtyOrdered, bom.QtyReceived, bom.QtyRequired as Quantity,Parts.LastOrderedCost*bom.QtyRequired as Cost, bom.ExpectedDelDt, " & _
                            " bom.WearSparePartFlag,BOM.PONum  FROM BOM, PARTS, Manufacturer where parts.PartID = bom.PartID and parts.ManuID = Manufacturer.ManuID and " & _
                            " BOM.CustomerID=" & ddlCustomer.SelectedItem.Value & " and JobNum = '" & ddlJob.SelectedItem.Value & "' and BomID = " & ddlBOM.SelectedItem.Value & " and RevisionLvl='" & ddlRevLvl.SelectedItem.Value & "' order by ManuPartNo"
            Dim rowCount As Integer = Fill_SelectedParts_Grid(Sql)
           Export_PO_ToExcel(dt, Cust_Name, Bom_Name, JobNo, totBOMCost, PONumber)

        Catch ex As Exception
            MessageBox("Error while generating PO :" & ex.Message, lblMessage)
        End Try
    End Sub

 "Fill_SelectedParts_Grid" should update the grid. Infact it is updating if I remove "Export_PO_ToExcel(dt, Cust_Name, Bom_Name, JobNo, totBOMCost, PONumber)" code.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JHMH IT StaffCommented:
Why not just draw a new gridview with the updated info based on the datatable you created when selecting the parts?
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. Actually I do not need another gridview. Because the problem is,  the gridview do not update if there is a Excel code after that. But I found a way to do it. I have created a hidden button and call its click event from client side after updating the gridview.

My code is:-
create a hidden button :
<asp:Button ID="cmdExport" runat="server" ClientIDMode="Static" style="display:none;" OnClick="cmdExport_Click" /><!--Hidden button  -->

<script type="text/javascript">
        function ExportExcel() { //function to be executed from server side after gridview is bound

Protected Sub cmdExport_Click(sender As Object, e As EventArgs) Handles cmdExport.Click
        'cmdExport is a hidden button used just to send the data to Excel file. This click event is called from the    client side code

             Export_PO_ToExcel(dtPOExcel, Cust_Name, Bom_Name, JobNo, totBOMCost, gPONumber)
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you everyone for your help.  The resolution I have already described.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.