Editable Datagrid C# Windows Application Help

  • Thread starter Thread starter seanmle
  • Start date Start date
S

seanmle

I want to build a windows application that has a datagrid filled with
data. When a person modifies information on a single cell, it updates
the database without the user having to click on a save button.

Any ideas on how I can accomplish this?

Thanks,

Sean
 
Hi Sean,

You could handle the appropriate changed event of the data source. In the
2.0 framework you could use a BindingSource component to wrap your data
source and handle the CurrentItemChanged event:

"BindingSource.CurrentItemChanged Event"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingsource.currentitemchanged.aspx

If you're binding directly to a DataTable or DataSet you can handle the
RowChanged event on the DataTable to which the grid is bound:

"DataTable.RowChanged Event"
http://msdn2.microsoft.com/en-us/library/system.data.datatable.rowchanged.aspx

If you're binding to a custom object or binding list you can implement your
own property changed event (although you can still use a BindingSource in
the 2.0 framework)

(Note: If you're using the 2.0 framework use the DataGridView control
instead of DataGrid, which is included in the VS 2005 Toolbox instead of
DataGrid anyway)

I recommend that you don't do this for a single cell, however. Instead,
think about updating the database after the current row loses focus and has
been validated. Also, the entire process is much easier using a
DataGridView. Simply handle the RowValidated event and perform the database
update there:

"DataGridView.RowValidated Event"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidated(VS.80).aspx

You can tell whether a row has changes after it loses focus easily if you're
using a DataSet by checking the DataRow.RowState property for any value
other than
DataRowState.Unchanged and DataRowState.Detached.

"DataRow.RowState Property"
http://msdn2.microsoft.com/en-us/library/system.data.datarow.rowstate.aspx
 

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

Back
Top