Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am populating datagridview manually..On clicking "Add" button,user can add row to a grid.
For first record,it works well but when I add 2nd row to grid,2nd row gets added but records of first row becomes null..I tried debugging but I am not able to solve the problem..Below is the code to add row to datagridview:
VB
OWNGrid.RowCount = OwnGRow
        OWNGrid.Rows(OwnGRow - 1).Cells(1).Value = CStr(OwnGRow - 1)
        OWNGrid.Rows(OwnGRow - 1).Cells(2).Value = TxtOWNJEVS.Text
        OWNGrid.Rows(OwnGRow - 1).Cells(3).Value = TxtOWNName.Text
        OWNGrid.Rows(OwnGRow - 1).Cells(4).Value = VB6.Format(Today, "dd-MMM-yyyy")

        TxtOWNJEVS.Text = ""
        TxtOWNName.Text = ""

        OwnGRow = OwnGRow + 1


where initially Owngrow = 2...
Posted
Comments
adrian@csdev 21-Mar-13 18:43pm    
Hi Rachna0309

First method is to create a dataset and a table then add the data to the datatable in the dataset and then set the gridview's datasource to the datatable in the dataset.

Second method is to do something like this...

Dim rowNum As Integer = DataGridView1.Rows.Add()
DataGridView1.Rows.Item(rowNum).Cells(0).Value = "Fisrt Value"
DataGridView1.Rows.Item(rowNum).Cells(1).Value = "Second Value"

The first line of code gets the new row of the datagrid everytime

Just test it and let me know if it worked for you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900