Datagrid default value - .NET FRAMEWORK C# WINFORM

Vito 1 Reputation point
2021-07-06T14:28:55.243+00:00

I'm trying to insert a default value using a datagrid whit binding navigator.
If I insert a new record from cliccking the asterisk in the left/bottom of the form "DefaultValuesNeeded" works fine .
If I insert a new record cliccking the plus, using this code, it's insert a good row and a null row using the trigger in the "bindingNavigatorAddNewItem_Click".
Where is the problem ?
Thanks a lot.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GRIDDEFAULT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void rUBRICABindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.rUBRICABindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.dMCODataSet);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: questa riga di codice carica i dati nella tabella 'dMCODataSet.RUBRICA'. È possibile spostarla o rimuoverla se necessario.
        this.rUBRICATableAdapter.Fill(this.dMCODataSet.RUBRICA);

    }

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {
        // BindingNavigator button's AddNewItem automatic tie-in to bindingSource's AddNew() is removed
        // This sets the current cell to the NewRow's cell to trigger DefaultValuesNeeded event
        rUBRICADataGridView.CurrentCell = rUBRICADataGridView.Rows[rUBRICADataGridView.NewRowIndex].Cells[0];
    }

    private void rUBRICADataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {
        // Gets called when datagridview's */NewRow is entered.
        e.Row.Cells[0].Value = 1;
        e.Row.Cells[1].Value = "nome";
        e.Row.Cells[2].Value = "cognome";
    }
}

}

Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vito 1 Reputation point
    2021-07-06T14:54:00.003+00:00

    I'm trying even with this code but the "plus button" generate a first row NULL and the second row correct.

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
    {

            this.dMCODataSet.RUBRICA.Columns[00].DefaultValue = 1;
            this.dMCODataSet.RUBRICA.Columns[01].DefaultValue = "nome";
            this.dMCODataSet.RUBRICA.Columns[02].DefaultValue = "cognome";
    
        }
    

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-07-06T14:55:29.553+00:00

    For the BindingNavigator add record there are default code that does not pay attention to default value needed. To fix this change the default action to none

    112272-bsn.png

    Then double click the add button and write your code to add.

    112224-add.png

    private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)  
    {  
        // TODO  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.