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

No subject

I'm working with a GridControl (unbound grid) and I am dynamically building the contents of that grid at runtime. In one of the columns I need to add a combobox to give the user the ability to select from a range of choices. This was fairly easy to accomplish, but what I'd like to do in addition is set the width of the combobox control in the grid. The column width for that particular column is 200 pixels. But, for aesthetic reasons, I'd like to set the width of the combobox to 150 pixels and center it horizontally. This question also applies to other types of controls that I might want to add, such as buttons. Suggestions?

5 Replies

AD Administrator Syncfusion Team August 21, 2003 09:46 AM UTC

To center the combobox in the cell, you can try using the cell style's BorderMargin properties. this.gridControl1.ColWidths[2] = 200; GridStyleInfo style = this.gridControl1[2,2]; style.CellType = "ComboBox"; style.DataSource = dt; style.DisplayMember = "Col1"; style.ValueMember = "Col1"; style.BorderMargins.Right = 25; style.BorderMargins.Left = 25; To change the width of the dropdown itself, you have to handle the CurrentCellShowingDropDown event and set the e.Size.
private void gridControl1_CurrentCellShowingDropDown(object sender, GridCurrentCellShowingDropDownEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	if(cc.ColIndex == 2 && cc.RowIndex == 2)
	{
		e.Size = new Size(150, e.Size.Height);
	}
}


AD Administrator Syncfusion Team August 21, 2003 10:39 AM UTC

That works great, but is there anyway to set the background color of the combobox that is rendered? If I set the BackColor porperty of the style that affects the background color for the entire cell, I just want to change it for the control within the cell only (mainly because my default grid color is non-white). My first thought to get around this since I couldn't seem to find an easy way to do this was to build my own ComboBox and add it to the cell (e.g. style.CellType = "Control", style.Control = myComboBox, and add the control to the form's controls collection). When I did this, however, I noticed that my cpu usage rockets to 100% while the combobox cell has the current focus. When I move off the cell to another cell (without my combobox), the cpu usage returns to normal. What is the best way to handle this? Continue tweaking the properties of the built-in ComboBox CellType or figure out the cpu usage issue if I drop a regular WinForms ComboBox control into that cell? Thanks! > To center the combobox in the cell, you can try using the cell style's BorderMargin properties. > > this.gridControl1.ColWidths[2] = 200; > GridStyleInfo style = this.gridControl1[2,2]; > style.CellType = "ComboBox"; > style.DataSource = dt; > style.DisplayMember = "Col1"; > style.ValueMember = "Col1"; > > style.BorderMargins.Right = 25; > style.BorderMargins.Left = 25; > > > To change the width of the dropdown itself, you have to handle the CurrentCellShowingDropDown event and set the e.Size. >
> private void gridControl1_CurrentCellShowingDropDown(object sender, GridCurrentCellShowingDropDownEventArgs e)
> {
> 	GridCurrentCell cc = this.gridControl1.CurrentCell;
> 	if(cc.ColIndex == 2 && cc.RowIndex == 2)
> 	{
> 		e.Size = new Size(150, e.Size.Height);
> 	}
> }
> 


AD Administrator Syncfusion Team August 21, 2003 11:19 AM UTC

Something to try. Add the color you want to your combobox cell. style.BackColor = Color.Red; Then in a CellDrawn event (requires version 1.6 maybe ??), color the rectangles on either end to be the color you want them to be.
              
private void gridControl1_CellDrawn(object sender, GridDrawCellEventArgs e)
{
	if(e.RowIndex == 2 && e.ColIndex == 2)
	{
		Rectangle rect = e.Bounds;
		rect = new Rectangle(rect.Left, rect.Top, 25 , rect.Height );
		e.Graphics.FillRectangle(Brushes.LightPink, rect);

		rect = e.Bounds;
		rect = new Rectangle(rect.Right - 25, rect.Top, 25, rect.Height );
		e.Graphics.FillRectangle(Brushes.LightPink, rect);
	}
}


AD Administrator Syncfusion Team August 21, 2003 11:45 AM UTC

Clever idea, thanks! Now I have more of a general design question for you when working with these grids. To achieve the functionality that I require, so far I require two event handlers to be added to the grid (CurrentCellShowingDropDown and CellDrawn). Since I will need these event handlers to check for more than one cell on the grid (I'm going to add multiple copies of this particular dropdown list on the fly as a user needs them), how best should I handle this? I'm thinking that I will need to maintain a list of which cells these operations should be against and then check that list each and every time these events fire. It doesn't seem like an elegant solution to me. Would you happen to have a better idea? Thanks so much for your help Clay! > Something to try. Add the color you want to your combobox cell. > > style.BackColor = Color.Red; > > > Then in a CellDrawn event (requires version 1.6 maybe ??), color the rectangles on either end to be the color you want them to be. >
              
> private void gridControl1_CellDrawn(object sender, GridDrawCellEventArgs e)
> {
> 	if(e.RowIndex == 2 && e.ColIndex == 2)
> 	{
> 		Rectangle rect = e.Bounds;
> 		rect = new Rectangle(rect.Left, rect.Top, 25 , rect.Height );
> 		e.Graphics.FillRectangle(Brushes.LightPink, rect);
> 
> 		rect = e.Bounds;
> 		rect = new Rectangle(rect.Right - 25, rect.Top, 25, rect.Height );
> 		e.Graphics.FillRectangle(Brushes.LightPink, rect);
> 	}
> }
> 


AD Administrator Syncfusion Team August 21, 2003 12:05 PM UTC

I would probably try to avoid the list if possible just to avoid having to maintain it. Instead, maybe everything you need to know is already available in the style. You can get the style in CellDrawn with the e.Style property. In CurrentCellShowingDropDown, you caould get it with code like: GridCurrentCell cc = this.gridControl1.CurrentCell; GridStyleInfo style = this.gridControl1[cc.RowIndex, cc.ColIndex]; So, if you need to test for say the cell style to do certain actions (say checking the bordermargins in CellDrawn to know whether to draw the border rectangles), then you can just do it without having to maintain a list. If you really need to track additional information that is not part of the style, you could actually add it to the style using the style.Tag property. Then you would have access to it in these events as well.

Loader.
Up arrow icon