BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
I am able to Load/Bound the Data to the sfdatagrid to a Datatable, but the issue is that i want to add a Checkbox Column in the start of the grid and user can select multiple records but the checkbox must be checked only when the checkbox cell was clicked else it should not be checked/unchked.
I have a Data Entry Form where user fills records in several textbox/combo boxes and when user saves it it should be added in the datagrid (which will also be saved in the backend table). now when user clicks on any row the selected row value is need to be filled in the form controls (to edit that record). and that form has a generate button which updates status of the records in the table. so if user clickes on checkboxcell then only it must be checked/uncheked.
How do i Get if the checkbox cell is checked in current selected item. below is the code where i am struggling.
foreach (var intRow1 in sfdgBilties.SelectedItems)
{
var dr1 = (intRow1 as DataRowView).Row;
//var CellValue = dr1["IsSelected"].ToString();
bool isSelected=false; //= Convert.ToBoolean(dr1["IsSelected"].ToString());
GridColumn col = sfdgBilties.Columns["IsSelected"];
var record = dr1;
var column = this.sfdgBilties.Columns["IsSelected"];
this.sfdgBilties.SelectCell(record, column);
var currentCellValue = sfdgBilties.CurrentCell(column).CellRenderer.GetControlValue();
if ((bool)dr1["IsSelected"]==true)
{
SelectedCount += 1;
isSelected = true;
}
if (isSelected)
{
SelectedCount += 1;
}
}
if (SelectedCount>0)
{
if (strtablename == "tmpBillty")
{
}
else
{
foreach (var item in sfdgBilties.SelectedItems)
{
var dr1 = (item as DataRowView).Row;
bool isSelected = false;// = Convert.ToBoolean(dr1["IsSelected"].ToString());
if ((bool)dr1["IsSelected"] == true)
{
isSelected = true;
}
if (isSelected)
{
if (ValidateGridForBlank(dr1)==false)
{
dr1["IsSelected"] = false;
return;
}
}
}
SaveDataNew("Bilty");
btnGenerate.Enabled = true;
EnableDisableButtons(false);
EnableDisableCtrls(false);
BlankControls();
BlankVariables();
}
}
Hi Ravindra Kumar Gautam,
We apologize for any inconvenience caused. Unfortunately, we do not have an UnBoundCheckBox column in SfDataGrid. However, we do have an UnBoundCheckBoxSelector column that can be used to select rows when any column is selected. We understand that this may not be suitable for your specific requirement.
https://help.syncfusion.com/windowsforms/datagrid/columntypes#gridcheckboxselectorcolumn
We want to assure you that we are actively exploring the feasibility of implementing a solution to meet your specific requirement. We will provide further updates on April 24, 2023. Thank you for your patience and understanding.
Regards,
Sathiyathanam
Ravindra Kumar Gautam,
Unfortunately, there is no direct support for the GridUnBoundCheckBoxColumn in our current setup. This is because we need to store the checkbox state in a property before we can modify it during runtime. However, we have prepared a workaround for the GridUnBoundCheckBoxColumn.
You can now create a GridUnBoundCheckBoxColumn that is not bound to the data object from the underlying DataSource. It can be added to the datagrid like a normal column. The checkbox state will only be updated if we interact with it. Please refer to the code snippet below for more information:
Custom Column
public class GridUnBounCheckBoxColumn : GridColumn { public GridUnBounCheckBoxColumn() { SetCellType("UnBoundCheckBox");
}
}
|
Custom Renderer
public class GridUnBounCheckBoxColumnRenderer : GridCellRendererBase {
public GridUnBounCheckBoxColumnRenderer(SfDataGrid dataGrid) { DataGrid = dataGrid; IsEditable = false; } /// <summary> /// CheckedSateKeyValuePairs is used to hold the rowindex and checkBox States. /// </summary> public Dictionary<int, bool> CheckedSateKeyValuePairs = new Dictionary<int, bool>(); protected SfDataGrid DataGrid { get; set; }
internal bool IsCheckBoxHovered { get; set; }
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) { style.HorizontalAlignment = HorizontalAlignment.Center; var clipBounds = paint.ClipBounds; Assembly assembly = Assembly.GetAssembly(typeof(SfDataGrid)); var cellRendererType = assembly.GetType("Syncfusion.WinForms.DataGrid.Renderers.GridCellRendererBase"); var setClipBounds = cellRendererType.GetMethod("SetClipBounds", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); setClipBounds.Invoke(null, new object[] { column, paint, cellRect });
var selectorColumn = column.GridColumn as GridCheckBoxSelectorColumn; var drawingHelperType = assembly.GetType("Syncfusion.WinForms.DataGrid.Helpers.DrawingHelper"); var paintCellBackground = drawingHelperType.GetMethod("PaintCellBackground", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); paintCellBackground.Invoke(null, new object[] { paint, cellRect, style, true });
CheckBoxCellStyleInfo checkBoxStyle = new CheckBoxCellStyleInfo(); checkBoxStyle.ModifyStyle(this.DataGrid.Style.CheckBoxStyle, StyleModifyType.Override); // Gets the bounds of the check box. var type = assembly.GetType("Syncfusion.WinForms.DataGrid.CheckBoxPainter"); var method = type.GetMethod("GetCheckBoxBounds", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); Rectangle checkBoxBounds = (Rectangle)method.Invoke(null, new object[] { this.DataGrid.TableControl, cellRect, null }); checkBoxBounds.X += ((cellRect.Width / 2) - (checkBoxBounds.Width / 2)) - 5;
if (!CheckedSateKeyValuePairs.ContainsKey(rowColumnIndex.RowIndex)) { CheckedSateKeyValuePairs.Add(rowColumnIndex.RowIndex, false); } CheckState checkBoxState = CheckedSateKeyValuePairs[rowColumnIndex.RowIndex] ? CheckState.Checked : CheckState.Unchecked; if (checkBoxBounds.Width <= cellRect.Width && checkBoxBounds.Height <= cellRect.Height) { var drawCheckBox = type.GetMethod("DrawCheckBox", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); drawCheckBox.Invoke(null, new object[] { paint, checkBoxBounds, checkBoxState, checkBoxStyle, IsCheckBoxHovered }); } paint.SetClip(clipBounds); }
protected override void OnMouseUp(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e) { VisibleLineInfo columnInfo = this.TableControl.ScrollColumns.GetVisibleLineAtLineIndex(rowColumnIndex.ColumnIndex); Rectangle cellRectangle = this.TableControl.GetCellRectangle(rowColumnIndex.RowIndex, rowColumnIndex.ColumnIndex, columnInfo.IsClipped); GridCheckBoxColumn checkBoxColumn = dataColumn.GridColumn as GridCheckBoxColumn;
Assembly assembly = Assembly.GetAssembly(typeof(SfDataGrid)); var type = assembly.GetType("Syncfusion.WinForms.DataGrid.CheckBoxPainter"); var method = type.GetMethod("GetCheckBoxBounds", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); Rectangle checkBoxBounds = (Rectangle)method.Invoke(null, new object[] { this.DataGrid.TableControl, cellRectangle, null }); checkBoxBounds.X += ((cellRectangle.Width / 2) - (checkBoxBounds.Width / 2)) - 5; if (checkBoxBounds.Contains(e.Location) && dataColumn.GridColumn.AllowEditing) { CheckedSateKeyValuePairs[rowColumnIndex.RowIndex] = !CheckedSateKeyValuePairs[rowColumnIndex.RowIndex]; } base.OnMouseUp(dataColumn, rowColumnIndex, e); } } |
public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen;
this.sfDataGrid1.CellRenderers.Add("UnBoundCheckBox", new GridUnBounCheckBoxColumnRenderer(this.sfDataGrid1)); this.sfDataGrid1.AutoGenerateColumns = false; var table = this.GetDataTable(); sfDataGrid1.DataSource = table; this.sfDataGrid1.SelectionMode = GridSelectionMode.Multiple;
this.sfDataGrid1.Columns.Add(new GridUnBounCheckBoxColumn() { MappingName = "IsSelected", HeaderText = "Is Selected" }); this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "OrderID", HeaderText = "Order ID" }); this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "CustomerID", HeaderText = "Customer ID", ColumnMemberType = typeof(string) }); this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "CustomerName", HeaderText = "Customer Name" }); this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "Country", HeaderText = "Country" }); this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "ShipCity", HeaderText = "Ship City" }); this.sfDataGrid1.SelectionChanged += SfDataGrid1_SelectionChanged;
} |
You can retrieve the state of the CheckBox from the related information of the selected rows in SfDataGrid.SelectedItems.
var checkedKeyValuePairs = (this.sfDataGrid1.CellRenderers["UnBoundCheckBox"] as GridUnBounCheckBoxColumnRenderer).CheckedSateKeyValuePairs;
foreach (var item in this.sfDataGrid1.SelectedItems) { int rowIndex = this.sfDataGrid1.TableControl.ResolveToRowIndex(item); if(rowIndex > 0) { // The CheckBox state is getting for Selected Rows by using the CheckedSateKeyValuePairs. bool isSelected = checkedKeyValuePairs[rowIndex]; }
} |
Please review the attached sample and inform us if you require any additional assistance regarding this matter.