How could I add row to bounded datagridview programmatically?

G

Guest

Hi,

I am working on the windows application. I created a form Form1 to list
records. I used datagridview to show data and bound it to a dataadapter. User
can click Update button in Form1 to open a new dialog window Form2 to update
selected record and also add new records. When user closes the Form2, the
Form1's records should be updated (New records should be added and updated
record should be updated.) And we also want to stay in the same place in
Form1 so that user don't need scroll down to find his updates.

Is there any way to do this? I tried to add rows dynamically but got errors.
Is there a way we can refill the datagridview but keep the cursor in the
previous place?

Thanks,
Ying
 
N

Nicholas Paldino [.NET/C# MVP]

Ying,

You can not bind a windows forms grid (DataGrid or DataGridView) to a
data adapter. I imagine you mean that you populated a dataset/datatable
from a data adapter and then bound the grid to that.

How are you trying to add the row dynamically? What are the errors that
you are getting? If you have a data table, then you should be able to call
the NewRow method on the data table you are working with, and it will return
a new row for that table to you.

Once you are done populating that row, you need to add it back to the
Rows collection exposed by the DataTable through the Add method.

Finally, if you want to show the location where the row was added, it
will be dependent somewhat on how the grid is sorted (if you are sorting
it). If you don't have any sorting, then I imagine that the row will show
up at the end of the grid. If it is sorted, then the row should show up
where the row should appear in relation to the sort order.

Either way, you will have to scroll the grid to show the row, as opposed
to having it show up in the middle of the grid.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

"DataGridView add row dynamically" <DataGridView add row
(e-mail address removed)> wrote in message
news:[email protected]...
 
S

Sheng Jiang[MVP]

If you are using an object that supports IBindingList as the datasource
(such as BindingList), and the object raises the ListChanged event, then the
datagrid will be updated when items are added or removed from the
datasource.

--
Sheng Jiang
Microsoft MVP in VC++
"DataGridView add row dynamically" <DataGridView add row
(e-mail address removed)> wrote in message
news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top