Skip to content

Commit 126ca01

Browse files
How to add the RadioButton column in WinForms DataGrid (SfDataGrid)?
How to add the RadioButton column in WinForms DataGrid (SfDataGrid)?
1 parent 4f721ec commit 126ca01

File tree

1 file changed

+202
-2
lines changed

1 file changed

+202
-2
lines changed

README.md

Lines changed: 202 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,202 @@
1-
# how-to-add-the-radio-button-column-in-winforms-datagrid
2-
how to add the radio button column in winforms datagrid?
1+
# How to add the RadioButton column in WinForms DataGrid (SfDataGrid)?
2+
3+
## About the sample
4+
This example illustrates how to add the radio button column in winforms datagrid
5+
6+
By default, SfDataGrid doesn’t have a build in RadioButtonColumn, but we can create the GridRadioButtonColumn by customizing the GridColumn, RadioButtonAdv and GridCellRendererBase in SfDataGrid.
7+
8+
```C#
9+
[ToolboxItem(false)]
10+
public partial class GridCellRadioButton : RadioButtonAdv
11+
{
12+
public GridCellRadioButton()
13+
{
14+
}
15+
16+
/// <summary>
17+
/// Draws check mark of the radio button.
18+
/// </summary>
19+
/// <param name="g">Graphics object</param>
20+
public void DrawSelectedCheckMark(Graphics g, Rectangle mrectBox, RadioButtonAdv radio, Color circleBackColor, Color iconColor)
21+
{
22+
Rectangle rect = mrectBox;
23+
rect.Inflate((int)DpiAware.LogicalToDeviceUnits(-5), (int)DpiAware.LogicalToDeviceUnits(-5));
24+
25+
using (Pen pen = new Pen(circleBackColor))
26+
{
27+
g.DrawEllipse(pen, rect);
28+
}
29+
30+
rect.Inflate(-1, -1);
31+
32+
using (GraphicsPath path = this.GetCheckMarkPath(rect))
33+
{
34+
using (PathGradientBrush brush = new PathGradientBrush(path))
35+
{
36+
brush.CenterColor = iconColor;
37+
brush.CenterPoint = new PointF((float)rect.X + 1, (float)rect.Y + 1);
38+
brush.SurroundColors = new Color[] { iconColor };
39+
SmoothingMode mode = SmoothingMode.AntiAlias;
40+
g.SmoothingMode = mode;
41+
g.FillPath(brush, path);
42+
}
43+
}
44+
45+
using (GraphicsPath path = this.GetCheckMarkBorderPath(rect))
46+
{
47+
using (Pen pen = new Pen(iconColor))
48+
{
49+
g.DrawPath(pen, path);
50+
}
51+
}
52+
}
53+
54+
internal GraphicsPath GetCheckMarkBorderPath(Rectangle rect)
55+
{
56+
GraphicsPath path = new GraphicsPath();
57+
58+
#region Fortouch
59+
path.AddEllipse(rect);
60+
path.CloseFigure();
61+
#endregion
62+
return path;
63+
}
64+
65+
internal GraphicsPath GetCheckMarkPath(Rectangle rect)
66+
{
67+
GraphicsPath path = new GraphicsPath();
68+
path.AddEllipse(rect);
69+
path.CloseFigure();
70+
return path;
71+
}
72+
73+
public void DrawBorder(Graphics g, Color borderColor, Rectangle mrectBox, RadioButtonAdv radioButtonAdv)
74+
{
75+
Rectangle rect = mrectBox;
76+
rect.Inflate(-1, -1);
77+
g.SmoothingMode = SmoothingMode.AntiAlias;
78+
g.CompositingQuality = CompositingQuality.AssumeLinear;
79+
80+
using (Pen pen = new Pen(borderColor))
81+
{
82+
pen.Width = 1;
83+
g.DrawEllipse(pen, rect);
84+
}
85+
}
86+
}
87+
```
88+
89+
```C#
90+
public class GridRadioButtonCellRender : GridCellRendererBase
91+
{
92+
public GridRadioButtonCellRender(SfDataGrid dataGrid, Options options)
93+
{
94+
IsEditable = true;
95+
DataGrid = dataGrid;
96+
RadioOptions = options;
97+
}
98+
99+
protected Options RadioOptions { get; set; }
100+
101+
protected List<GridCellRadioButton> RadioButtonCollection { get; set; }
102+
103+
/// <summary>
104+
/// Gets or Sets to specifies the datagrid.
105+
/// </summary>
106+
protected SfDataGrid DataGrid { get; set; }
107+
108+
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
109+
{
110+
int starHeight, starWidth;
111+
Rectangle drawArea;
112+
113+
var padding = 5;
114+
starWidth = 16;
115+
starHeight = 16;
116+
117+
var RadioButtonColumn = column.GridColumn as GridRadioButtonColumn;
118+
drawArea = new Rectangle(cellRect.X + padding, cellRect.Y + ((cellRect.Height / 2) - (starHeight / 2)), starWidth, starHeight);
119+
120+
RadioButtonCollection = new List<GridCellRadioButton>();
121+
for (int i = 0; i < RadioButtonColumn.ItemCount; i++)
122+
{
123+
var radioButton = new GridCellRadioButton();
124+
125+
radioButton.Location = new Point(drawArea.X, drawArea.Y);
126+
radioButton.Width = starWidth;
127+
radioButton.Height = starHeight;
128+
129+
//Draw outer border of RadioButton
130+
radioButton.DrawBorder(paint, Color.Black, drawArea, radioButton);
131+
132+
Point point = new Point(drawArea.X + drawArea.Width + 2, drawArea.Y);
133+
Font font = style.GetFont() != Control.DefaultFont ? style.GetFont() : Control.DefaultFont;
134+
135+
int value = i;
136+
var buttonValue = ((Options)value).ToString();
137+
138+
//Draw string for RadioButton
139+
paint.DrawString(buttonValue, font, new SolidBrush(Color.Gray), point);
140+
if (buttonValue.Equals(cellValue))
141+
{
142+
radioButton.DrawSelectedCheckMark(paint, drawArea, radioButton, Color.Black, Color.Black);
143+
}
144+
//Add RadioButton to Cell
145+
RadioButtonCollection.Add(radioButton);
146+
147+
//Set the start point for next RadioButton
148+
Size stringlenth = TextRenderer.MeasureText((RadioOptions = 0).ToString(), font);
149+
drawArea.X += drawArea.Width + 10 + stringlenth.Width;
150+
}
151+
}
152+
153+
protected override void OnMouseDown(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e)
154+
{
155+
var radiobuttoncollection = (dataColumn.Renderer as GridRadioButtonCellRender).RadioButtonCollection;
156+
PropertyInfo dataRow = (dataColumn as DataColumnBase).GetType().GetProperty("DataRow", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
157+
DataRow rowdata = (DataRow)dataRow.GetValue(dataColumn as DataColumnBase);
158+
159+
for (int i = 0; i < radiobuttoncollection.Count; i++)
160+
{
161+
var rect = radiobuttoncollection[i].Bounds;
162+
if (e.Location.X > rect.X && e.Location.X < (rect.X + rect.Width))
163+
{
164+
radiobuttoncollection[i].Checked = true;
165+
(rowdata.RowData as OrderInfo).RadioOptions = (Options)i;
166+
167+
DataGrid.TableControl.Invalidate(rect, true);
168+
169+
}
170+
}
171+
}
172+
}
173+
```
174+
175+
```C#
176+
public class GridRadioButtonColumn : GridColumn
177+
{
178+
public GridRadioButtonColumn()
179+
{
180+
SetCellType("RadioButton");
181+
}
182+
183+
private int itemCount;
184+
public int ItemCount
185+
{
186+
get
187+
{
188+
return itemCount;
189+
}
190+
set
191+
{
192+
itemCount = value;
193+
}
194+
}
195+
}
196+
```
197+
```C#
198+
this.sfDataGrid1.Columns.Add(new GridRadioButtonColumn() { MappingName = "RadioOptions", ItemCount = 3, Width = 140 });
199+
```
200+
201+
## Requirements to run the demo
202+
Visual Studio 2015 and above versions

0 commit comments

Comments
 (0)