Managing Cell Controls in Worksheets
Accessing Cell Controls
To access and modify an existing cell control in the worksheet, developers can access a specific cell control from the Controls collection of the Worksheet by specifying the cell (using cell name or its location in terms of row and column numbers) in which the cell control is being displayed. Once a cell control is accessed, developers can modify its properties at runtime. For an instance, in the example given below, we have accessed an existing CheckBox cell control from the Worksheet and modified its Checked property.
IMPORTANT: Controls collection contains all types of cell controls in the form of CellControl objects. So, if you need to access a specific type of cell control, say CheckBox then you will have to typecast the CellControl object to CheckBox class.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Getting the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Accessing cell control and typecasting it to CheckBox | |
Aspose.Cells.GridDesktop.CheckBox cb = (Aspose.Cells.GridDesktop.CheckBox)sheet.Controls[cl.Row, cl.Column]; | |
if (cb != null) | |
{ | |
// Modifying the Checked property of CheckBox | |
cb.Checked = true; | |
} | |
else | |
{ | |
MessageBox.Show("Please add control before accessing it."); | |
} |
Removing Cell Controls
To remove an existing cell control, developers can simply access a desired worksheet and then Remove the cell control from the Controls collection of the Worksheet by specifying the cell (using its name or row & column number) containing cell control.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Getting the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Removing the cell control by specifying the location of cell containing it | |
sheet.Controls.Remove(cl.Row, cl.Column); |