We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Initializing hidden fields

Hi, Which event is the best to set value hidden fields (as Timestamp/userId/default values...) in DBGrid when a new row is "edited" ? Thanks Eddy POULLET Brussels

4 Replies

AD Administrator Syncfusion Team July 13, 2004 02:40 PM UTC

There is sort of a chicken and egg problem here. The grid does not actually add the row to the DataSource until you confirm the new row by leaving it. So, if you want to set default values of things that are not in the grid, you have to work around this delay of adding the new row to the DataSource. One way you can do it is to add the new row yourself, not relying on the grid to do it. You can do this in RowEnter. Here is a minimal snippet assuming the DataSource is a Datatable.
private void gridDataBoundGrid1_RowEnter(object sender, GridRowEventArgs e)

{
	if(this.gridDataBoundGrid1.Model.RowCount == e.RowIndex)
	{
		//assumes DataSource is a DataTable
		DataTable dt = (DataTable) this.gridDataBoundGrid1.DataSource;
		DataRow dr = dt.NewRow();
		//populate your new row however you want
		dr[0] = "col0";
		dr[1] = "col1";
		dt.Rows.Add(dr);
	}
}


EP Eddy Poullet July 14, 2004 09:31 AM UTC

Thanks Clay, Until now, I have used the ColumnChanging event of the datatable. >There is sort of a chicken and egg problem here. The grid does not actually add the row to the DataSource until you confirm the new row by leaving it. So, if you want to set default values of things that are not in the grid, you have to work around this delay of adding the new row to the DataSource. > Why doesn''t it exist an event in the dataset/grid like onNewRow or onBeforeEdit? This will a litle easier ! Eddy


AD Administrator Syncfusion Team July 14, 2004 10:33 AM UTC

The grid has the problem of postponing the actual adding of the row. So, if you wanted to set defaluts in the grid, you would likely need to do that prior to the user typing into the new row. But, by default, the new row is not really in the DataTable at this point, and trying to add default values for fields not in the grid would be a problem. The above code works around this problem. If you want to use a DataSource event to manage setting default values (whether they are in the grid or not), you can catch the IBindingList.ListChanged event on the List member of the CurrencyManager. Below are some snippets. I have forwarded your request for a special grid event to try to manage this task to the grid architect to see if this is something we want to do. CurrencyManager cm = (CurrencyManager) this.BindingContext[this.gridDataBoundGrid1.DataSource, this.gridDataBoundGrid1.DataMember]; IBindingList ibl = cm.List as IBindingList; ibl.ListChanged += new ListChangedEventHandler(ibl_ListChanged);
private void ibl_ListChanged(object sender, ListChangedEventArgs e)
{
	if(e.ListChangedType == ListChangedType.ItemAdded)
	{
		DataView dv = sender as DataView;
		dv[e.NewIndex]["col0"] = "aaaaa";
		dv[e.NewIndex]["col1"] = "bbbbb";
	}
}


EP Eddy Poullet July 14, 2004 01:21 PM UTC

Hi Clay, I understand now better the inner working of the DBGrid. Thanks for the help and the support ! Eddy

Loader.
Live Chat Icon For mobile
Up arrow icon