Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement CheckBox cell type for entire column. #515

Open
kpenigar opened this issue Jan 3, 2024 · 6 comments
Open

How to implement CheckBox cell type for entire column. #515

kpenigar opened this issue Jan 3, 2024 · 6 comments
Labels

Comments

@kpenigar
Copy link

kpenigar commented Jan 3, 2024

I am unable to get the checkbox cell type to persist for an entire column.

case (int)Headers.IsOpen:
  SetColumnWidth(colIndex, 1);
  var header = Grid.CurrentWorksheet.ColumnHeaders[colIndex];
  header.DefaultCellBody = typeof(CheckBoxCell);
  header.Style.HorizontalAlign = ReoGridHorAlign.Center;
  break;
@jingwood
Copy link
Member

jingwood commented Jan 4, 2024

Hi @kpenigar. Thanks for your donation! Could you please provide more details about your issue?

I understand that you want to display checkboxes in all cells of a specific column. Your approach is on the right track. After setting DefaultCellBody to use checkboxes, you should assign a boolean value (true or false) to each cell in the column. Here's an example:

int dataCount = 10; // Number of rows
for (int i = 0; i < dataCount; i++)
{
  sheet[i, 0] = false; // Assigns false to each cell in the first column
}

This code will populate the first column with unchecked checkboxes for the first 10 rows. Please adjust dataCount based on the actual number of rows in your sheet.

The result:
image

@kpenigar
Copy link
Author

kpenigar commented Jan 4, 2024

Thanks for the quick response.

Context: I pull data from a database into the spreadsheet and then configure the spreadsheet, so I don't assign the default body style until all the data is loaded. The "IsOpen" field has a data type of Bit (0, 1).

@jingwood
Copy link
Member

jingwood commented Jan 7, 2024

Did my answer resolve your question? Given your context, here is another example:

// Assuming `sheet` is the current worksheet and data is loaded
int columnIndexOfIsOpen = [Column Index of IsOpen]; // Replace with actual column index
for (int i = 0; i < sheet.RowCount; i++)
{
    object dbValue = sheet[i, columnIndexOfIsOpen]; // Get value from cell
    bool checkboxValue = Convert.ToBoolean(dbValue); // Convert DB Bit value to boolean
    sheet[i, columnIndexOfIsOpen] = checkboxValue; // Assign boolean value to cell
    // Optionally, set the cell body to CheckBoxCell if not already done
    sheet.SetCellBody(i, columnIndexOfIsOpen, new CheckBoxCell());
}

In this example you don't have to use Header.DefaultCellBody field, just set each checkbox into cells manually.

@kpenigar
Copy link
Author

kpenigar commented Jan 8, 2024

Ok, I'll try to implement your suggestion and let you know.

Thanks again for your help.

@jingwood
Copy link
Member

jingwood commented Jan 8, 2024

Yes, when you use Header.DefaultCellBody, you don't have to set checkbox instances. Instead, if you set a boolean value to true or false, ReoGrid will automatically create the checkbox instance for you. Both methods achieve the same result.

@kpenigar
Copy link
Author

kpenigar commented Jan 8, 2024

So I guess it probably would be more efficient if I did the data conversion on the server side, that way when the data's loaded it's already in the right format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants