日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DatagridView 常用功能代码

發布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 常用功能代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。