Results 1 to 4 of 4

Thread: DataGridView doesn't give values to DataSource

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    198

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    198

    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:
    1. public virtual void Initialize()
    2.         {
    3.             db = new Db();
    4.             this.AllowUserToAddRows = false;
    5.             this.EditMode = DataGridViewEditMode.EditOnEnter;
    6.         }
    7.  
    8.         public void Load()
    9.         {
    10.             if (db == null)
    11.             {
    12.                 db = new Db();
    13.             }
    14.             db.Open();
    15.             source = db.GetData(CreateQuery());
    16.             db.Close();
    17.             this.DataSource = source;
    18.             this.DataMember = source.Tables[0].TableName;
    19.         }
    20.  
    21.         public void UpdateSource()
    22.         {
    23.             if (this.IsCurrentCellInEditMode)
    24.             {
    25.                 base.ProcessDialogKey(Keys.Enter);
    26.             }
    27.             if (source != null)
    28.             {
    29.                if (source.HasChanges())
    30.                {
    31.                    if (IsDataValid())
    32.                    {
    33.                        db.Open();
    34.                        db.Update(source);
    35.                        db.Close();
    36.                    }
    37.                    else
    38.                    {
    39.                        App.Show("Existen errores en los datos ingresados. No se han realizado cambios.");
    40.                    }
    41.                }
    42.             }
    43.         }
    44.  
    45.         private void AddRow()
    46.         {
    47.             if (source != null)
    48.             {
    49.                 if (IsDataValid())
    50.                 {
    51.                     source.Tables[0].Rows.Add(source.Tables[0].NewRow());
    52.                 }
    53.             }
    54.             this.CurrentCell = this.Rows[this.Rows.Count - 1].Cells[this.FirstDisplayedCell.ColumnIndex];
    55.         }
    56.  
    57.         private void MoveToNextCell()
    58.         {
    59.             if (this.CurrentCell.ColumnIndex == this.Columns.Count - 1)
    60.             {
    61.                 AddRow();
    62.                 this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
    63.                 this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down));
    64.             }
    65.             else
    66.             {
    67.                 this.ProcessDataGridViewKey(new KeyEventArgs(Keys.Right));
    68.             }
    69.         }

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width