|
-
Jun 26th, 2009, 12:37 PM
#1
Thread Starter
Addicted Member
DataGridView doesn't give values to DataSource
Hi there
I'm working in my own datagridview. I set AllowUserToAddRows = false and EditMode to EditOnEnter.
The way user can actually add rows is clicking a button or pressing insert key.
The thing is that when I try to update the source, all values of the new row added are null, so insert statement fails, because some fields doesn't accept them.
I try calling DataGridView.EndEdit() and DataGridView.ProcessDialogKey(Keys.Enter) but none of these worked.
When the view passes values to model? Wich is the way DataGridView to tell to the DataSource "hey, I've new values for you, take them!".
Best regards.
-
Jun 27th, 2009, 12:07 AM
#2
Re: DataGridView doesn't give values to DataSource
You've provided essentially no information. We know that you have a DataGridView and a "model" and that's it. We can't possibly know what you're doing wrong if we don't know what you're doing.
The obvious choice to me is to just use data-binding. When you want to add a new item you add it to the bound collection. The grid doesn't have to tell anybody anything then because the model already contains the data. It's the model that tells the grid that there's new data to display. Whether or not you're already doing that I don't know because you've chosen not to provide us with that information.
In future I suggest that you remember the fact that we know nothing about your project other than what you tell us, so you need to provide us with all the relevant information.
-
Jun 27th, 2009, 12:19 PM
#3
Thread Starter
Addicted Member
Re: DataGridView doesn't give values to DataSource
I'm sorry about that. In the future i'll provide you with more information. Here's the code I wrote:
C# Code:
public virtual void Initialize() { db = new Db(); this.AllowUserToAddRows = false; this.EditMode = DataGridViewEditMode.EditOnEnter; } public void Load() { if (db == null) { db = new Db(); } db.Open(); source = db.GetData(CreateQuery()); db.Close(); this.DataSource = source; this.DataMember = source.Tables[0].TableName; } public void UpdateSource() { if (this.IsCurrentCellInEditMode) { base.ProcessDialogKey(Keys.Enter); } if (source != null) { if (source.HasChanges()) { if (IsDataValid()) { db.Open(); db.Update(source); db.Close(); } else { App.Show("Existen errores en los datos ingresados. No se han realizado cambios."); } } } } private void AddRow() { if (source != null) { if (IsDataValid()) { source.Tables[0].Rows.Add(source.Tables[0].NewRow()); } } this.CurrentCell = this.Rows[this.Rows.Count - 1].Cells[this.FirstDisplayedCell.ColumnIndex]; } private void MoveToNextCell() { if (this.CurrentCell.ColumnIndex == this.Columns.Count - 1) { AddRow(); this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home)); this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down)); } else { this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Right)); } }
The AddRow() method add a new row to the DataTable bounded. I'm not adding rows to the 'view'. The UpdateSource() method is called to update the bounded data source.
If there's something that you don't understand about it, please let me know.
Best regards.
-
Jun 27th, 2009, 09:13 PM
#4
Re: DataGridView doesn't give values to DataSource
The first thing I would suggest is to bind your data via a BindingSource. You bind the data to the BindingSource and the BindingSource to the grid. To add a new row you then call AddNew on the BindingSource. When you're ready to save you call EndEdit on the BindingSource.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|