Ask a questionAsk a question
 

AnswerAdding rows to a databound DataGridView

  • Wednesday, October 05, 2005 9:16 PMTkNeo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Does any know how to programmatically add rows to a Data bound DataGridView.

    1. the .rows.add method doesnt work because its for unbound datagridviews

    Currently i am calling the addnew() method of the BindingSource connected to the DataGridView but this solution gets me into much more trouble.

    Any other way out ?

Answers

  • Thursday, October 06, 2005 5:41 PMScott Berry Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You can't: the nth row represents the nth item in the bound data.  There are workaround, but none of them do exactly what you ask:
    1) Add a row to the bound data.  This may not be feasible, especially if you're writing the data back when you're done.
    2) Use an unbound DataGridView, reading the cell contents in and either setting RowCount or Rows.Add to get the number of rows you want. 
    3) Especially if you have a lot of rows, you might go the virtual route: check out the docs for VirtualMode.  You set it to true, and deal with events to pass out and collect data.  You'd just return the value in the data if it's a row corresponding to the data, and your extra values if it's not.  As with #2 here, you'd have to handle some of the data yourself if you want the data changable (so handle CellValuePushed here and change the underlying data).
       -Scott

All Replies

  • Thursday, October 06, 2005 5:41 PMScott Berry Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You can't: the nth row represents the nth item in the bound data.  There are workaround, but none of them do exactly what you ask:
    1) Add a row to the bound data.  This may not be feasible, especially if you're writing the data back when you're done.
    2) Use an unbound DataGridView, reading the cell contents in and either setting RowCount or Rows.Add to get the number of rows you want. 
    3) Especially if you have a lot of rows, you might go the virtual route: check out the docs for VirtualMode.  You set it to true, and deal with events to pass out and collect data.  You'd just return the value in the data if it's a row corresponding to the data, and your extra values if it's not.  As with #2 here, you'd have to handle some of the data yourself if you want the data changable (so handle CellValuePushed here and change the underlying data).
       -Scott

  • Thursday, June 22, 2006 10:06 PMJoe M. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    But, in any way, how would you add a row to the DataGridView?

    And, once you have information on a DataGrid... how would you save it on a Form Exit?

    Any help would be appreciated.

    Thanks.

     

  • Thursday, June 22, 2006 10:44 PMScott Berry Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    If you're bound to a data source like a DataView, just add the data to the table.  You can call Update on a DataAdapter to save the data.

    If you use the Data Sources window and drag/drop the data you want on, it'll give you a good place to start for small to medium data sets (you wouldn't want to drag/drop a list of all the books in the library onto your form).  It will also generate code for a BindingNavigator's Save button that will show you how to update your data.

          -Scott

  • Wednesday, January 30, 2008 10:49 PMThomasBHolland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals