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

Changing the behaviour of TextBox cells in DB Grid

Hello, I''m trying to code the following functionality: Certain cells in the databound grid should contain editable values that are representations of some other values in the datasource. Example: column 1 should contain value of type "weight". While on the screen it would be displayed and edited as kgs, pounds, ounces etc; in datasource I want it to stay in kgs all the time. column 2 has "linear dimension", that should be displayed/edited in meters, feet, yards, depending on configuration, but again, datasource should store it all in meters. We already have a library of parsers/converters/validators that make it simple to convert any text value with specified units to standard units, and vice versa. What I''m trying to figure out is where do I plug in these parsers and formatters? Creating a new GridMyTextBoxCellModel with a new CellRendered seems like overkill - it''s still just a TextBox!.. Or can I inherit from GridTextBoxCellModel without creating my own renderer? Thanks in advance!

5 Replies

AD Administrator Syncfusion Team August 17, 2004 04:57 PM

You can use the event, QueryCellFormattedText, to set the formatted value in a cell, say converting kgs to lbs. Then you could use SaveCellFormattedText to convert the value back. Here is a little sample that has values stored in kgs in column 1. The grid will display it in either kgs or lbs depending upon whether there is a # in column 2. KGStoLBS_1578.zip


MF Michael Feinstein August 19, 2004 07:36 PM

This seem like perfect solution, except we need a way to determine format information from QueryCellFormattedText parameters alone (the example uses another column). I was thinking to inherit ColumnDescriptor (since we want to configure it anyway), is there a way to get to ColumnDescriptor class of the cell from GridCellTextEventArgs ? Is it good way to go, or can you recommend another way? For example in DataBound grids one can override PropertyDescriptor.


AD Administrator Syncfusion Team August 20, 2004 12:36 AM

I am not sure what you mean by column descriptor. If you mean GridBoundColumn or PropertyDescriptor, you can get those with code like: int field = this.gridDataBoundGrid1.Binder.ColIndexToField(e.Style.CellIdentity.ColIndex); GridBoundColumn gbc = this.gridDataBoundGrid1.Binder.InternalColumns[field]; PropertyDescriptor pd = gbc.PropertyDescriptor; If you have explicitly added GridBoundColumns, then you would use the grid.GridBoundColumns collection in the above code instead of the InternalColumns collection. If you mean something else, could you explain a little more about it.


MF Michael Feinstein August 20, 2004 02:07 PM

Sorry I was not very clear, we are going to use GroupingGrid, and there is plenty of information in ColumnDescriptor classes (that are in TableDescriptor) to allow us to dtermine in run-time on how to format text. What we need is a way to get to the information in the events (because ControlGrid has these 2 events as well) >I am not sure what you mean by column descriptor. > >If you mean GridBoundColumn or PropertyDescriptor, you can get those with code like: > >int field = this.gridDataBoundGrid1.Binder.ColIndexToField(e.Style.CellIdentity.ColIndex); >GridBoundColumn gbc = this.gridDataBoundGrid1.Binder.InternalColumns[field]; >PropertyDescriptor pd = gbc.PropertyDescriptor; > > >If you have explicitly added GridBoundColumns, then you would use the grid.GridBoundColumns collection in the above code instead of the InternalColumns collection. > >If you mean something else, could you explain a little more about it. >


AD Administrator Syncfusion Team August 21, 2004 07:55 AM

Here is some code that gets at the Record for the row from within the QueryCellFormattedText event on the main TableModel.
private void TableModel_QueryCellFormattedText(object sender, GridCellTextEventArgs e)
{
	Element el = this.groupingGrid1.Table.DisplayElements[e.Style.CellIdentity.RowIndex];
	RecordRow rec =  el as RecordRow ;
	if(rec != null)
	{
		Record r = rec.ParentRecord;
		DataRowView drv =  r.GetData() as DataRowView;
		if(drv != null)
		{
			Console.WriteLine("************ " + drv[0].ToString() + "  " + drv[1].ToString());
		}
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon