I need an example of actual binding of a user control with 2 properties to a column.
What I've done up to now:
1) created a class DataCellModel derived from GridGenericControlCellModel and a renderer derived from GridGenericControlCellRenderer
2) added the following model to grid models:
this.Grid1.Model.CellModels.Add("DataEntry", new DataCellModel(this.Grid1.Model));
3) populated the column I want to bind the data in the DataSet with a class with 2 properties in it.
4) defined the following for the column I need to bind:
MyControl control = new MyControl();
style.CellType = "DataEntry";
style.Tag = control;
Now I'm getting the control displayed but I have to populate the values that appear in style.CellValue into my control per each cell and in cases where style.CellValue is null - make the control invisible.
Please note I'm using GridDataBoundGrid
Please help!
RC
Rajadurai C
Syncfusion Team
December 15, 2008 01:46 PM UTC
Hi Leah,
Thanks for your interest in Syncfusion products.
To set values to the column, try handling PrepareViewStyleInfo event with setting the celltype as UserControl if the cellvalue is not null.
void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
{
if (e.ColIndex == 4 && e.Style .CellType =="TextBox")
{
if (e.Style.CellValue != null)
{
e.Style.CellType = "UserControl";
}
}
}
Regards,
Rajadurai
SL
Schneider Leah
December 15, 2008 02:34 PM UTC
Hi,
When I say user control with 2 properties I mean that each property has to get a value from the object stored in CellValue.
I'm looking for the right place to do that. Currently I implemented that in OnDraw method of the cell renderer, but it doesn't sound right to me.
What you provided is a way to set some cells to be bound to a control, I don't need that - doing it for the whole column.
Thanks, Leah
RC
Rajadurai C
Syncfusion Team
December 16, 2008 07:13 AM UTC
Hi Leah,
Sorry for misunderstand the issue.
Please try to initialize the usercontrol through overridden OnInitialize() method in cell renderer.
Here i'm attaching a sample codesnippet showing how to initialize a usercontrol through two custom properties.
//in cell renderer
DoubleTextCell editDoubleTextCell = new DoubleTextCell();
protected override void OnInitialize(int rowIndex, int colIndex)
{
DoubleTextBoxCellModel.InitializeDoubleTextBox(editDoubleTextCell, Grid.GetViewStyleInfo(rowIndex, colIndex));
}
//in cell model
public static void InitializeDoubleTextBox(DoubleTextCell c, GridStyleInfo style)
{
c.TopValue = string.Empty;
c.BottomValue = string.Empty;
if (style.CellValue != null)
{
Customer customer = style.CellValue as Customer;
c.TopValue = customer.CustomerID;
c.BottomValue = customer.CustomerName;
c.BackColor = style.BackColor;
}
}
Here TopValue and BottomValue are two custom properties reflecting customerId and customername to the grid.
Regards,
Rajadurai
SL
Schneider Leah
December 16, 2008 09:14 AM UTC
Hi Rajadurai,
That's where the problem lies. OnInitialize is being called only when I click on the displayed control. But I need the binding before. That's the cause I'm doing it in OnDraw event. This way it works.
Any ideas?
Thankis, Leah
RC
Rajadurai C
Syncfusion Team
December 16, 2008 12:47 PM UTC
Hi Leah,
You can initialize through the OnDraw() method in renderer class.
Please try the following code as this works in our side.
1) For initializing, you can handle drawDoubleTextCell object and invoke from OnDraw() method.
2) For editing, you can handle editDoubleTextCell object and invoke from OnInitialize() method.
DoubleTextCell drawDoubleTextCell = new DoubleTextCell();
DoubleTextCell editDoubleTextCell = new DoubleTextCell();
//add the following code in renderer' constructor part
FixControlParent(drawTwoTextCell);
SetControl(editTwoTextCell);
//OnDraw event
protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style)
{
if (this.ShouldDrawFocused(rowIndex,colIndex))
{
style.Control = editDoubleTextCell;
}
else
{
style.Control = drawDoubleTextCell;
DoubleTextBoxCellModel.InitializeDoubleTextBox(drawDoubleTextCell, style);
}
base.OnDraw (g, clientRectangle, rowIndex, colIndex, style);
}
Regards,
Rajadurai
SL
Schneider Leah
December 23, 2008 02:17 PM UTC
Hi Rajadurai,
I think I implemented whatever I was told.
Now my problem is with editing. When I edit my control in the first row then click on the next one - the change appears there as well. I have a feeling the same instance of a control is being used all over.
Please advise. I have to bind the data and then edit it and read from the control.
Thanks, Leah
Hierarchy3_6a80a782.zip
RC
Rajadurai C
Syncfusion Team
December 25, 2008 10:31 AM UTC
Hi Leah,
Apologies for the delay.
I have gone through the attached sample. Use derived cell model and derived cell renderer doesn't allow the duplication of values. I think the issue will be in saving changes made to the cell. Please refer to the following sample which shows a user control "DropDownList" in which some events are handled in such a way that each cell can store distinct values.
http://www.syncfusion.com/support/user/uploads/I52159 (2)_3f2ac273.zip
Please let me know if you have any further concerns.
Regards,
Rajadurai