DatagridView 常用功能代码
1.DatagridView自动编号
代码
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//自动编号与数据库无关
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4,e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
{
//自动编号与数据库无关
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4,e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
2.DatagridView 导出数据到Excel
代码
private void btnExport_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.FileNames.Length == 0)
{
MessageBox.Show("请先选择数据源!");
return;
}
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FileName = "mydata";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
saveFileDialog.ShowDialog();
Stream myStream;
try
{
myStream = saveFileDialog.OpenFile();
}
catch
{
return;
}
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
{
if (this.openFileDialog1.FileNames.Length == 0)
{
MessageBox.Show("请先选择数据源!");
return;
}
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FileName = "mydata";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
saveFileDialog.ShowDialog();
Stream myStream;
try
{
myStream = saveFileDialog.OpenFile();
}
catch
{
return;
}
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
3.DataGridView格式化日期
代码
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["PriceDateTime"].Index)
{
if (e.Value != null)
{
e.Value = Convert.ToDateTime(e.Value).ToString("yyyy年MM月dd日 hh时mm分");
}
}
}
{
if (e.ColumnIndex == dataGridView1.Columns["PriceDateTime"].Index)
{
if (e.Value != null)
{
e.Value = Convert.ToDateTime(e.Value).ToString("yyyy年MM月dd日 hh时mm分");
}
}
}
4.OpenFileDialog 打开多文件(记得将MultiSelect 这个属性改为True)
this.openFileDialog1.Filter = "mydata.dat|*.dat";
this.openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
string[] filenames = this.openFileDialog1.FileNames;
this.openFileDialog1.FileName = "";
this.openFileDialog1.ShowDialog();
string[] filenames = this.openFileDialog1.FileNames;