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

Catch new row insert in bound grid

How can I do that? Is there an event for new row insertion?

9 Replies

AD Administrator Syncfusion Team March 9, 2003 12:39 PM UTC

Currently, there is no rowinsert event for GridDataBoundGrid. But there are a couple of other events that you can use depending upon exactly what action you want to catch. To catch the currentcell moving to the AddNew row, you can handle RowEnter and test the rowindex. If you want to catch the first keystroke on the addnew row that starts the edit, you can catch RowEditing and test the row index there. Below are some snippets.
private void gridDataBoundGrid1_RowEditing(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
{
	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
	{
		Console.WriteLine("RowEditAddnew");
	}
}
private void gridDataBoundGrid1_RowEnter(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
{
	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
	{
		Console.WriteLine("RowEnterAddnew");
	}
}


AD Administrator Syncfusion Team March 16, 2003 03:47 PM UTC

> Currently, there is no rowinsert event for GridDataBoundGrid. > > But there are a couple of other events that you can use depending upon exactly what action you want to catch. To catch the currentcell moving to the AddNew row, you can handle RowEnter and test the rowindex. If you want to catch the first keystroke on the addnew row that starts the edit, you can catch RowEditing and test the row index there. Below are some snippets. > >
> private void gridDataBoundGrid1_RowEditing(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
> {
> 	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
> 	{
> 		Console.WriteLine("RowEditAddnew");
> 	}
> }
> private void gridDataBoundGrid1_RowEnter(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
> {
> 	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
> 	{
> 		Console.WriteLine("RowEnterAddnew");
> 	}
> }
> 


AD Administrator Syncfusion Team March 16, 2003 03:48 PM UTC

> Currently, there is no rowinsert event for GridDataBoundGrid. > > But there are a couple of other events that you can use depending upon exactly what action you want to catch. To catch the currentcell moving to the AddNew row, you can handle RowEnter and test the rowindex. If you want to catch the first keystroke on the addnew row that starts the edit, you can catch RowEditing and test the row index there. Below are some snippets. > >
> private void gridDataBoundGrid1_RowEditing(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
> {
> 	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
> 	{
> 		Console.WriteLine("RowEditAddnew");
> 	}
> }
> private void gridDataBoundGrid1_RowEnter(object sender, Syncfusion.Windows.Forms.Grid.GridRowEventArgs e)
> {
> 	if(this.gridDataBoundGrid1.EnableAddNew && e.RowIndex == this.gridDataBoundGrid1.Model.RowCount)
> 	{
> 		Console.WriteLine("RowEnterAddnew");
> 	}
> }
> 


KL Ken Law March 16, 2003 03:58 PM UTC

I've been very frustrated trying to do something that should be extremely simple. Setting default values in new rows. Using RowEditing or RowEnter is a problem because the datasource does not yet have a record to edit. Trying to set the CellValue just doesn't work (no effect). So I tried to use the binder.EditModeChanged event. This works because the datasource available and editing, but there is no way to detect if you have a new row because conditions are the same when you re-edit the last row (you can't set a default value again - the user may have changed it during the initial edit). There really should be a default value property in GridBoundColumn.StyleInfo to avoid even having to write code. Otherwise, there should be a NewRow event so default values can be set. This is the most basic of needs.


AD Administrator Syncfusion Team March 16, 2003 09:53 PM UTC

We will make this easier. Attached is a sample that uses the DataColumn.DefaultValue to specify new defaults. It also has a CurrentCellMoved event handled to set the default into the activated cell on the new row. Maybe something like this will work for you.


KL Ken Law March 17, 2003 04:08 PM UTC

Clay, Thanks, this is a step in the right direction because the code: cc.RowIndex == this.gridDataBoundGrid1.Model.RowCount is a valid test for a new row. Unfortunately, I need to set a value for a column in the DataTable that is not visible to the user. This value is different depending on which of two GDBGs the table is bound to (each has its own view), so I can't use the DataColumn.DefaultValue. I believe when this new row condition is detected in the CurrentCellMoved handler, there is not yet a data row bound to the grid row. The tough issue seems to be: how do I detect a new row condition and set values in the underlying data row in fields not bound to GridDataBound columns (invisible to the user).


AD Administrator Syncfusion Team March 17, 2003 06:05 PM UTC

Since you want to set values in the DataRow object that has no impact on the GridBoundDataGrid (as these columns are not used in the grid, are they?), why can't you set these values when you leave the row? Leaving the row is the confirmation action that says things are OK to save this edited grid data. Before this point, the changed values are cached locally in the grid, and are not flushed to the data source until you leave the row (or call Binder.EndEdit()). So, can you set these defaults in a RowSaved event handler. At that point, there should be no problem of the DataRow not existing?


MI Mike April 28, 2003 08:47 PM UTC

I want to do the same thing and set defaults, but I want them to actually show up in the row when a user clicks to a new record. This way holds the defaults in the dataset and if nothing is entered it fills it in. I need it to show the user before they finish inserting the record, so they don't have to fill certain defaulted columns out if they don't want to. any ideas?


AD Administrator Syncfusion Team April 28, 2003 10:15 PM UTC

In the grid, the default values come from GridDataBoundGrid.Model.ColStyles[col].CellValue. So, in your formload, if you have the DataTable.Columns[col].DefaultValue's, you could move them into the ColStyles. Here is a little sample showing how you might do it. But if you want to dynamically set the values without relying on the defaults in the DataTable, try handling CurrentCellMoved.

private void gridDataBoundGrid1_CurrentCellMoved(object sender, Syncfusion.Windows.Forms.Grid.GridCurrentCellMovedEventArgs e)

{

	GridCurrentCell cc = this.gridDataBoundGrid1.CurrentCell;

	if(cc.RowIndex == this.gridDataBoundGrid1.Model.RowCount)

	{

		this.gridDataBoundGrid1.Binder.BeginEdit();

		this.gridDataBoundGrid1[cc.RowIndex , 1].Text = "defaultcol1";

		this.gridDataBoundGrid1[cc.RowIndex  , 2].Text = "defaultcol2";

	}

}


Loader.
Up arrow icon