question

Vito-9007 avatar image
0 Votes"
Vito-9007 asked karenpayneoregon commented

Datagrid default value - .NET FRAMEWORK C# WINFORM

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";
     }
 }

}

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Vito-9007 avatar image
0 Votes"
Vito-9007 answered karenpayneoregon commented

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";

     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Best not to use the DataGridView, instead work against the BindingSource.Add method by adding a new DataRow with default values.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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
 }




bsn.png (4.5 KiB)
add.png (2.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.