DataGridView 複製與貼上

參考 取之於網路用之於網路 http://rely1020.blog.ithome.com.tw/post/1606/107293

  • DataGridView預設己有Ctrl-C 複製入剪貼簿之功能
  • 相關之events
    • KeyUp
      • 接受Ctrl-V
    • ClipboardCopyMode
      • DataGridViewClipboardCopyMode.EnableWithAutoHeaderText  or  .EnableWithoutHeaderText
      • 設定好後就能接收Ctrl-C
    • SelectedCells
      • 被選取之Cells
    • OwningColumn
      • 某Cell所屬之 Column
    • DataError
      • 有人建議就bypass, 有人建議先檢查data type
  • 剪貼簿文字模式(text)
    • 每Row為新行
    • 每Row之Columns以Tab分隔
    • 貼入前檢查row/column之數量
  • 參考範例
   1:  private void dgvData_KeyUp(object sender, KeyEventArgs e)
   2:  {
   3:      //##  + V
   4:      if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
   5:      {
   6:          try
   7:          {
   8:              // UI控制-mouse
   9:              this.Cursor = Cursors.WaitCursor;
  10:   
  11:              //# 自 Clipboard 取值
  12:              string copyText = Clipboard.GetText(TextDataFormat.Text);
  13:   
  14:              //# by each selected cells to proceed...
  15:              foreach (DataGridViewCell c in dgvData.SelectedCells)
  16:              {
  17:                  switch (c.OwningColumn.Name)
  18:                  {
  19:                      case "bdg_tpe": // 預算別
  20:                          string bdg_tpe;
  21:                          // 若不是合法的資料,離開。
  22:                          if (!this.DoParse_BdgTpe(copyText, out bdg_tpe)) return;
  23:                          // 貼上合法的資料
  24:                          c.Value = bdg_tpe;
  25:                          break;
  26:                      case "tckt_num": // 傳票號碼
  27:                          //......
  28:                          break;
  29:                  }
  30:              }
  31:          }
  32:          finally
  33:          {
  34:              // UI控制-mouse
  35:              this.Cursor = Cursors.Default;
  36:          }
  37:   
  38:      }
  39:      else if (e.KeyCode == Keys.Delete) // 
  40:      {
  41:          try
  42:          {
  43:              // UI控制-mouse
  44:              this.Cursor = Cursors.WaitCursor;
  45:   
  46:              //# by each selected cells to proceed...
  47:              foreach (DataGridViewCell c in dgvData.SelectedCells)
  48:              {
  49:                  switch (c.OwningColumn.Name)
  50:                  {
  51:                      case "bdg_tpe": // 預算別
  52:                      //......                    
  53:                          break;
  54:                  }
  55:              }
  56:          }
  57:          finally
  58:          {
  59:              // UI控制-mouse
  60:              this.Cursor = Cursors.Default;
  61:          }
  62:      }
  63:  }
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: