Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dynamic value for new row in DataGrid
Message
De
26/12/2006 09:05:55
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Titre:
Dynamic value for new row in DataGrid
Versions des environnements
Environment:
C# 1.1
Divers
Thread ID:
01180182
Message ID:
01180182
Vues:
68
I have .NET 1 WinForms dataGrid bound to dataTable.
dataTable has primary key field.
dataTable constraints should be checked during editing.

If user moves out from new row, an error occurs.
RowChanging event does not fire before error.

How to fill field with dynamic default value when new row is added to DataGrid ?

To reproduce:

1. Run code
2. Enter data to first column only
3. Press Down Arrow

Observed error:

---------------------------
Committing the row to the original data store has caused an error.
---------------------------
Value Column 'Column2' does not allow nulls. Do you want to correct this value?

Expected:

No error. Primary key column should be filled.
using System.Windows.Forms;
using System.Data;
static class Program {
	static void Main() {
		Application.Run(new Frm());
		}
	}

class Frm: Form {
	public Frm() {
		DataTable table = new DataTable();
		DataColumn[] keys = new DataColumn[1];
		DataColumn column1 = new DataColumn();
		DataColumn column2 = new DataColumn();
		DataGrid grid = new DataGrid();
		
		grid.Dock = DockStyle.Fill;
		table.Columns.Add(column1);
		table.Columns.Add(column2);
		keys[0] = column2;
		table.PrimaryKey = keys;
		Controls.Add(grid);
		table.RowChanging += new DataRowChangeEventHandler(OnRowChanging);
		grid.SetDataBinding(table, "");
		}

	private void OnRowChanging(object sender, DataRowChangeEventArgs e) {
		if (e.Action == DataRowAction.Add) {
			string pk = e.Row.Table.PrimaryKey[0].ColumnName;
			if (e.Row[pk] == null)
				e.Row[pk] = new System.Guid();
			}
		}
	}
Andrus
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform