DatagridView 常用功能代码
文章是飛鴿傳書轉載的,版權歸原作者所有,作者是:liguangxi8
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);
??????? }
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();
??????????? }
??????? }
?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分");
??????????????? }
??????????? }
??????? }
?4.OpenFileDialog 打開多文件(記得將MultiSelect 這個屬性改為True)
? this.openFileDialog1.Filter = "mydata.dat|*.dat";
? this.openFileDialog1.FileName = "";
? this.openFileDialog1.ShowDialog();
? string[] filenames = this.openFileDialog1.FileNames;
?
飛鴿傳書2.0:http://www.freeeim.com/
總結
以上是生活随笔為你收集整理的DatagridView 常用功能代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PIC单片机精通_串口通信模块C实现
- 下一篇: PIC单片机精通_A/D模数转换模块细节