RadhaKrishnaKiJaya
asked on
Append data to DataGridView instead of overwriting it in Vb.net
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.
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.
How do you add them to the 2nd dgv? To the bound datatable?
ASKER
yes. I am using the following code.
For Each dr As DataGridViewRow In Me.dgvAdd.SelectedRows
If Me.dgvAdd.SelectedRows.Cou nt = 1 Then
'*** When only one row is selected in the first datagrid
Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cell s(4).Value
finalSql = finalSql + " " + sqlPaletNo + " '" + StockPaletNo + "'"
Else
Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cell s(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
For Each dr As DataGridViewRow In Me.dgvAdd.SelectedRows
If Me.dgvAdd.SelectedRows.Cou
'*** When only one row is selected in the first datagrid
Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cell
finalSql = finalSql + " " + sqlPaletNo + " '" + StockPaletNo + "'"
Else
Dim StockPaletNo As String = dgvAdd.Rows(dr.Index).Cell
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
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 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
put a messagebox or debug.print to show the finalsql and look at the contents.
ASKER
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,ST K_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.
"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,
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.
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.
ASKER
Thank you for your reply. Can you give some code please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Because I found the answer by myself.
Thank you.
Thank you.