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

Insert custom control into DataBoundGrid

Hi, thanks a lot for the last answer. It solved the problem. This is what I'm doing: - read from Data Reader several rows - build DataTable with those rows - put DataTable in DataSet - view DataSet selected in a GridDataBoundGrid How can I insert a control I made into the second column of each row of a DataBoundGrid ? I'm building the control when I'm reading from the Data Reader, and I would like to put it at this time in the DataSet, so it would be already build when I put the DataSet in the DataBoundGrid. Thanks a lot once again. Jose Melo.

7 Replies

AD Administrator Syncfusion Team October 27, 2003 03:28 PM UTC

What do you mean by control? Do you have a single Control derived object that you want to use to display the data in column 2? Or, do you have many controls (one for each row), that you want to use display the data is each row? To do the first, you would have to derive a cellmodel class and a cellrenderer class as discussed in section 3.15 of the user's guide. You would override the renderer's OnInitialize method to move the state information from the style object to your control. You would also override the OnSaveChanges method to move the state information from your control back to the style. These are the techniques used for sharing controls. There are several samples listed in teh Users Guide, and there are several more in the CellTypes samples folder. If you do a search for CellRenderer in our Samples folder, you will see many, many hits for derived cell controls. To use teh second method, you could try using the "Control" cell type. For a GridDataBoundGrid, you woul dneed to handle the grid.Model.QueryCellInfo event and set the e.Style.Control based on e.RowIndex. Here is a little code that just puts a WIndows Forms TextBox in a cell using QueryCellInfo.
private TextBox theTextBox;
private void Form1_Load(object sender, System.EventArgs e)
{
	theTextBox = new TextBox();
}
private void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
	if(e.RowIndex == 2 && e.ColIndex == 2)
	{
		e.Style.CellType = "Control";
		e.Style.Control = this.theTextBox;
	}
}


JL Jose Luis Melo October 31, 2003 04:36 PM UTC

Hi, it was the first situation that I needed. Thanks a lot. But I still have a problem. All the examples I saw you used Gridcontrols. I'm already displaying the control in a DataBoundGrid, but it is always the same control. I want to use the control, in a certain column in all rows, but with different properties. How can I show in each row, its instance of the control ? This is what I have: Dim sp As BarStyleProperties gridDataBoundGrid1.Model.CellModels.Add("Bar", New BarCellModel(gridDataBoundGrid1.Model)) Dim cm As CurrencyManager cm = CType(Me.BindingContext gridDataBoundGrid1.DataSource, gridDataBoundGrid1.DataMember), CurrencyManager) CType(cm.List, DataView).AllowNew = False Dim pdc As PropertyDescriptorCollection pdc = cm.GetItemProperties Dim pd As PropertyDescriptor For Each pd In pdc Dim col As GridBoundColumn col = New Syncfusion.Windows.Forms.Grid.GridBoundColumn(pd) col.MappingName = pd.Name col.HeaderText = pd.Name If (pd.Name = "MyControl") Then col.StyleInfo.CellType = "Bar" sp = New BarStyleProperties(col.StyleInfo) sp.Property1 = "11" sp.Property2 = "22" End If gridDataBoundGrid1.GridBoundColumns.Add(col) Next I tried to insert the control in only one row ( with col.StyleInfo = gridDataBoundGrid1(2, 2) ), but I couldn't. It only worked with GridControls. Thanks a lot once again. Jose Melo.


AD Administrator Syncfusion Team October 31, 2003 10:43 PM UTC

Your code setting the GridBoundColumn.StyleInfo.CellType is the proper way to try to use your custom control in a column in a GrigDataBoundGrid. In your BarCellRenderer class, have you overridden OnInitialize? This is where you would normally retrieve proprrties from the style object and use them to initialize your 'bar' control to reflect the state of the row and col passed in. Also, you would normally override renderer.OnSaveChanges to takes the current settings in your control and move them back into the style.


JL Jose Luis Melo November 3, 2003 02:09 PM UTC

Yes, I have a overridden OnInitialize in my BarCellRenderer class. Protected Overrides Sub OnInitialize(ByVal rowIndex As Integer, ByVal colIndex As Integer) BarCellModel.InitializeBar(bar, Grid.GetViewStyleInfo(rowIndex, colIndex)) End Sub I already created a RowNumber and ColumnNumber property, but how can I send it the row number, since the (class) property is not known inside the renderer class ? Couldn't I use: col.StyleInfo = gridDataBoundGrid1(2, 2) like I use with a Gridcontrol ? Thanks, Jose Melo


AD Administrator Syncfusion Team November 3, 2003 02:35 PM UTC

>>I already created a RowNumber and ColumnNumber property, but how can I send it the row number, since the (class) property is not known inside the renderer class ? I am not sure what you are referring to by 'it' when you ask how can I send it the row number. You do not call renderer.OnInitialize. The grid will call it when it needs to, and will pass it the proper row and col. So, you should normally not have to call it. In a GridDataBoundGrid, if you want some style property to change from row to row, then you either have to do it in the grid.PrepareViewStyleInfo event, or in teh grid.Model.QueryCellInfo event. In either of these events, you would set e.Style depending upon e.RowIndex and e.ColIndex. In this manner, you can make the style vary from row to row.


JL Jose Luis Melo November 5, 2003 08:38 AM UTC

Hi, Ok, that solved. It was really the DataBoundGrid.PrepareViewStyleInfo event I was looking for. I can put the "Bar" control in several rows with different values. Only one problem left. When I re-populate the grid with different values and different "Bar" controls, some cells are not cleaned from the last populated values. And sometimes the BoundGrid still shows, with no columns and rows in it, the "Bar" controls I designed before... In conclusion, the grid is not cleanned. I tried grid.Refresh, grid.GridBoundColumns.Clear and grid.ResetGridBounds methods, but the "Bar" controls don't disapear. How can I clean the DataBoundGrid ? PS-My "Bar" controls is very like a progress bar, but with several colors. Thanks a lot once again. Jose Melo.


AD Administrator Syncfusion Team November 5, 2003 10:27 AM UTC

PrepareViewStyleInfo is completely dynamic. It is call continously as the grid is drawn and worked with by the user. So, any properties you set there should be dynamically and instantaneouly reflected as the grid is drawn again. So, after changing your data, if you do a grid.Refresh that should be enough to trigger PrepareViewStyleInfo being hit for all visible cells. So, if these cells are not being drawn as you think they should be, then check out how you are setting the e.Style properties in PrepareViewStyleInfo. Check whether the data/controls you use to set these properties reflect the data you think they should have.

Loader.
Live Chat Icon For mobile
Up arrow icon