假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的CellFormatting事件中设置。
下面介绍的是第二种情况下的处理。
举个例子,DataGridView的第4列需要在金额后面加个“元”,在第14列根据1和0显示为相应的是和否,在显示的时候可以这样设置:
下面介绍的是第二种情况下的处理。
举个例子,DataGridView的第4列需要在金额后面加个“元”,在第14列根据1和0显示为相应的是和否,在显示的时候可以这样设置:
private void dgData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 3)
{
if (e.Value != null)
e.Value = e.Value.ToString() + "元";
}
if (e.ColumnIndex == 13)
{
if (e.Value != null)
e.Value = e.Value.ToString() == "1" ? "是" : "否";
}
}
但是在循环行列导出Excel的时候并不会得到格式化后的数据,需要在循环列时判断是哪一列,再调用方法格式化列,如下:
private string FormatRMB(string s)
{
return s + "元";
}
private string FormatStatu(string s)
{
return s == "1" ? "已放款" : "未放款";
}
private void ToExcel(int columnIndex, string fileName)
{
List<int> indexList = dgData.CheckBoxSelect(0);
if (indexList.Count == 0)
{
MessageBox.Show("请先选择!");
retur