datagrid - new row

vbnetlearner

Member
Joined
Aug 10, 2006
Messages
13
Programming Experience
Beginner
I have a vb.net form that has a datagrid. New rows are to datagrid by click of a button. Code to add rows into datagrid is in the click event of the button and has datagrid linked to a datatable. So datagrid.datasource is datatable. The button click event calls a SQL stored procedure that gets information for the row and fills the datatable which inturn will populate datagrid because it is linked to datatable.

When I click the button to add new row, it successfully adds new row. If old rows already exist on the datagrid, it successfully appends new row. So far so good. The problem I have is when I click into the datagrid. When I click into the datagrid and then click on button to add new row, it deletes all old rows and creates a new row and that will be the first row in the grid, instead it should have simply appended to old list.

Could someone please help as I am stuck with this for a long time. I need to urgently move on.

Thanks
 
The rows aren't just disapperaing on their own. If they're being deleted or removed or the data source is being changed then it's your code that's doing it, so without seeing the code we can't possibly tell you what the problem is.
 
datagrid

Here's a part of my code for click event of the button:


If Me.grdMaterial.VisibleRowCount <> 0 Then
Dim cmdGetGrdMat As New SqlCommand("spGet", cn)
cmdGetGrdMat.CommandType = CommandType.StoredProcedure
cmdGetGrdMat.Parameters.Add("@JobID", JobNumber)
Dim daGetGrdMat As New SqlDataAdapter(cmdGetGrdMat)
Dim dtGetGrdMat As New DataTable
daGetGrdMat.Fill(dtGetGrdMat)
Dim rowGetGrdMat As DataRow
For Each rowGetGrdMat In dtGetGrdMat.Rows
Me.grdMaterial.DataSource = tblMaterial
rowGrdMaterial = tblMaterial.NewRow()
rowGrdMaterial("Material") = rowGetGrdMat("fldmatdesc")
rowGrdMaterial("Original") = rowGetGrdMat("fldO_sides")
rowGrdMaterial("Printed Copies") = rowGetGrdMat("fldp_sides")
rowGrdMaterial("Pages") = rowGetGrdMat("fldo_pages")
rowGrdMaterial("Copies") = rowGetGrdMat("fldp_copies")
rowGrdMaterial("Total Printed Pages") = rowGetGrdMat("fldp_pages")
rowGrdMaterial("Cost") = rowGetGrdMat("fldmatcost")
rowGrdMaterial("Type") = rowGetGrdMat("fldmattype")
rowGrdMaterial("jobmatid") = rowGetGrdMat("jobmatid")
tblMaterial.Rows.Add(rowGrdMaterial)
Next
End If

Me.grdMaterial.DataSource = tblMaterial
rowGrdMaterial = tblMaterial.NewRow()
rowGrdMaterial("Pages") = Me.txtO_Pages.Text
rowGrdMaterial("Copies") = Me.txtP_Copies.Text
rowGrdMaterial("Total Printed Pages") = Me.lblP_Pages.Text
rowGrdMaterial("Cost") = Me.lblMatCost.Text
tblMaterial.Rows.Add(rowGrdMaterial)


Hope it makes sense
 
CURIOSITIES:
#1
Me.grdMaterial.DataSource = tblMaterial
DOES NOT NEED TO BE INSIDE THE 'FOR' LOOP.

#2
Me.grdMaterial.DataSource = tblMaterial
rowGrdMaterial = tblMaterial.NewRow()
rowGrdMaterial("Pages") = Me.txtO_Pages.Text
rowGrdMaterial("Copies") = Me.txtP_Copies.Text
rowGrdMaterial("Total Printed Pages") = Me.lblP_Pages.Text
rowGrdMaterial("Cost") = Me.lblMatCost.Text
tblMaterial.Rows.Add(rowGrdMaterial)


WHAT ARE YOU DOING HERE? ADDING HALF A RECORD AFTER THE LOOP?


ANYWAY, THERE IS NO NEED TO KEEP ASSIGNING THE DATASOURCE.
START THERE.
 
datagrid

I have solved this now. It was my mistake that I did not notice the counter that I had set which was causing the for loop to be skipped.

Thanks anyway for your help.
 
Back
Top