DatagridView 常用功能代碼

來源:互聯網
上載者:User

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;

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.