Solved

Append data to DataGridView instead of overwriting it in Vb.net

Posted on 2014-10-30
8
744 Views
Last Modified: 2014-11-05
Hi Experts,
 
I have two datagridviews. I select multiple rows from one datagridview and move it to another Datagridview. And I have to do it multiple times. First time it works fine. But when I do it next time the previous data is overwritten. I want the Previous rows as well as the new rows in the second datagridview.
I am using the code below to bind the datagridview.

            connection.Open()
            cmd = New SqlCommand(finalSql, connection)
            da.SelectCommand = cmd
            da.Fill(dt)
            bsource.DataSource = dt
            dgvRegister.DataSource = bsource

Thank you in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:ReneD100
ID: 40413641
How do you add them to the 2nd dgv? To the bound datatable?
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40413690
yes. I am using the following code.

For Each dr As DataGridViewRow In Me.dgvAdd.SelectedRows
                If Me.dgvAdd.SelectedRows.Count = 1 Then
                    '*** When only one row is selected in the first datagrid
                    Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cells(4).Value
                    finalSql = finalSql + " " + sqlPaletNo + " '" + StockPaletNo + "'"

                Else
                    Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cells(4).Value

                    If finalSql <> "" Then
                        finalSql = finalSql + " or STK_PLT_NO =" + " '" + StockPaletNo + "'"
                    Else
                        finalSql = finalSql + " " + sqlPaletNo + " '" + StockPaletNo + "'"
                    End If
                End If

            Next

            finalSql = SQL + finalSql + sqlOrderBy
            connection.Open()
            cmd = New SqlCommand(finalSql, connection)
            da.SelectCommand = cmd
            da.Fill(dt)
            bsource.DataSource = dt
            dgvRegister.DataSource = bsource
0
 
LVL 5

Expert Comment

by:ReneD100
ID: 40413849
Took me a second to read the code, but I think I get what you're trying to do. SQL is the 'base' query that retrieves data, then you include more rows by adding finalsql (which has a series of ORs). Then you retrieve that and bind that to the datagrid.
finalsql is defined outside this function.
Not the easiest way I think, but that is also not really your question. Assuming I understood your code correctly, are you able to store any data in the dgv the second time? Because the second time I don't think this going to run correctly:
finalSql = SQL + finalSql + sqlOrderBy

Open in new window

put a messagebox or debug.print to show the finalsql and look at the contents.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40413931
Thank you  for ur reply. This is the final query.

"SELECT * FROM PNLSTKMST WHERE STK_PART_NO = '663113S000         ' AND CONVERT(INT,STK_QTY) != 0 AND STK_PRO_STS = '0' AND STK_BAD_CLS = '00' AND   STK_PLT_NO =  '1014' or STK_PLT_NO = '1030' ORDER BY STK_DATE,STK_TIME,STK_ROW,STK_BAY,STK_LEV"

Actually there is no problem with the query. I am getting all the records I need to. But how can I bind the DGV so that it should not overwrite the previous data.

Thanks.
0
 
LVL 5

Expert Comment

by:ReneD100
ID: 40413976
If you don't want to store the data in a database table you could use a datatable for the intermediate results. You use a second datatable for the second dgv (if the structure is the same you can use the datatable.clone method-  this copies the structure but not the data). Then you can import the datarows from the datatable bound to the first dgv to the second datatable: datatable2.import(row) - you need to look at the exact syntax. That way you can keep on adding to datatable2.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40414931
Thank you for your reply. Can you give some code please?
0
 

Accepted Solution

by:
RadhaKrishnaKiJaya earned 0 total points
ID: 40416340
Thank you for your time to help me. But I found the solution. Instead of binding the second datagrid I am moving the selected row from one DGV to another.

For i = 0 To dgvAdd.SelectedRows.Count - 1
                Dim index As Integer = dgvRegister.Rows.Add
                dgvRegister.Rows(index).Cells(0).Value = dgvAdd.SelectedRows(i).Cells(0).Value.ToString
next i
0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 40423518
Because I found the answer by myself.

Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question