在 `DataGridView` 中:
微軟官方文檔提供了完整示例:
DataGridView 控制項 - 自定義格式化與統計行(中文)
dataGridView1.CellFormatting += (s, e) => {
if (e.ColumnIndex == 2 && e.Value != null) {
int val = Convert.ToInt32(e.Value);
e.CellStyle.ForeColor = val < 0 ? Color.Red : Color.Black;
}
};
// 綁定數據後,手動添加統計行
var dt = (DataTable)dataGridView1.DataSource;
DataRow sumRow = dt.NewRow();
sumRow["Name"] = "合計";
sumRow["Amount"] = dt.Compute("SUM(Amount)", "");
dt.Rows.Add(sumRow);
// 設置該行樣式
int lastRow = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[lastRow].DefaultCellStyle.ForeColor = Color.Blue;
dataGridView1.Rows[lastRow].DefaultCellStyle.Font = new Font("微軟雅黑", 9, FontStyle.Bold);
直接閱讀官方文檔(上方鏈接)可獲得最權威、最完整的示例與 API 說明,涵蓋虛擬模式、大數據優化、導出 Excel 等進階主題。