RadioButton column in System.Windows.Forms.DataGrid

G

Guest

Good day!

Here is my problem: I need to have a radiobutton column in my DataGrid for a
representation of a bool column, where only row can be checked at one
time(only one value in bool column can be true). So I've created a class
inheriting from DataGridColumnStyle and added the following code to the class:

protected RadioButton [] rb ;

....
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
if(Convert.ToBoolean(GetColumnValueAtRow(source, rowNum)))
rb[rowNum].Checked = true;
else
rb[rowNum].Checked = false;
rb[rowNum].Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if(value != null)
{
if(value.DataSource!=null)
{
CurrencyManager source =
(CurrencyManager)value.BindingContext[value.DataSource];
rb = new RadioButton[source.Count];
for(int i=0;i<source.Count;i++)
{
rb = new RadioButton();
rb.Visible = true;
rb.CheckedChanged+=new EventHandler(RadioButtonValueChanged);
value.Controls.Add(rb);
}
}
}
}

private void RadioButtonValueChanged(object sender, EventArgs e)
{
RadioButton rb1 = (RadioButton)sender;
CurrencyManager source =
(CurrencyManager)((DataGrid)rb1.Parent).BindingContext[((DataGrid)rb1.Parent).DataSource];
for(int i=0; i< source.Count; i++)
{
source.Position = i;
if(rb1.Equals(rb))
SetColumnValueAtRow(source, i , true);
else
SetColumnValueAtRow(source, i, false);
}
}


So here I've faced some problems:
1. when I sort the datagrid, the checked radiobutton doesn't change it's
positions, seems like CurrencyManager stays the same.
2. when I use dataview with some rowfilter as a source for my datagrid,
checking radiobutton constanly throws Out of Index exception.
3. do I really have to change source.position in cycle(otherwise, it
throws an exception)?

Thanks in advance.
 
G

Guest

Found out solutions to most of my problems, except for one: after I click on
the column header of DatGrid control, Sort property of the DataView I'm using
as a source for a DataGrid doesn't seem to change :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top