1、鼠标划过违章次数列,显示为手型
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
int colNum = e.ColumnIndex;
if (colNum == 4)
{
this.dataGridView1.Cursor = Cursors.Hand;
}
else {
this.dataGridView1.Cursor = Cursors.Default;
}
}
2、设置列样式
this.dataGridView1.Columns[1].Width = 250;
this.dataGridView1.Columns[4].DefaultCellStyle.BackColor = Color.AliceBlue;
3、//添加下划线
this.dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(Font.Name, Font.Size, FontStyle.Underline);
4、导出Excel
//导出Excel
private void button3_Click(object sender, EventArgs e)
{
string fileName = "郑州市运输管理中心违章车辆信息.xls";
sh.exportExcel(fileName, this.dataGridView1);
}
//导出excel
public void exportExcel(string fn, DataGridView dv)
{
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "将查询结果导出成EXCEL文件";
sf.DefaultExt = "xls";
sf.FileName = fn;
sf.Filter = "Excel文件(*.xls)|*.xls";
if (sf.ShowDialog() == DialogResult.OK)
{
string sFileName = sf.FileName;
if (File.Exists(sFileName))
{
File.Delete(sFileName);
}
FileStream objFileStream;
StreamWriter objStreamWrite;
string strLine = "";
objFileStream = new FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWrite = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int i = 0; i < dv.Columns.Count; i++)
{
if (dv.Columns[i].Visible == true)
{
if (!dv.Columns[i].Name.Contains("未对应信息"))
{
strLine = strLine + dv.Columns[i].HeaderText.ToString() + Convert.ToChar(9);
}
}
}
objStreamWrite.WriteLine(strLine);
strLine = "";
for (int i = 0; i < dv.Rows.Count; i++)
{
if (dv.Columns[0].Visible == true)
{
if (dv.Rows[i].Cells[0].Value == null)
{
strLine = strLine + " " + Convert.ToChar(9);
}
else
{
strLine = strLine + dv.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9);
}
for (int j = 1; j < dv.Columns.Count; j++)
{
if (dv.Columns[j].Visible == true)
{
if (!dv.Columns[j].Name.Contains("未对应信息"))
{
if (dv.Rows[i].Cells[j].Value == null)
{
strLine = strLine + " " + Convert.ToChar(9);
}
else
{
string rowstr = "";
rowstr = dv.Rows[i].Cells[j].Value.ToString();
if (rowstr.IndexOf("\r\n") > 0)
{
rowstr = rowstr.Replace("\r\n", " ");
}
if (rowstr.IndexOf("\t") > 0)
{
rowstr = rowstr.Replace("\t", " ");
}
strLine = strLine + rowstr + Convert.ToChar(9);
}
}
}
}
objStreamWrite.WriteLine(strLine);
strLine = "";
}
}
objStreamWrite.Close();
objFileStream.Close();
MessageBox.Show("导出Excel成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}