发布:2023/12/7 15:42:47作者:大数据 来源:大数据 浏览次数:430
设置某单元格字体样式和颜色等
主要通过在重写绘制时,对条件判断,然后设置,具体代码参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex > 0 && e.RowIndex > 0) { int widthFlag = int.Parse(dataGridView2.Rows[e.RowIndex].Cells["ColumnWidthFlag"].Value.ToString()); ; int heightFlag = int.Parse(dataGridView2.Rows[e.RowIndex].Cells["ColumnHeightFlag"].Value.ToString()); if (widthFlag == 1) { dataGridView2.Rows[e.RowIndex].Cells["ColumnWidth"].Style.Font = new Font(DefaultFont, FontStyle.Underline | FontStyle.Bold); dataGridView2.Rows[e.RowIndex].Cells["ColumnWidth"].Style.ForeColor = Color.Red; } if (heightFlag == 1) { dataGridView2.Rows[e.RowIndex].Cells["ColumnHeight"].Style.Font = new Font(DefaultFont, FontStyle.Underline ); dataGridView2.Rows[e.RowIndex].Cells["ColumnHeight"].Style.ForeColor = Color.Red; } } } |
这里主要用到了Font属性及类设置,还用到了DefaultFont系统默认属性
根据dataGridView1.SelectedCells获取选中单元格的rowindex,选中行的index,获取dataGridView1选中单元格的选中行索引
1 2 3 |
var rowIndexes = dataGridView1.SelectedCells.Cast<DataGridViewCell>() .Select(cell => cell.RowIndex) .Distinct(); |
删除选中单元格所在的行
1 2 3 4 5 6 7 8 9 10 |
if (dataGridView1.SelectedCells.Count > 0) { var rowIndexes = dataGridView1.SelectedCells.Cast<DataGridViewCell>().Select(cell => cell.RowIndex) .Distinct().OrderByDescending(o=>o); foreach (var index in rowIndexes) { dataGridView1.Rows.Remove(dataGridView1.Rows[index]); //dataGridView1.Rows.RemoveAt(index); } } |
C# Color 颜色转换 RGB和十六进制 html
1 2 3 4 5 |
Color bgColor= Color.FromArgb(22, 33, 33); string rgbString = string.Join(",", bgColor.R, bgColor.G, bgColor.B); Color color= ColorTranslator.FromHtml("#cc321c"); string colorHtmlString= ColorTranslator.ToHtml(color); |
C#设置Excel单元格填充颜色、字体颜色、单元格背景色
1 2 |
range.Font.Color = ColorTranslator.ToOle(Color.Red); range.Interior.Color= ColorTranslator.ToOle(Color.Green); |
worksheet.Range["A2:K2"]与worksheet.Range["A2","K2"]一个意思,可以选择范围
worksheet.Cells[3,2]获取的是3行2列单元格
冻结窗口,先选择对应的cell,然后冻结窗口,可以通过sheet中Parent属性一直找到Excel对象
1 2 |
worksheet.Range["A4"].Select(); ((worksheet.Parent as Workbook).Parent as Microsoft.Office.Interop.Excel.Application).ActiveWindow.FreezePanes = true; |
DataGridView单元格输入数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == 7) { try { if (!String.IsNullOrEmpty(e.FormattedValue.ToString())) { double val = double.Parse(e.FormattedValue.ToString()); } } catch (Exception ex) { e.Cancel = true; } } } |
C#DataGridViewCheckBoxColumn触发CheckBox值改变事件 DataGridView CheckBox 选中事件状态发生变化解决:
在DataGridView找到CurrentCellDirtyStateChanged事件在此事件添加如下代码,实时根据checkbox选中状态调用数据
1 2 3 4 5 6 7 |
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dataGridView1.IsCurrentCellDirty){ dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); //在此添加相应处理代码 } } |
Excel 单元格加粗及Range选择,这里需要强调的时加粗前需要将单元格或行对象转换成Range对象
第一行加粗
(sheet.Rows[1] as Excel.Range).Font.Bold = true;
第一行一列单元格加粗
(sheet.Cells[1,1] as Excel.Range).Font.Bold = true;
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4