Posted 20 February 2018, 6:05 am EST
Hi Saleem,
She is right.[quote=“meenakshi.singh”]
if you want to use unchecked CheckBox for false as well as DBNull.Value then the only way seems to change DBNull.Value to false at back-end. Both the values are treated differently while painting on the control.
[/quote]
However, I found a solution for you.
You can host a CheckBox control in C1FlexGridClassic for achieving your requirement if you want and change the cell data accordingly.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(int)), new DataColumn("InStock", typeof(Boolean))});
for (int i = 1; i < 11; i++)
{
dt.Rows.Add(new object[] { 1, "True" });
dt.Rows.Add(new object[] { 2, "False" });
dt.Rows.Add(new object[] { 3, DBNull.Value });
}
c1FlexGridClassic1.Paint += C1FlexGridClassic1_Paint;
c1FlexGridClassic1.DrawMode = DrawModeEnum.OwnerDraw;
c1FlexGridClassic1.OwnerDrawCell += C1FlexGridClassic1_OwnerDrawCell;
c1FlexGridClassic1.DataSource = dt;
c1FlexGridClassic1.AllowUserResizing = AllowUserResizeSettings.flexResizeBoth;
}
private void C1FlexGridClassic1_Paint(object sender, PaintEventArgs e)
{
foreach (CheckBox cb in c1FlexGridClassic1.Controls)
{
RowColumn rowColumn = cb.Tag as RowColumn;
Rectangle rg = c1FlexGridClassic1.GetCellRect(rowColumn.Row, rowColumn.Col);
cb.Size = new Size(rg.Size.Width - 2, rg.Size.Height - 2);
cb.Location = new Point(rg.Location.X + 1, rg.Location.Y + 1);
}
}
private void C1FlexGridClassic1_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
{
if (e.Col == 2)
{
Type type = c1FlexGridClassic1.GetData(e.Row, e.Col)?.GetType();
if (type?.Name == "DBNull")
{
foreach (CheckBox cb1 in c1FlexGridClassic1.Controls)
{
if (cb1.Name == e.Row + "" + e.Col)
{
return;
}
}
CheckBox cb = new CheckBox();
cb.Name = e.Row + "" + e.Col;
cb.Click += Cb_Click;
cb.Size = new Size(e.Bounds.Size.Width - 2, e.Bounds.Size.Height - 2);
cb.Location = new Point(e.Bounds.Location.X + 1, e.Bounds.Location.Y + 1);
cb.CheckAlign = ContentAlignment.MiddleCenter;
cb.Tag = new RowColumn()
{
Row = e.Row,
Col = e.Col
};
c1FlexGridClassic1.Controls.Add(cb);
}
}
}
private void Cb_Click(object sender, EventArgs e)
{
RowColumn rowColumn = (sender as CheckBox).Tag as RowColumn;
c1FlexGridClassic1.Select(rowColumn.Row, rowColumn.Col);
c1FlexGridClassic1.SetData(rowColumn.Row, rowColumn.Col, (sender as CheckBox).Checked);
//Uncomment this line if you want to remove this checkbox for next occurence
//c1FlexGridClassic1.Controls.Remove((sender as CheckBox));
c1FlexGridClassic1.Invalidate();
}
}
public class RowColumn
{
public int Row { get; set; }
public int Col { get; set; }
}
Thanks,
Singh