本帖最后由 bwcigs 于 2009-07-28 17:01:52 编辑

解决方案 »

  1.   

    赋值到剪切板
    Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());
      

  2.   

    我是需要是ctrl c ctrl v按键操作不被e.e.HandledClipboard.SetDataObject(this.dataGridView1.GetClipboardContent());
    请问这句怎么用?
      

  3.   

    DataGridView.ClipboardCopyMode 属性被设定为 DataGridViewClipboardCopyMode.Disable 以外的情况时,「Ctrl + C」 按下的时候,被选择的单元格的内容会拷贝到系统剪切板内。Clipboard.SetDataObject(DataGridView1.GetClipboardContent()) if (DataGridView1.CurrentCell == null)
        return;
    int insertRowIndex = DataGridView1.CurrentCell.RowIndex;
    string pasteText = Clipboard.GetText();
    if (string.IsNullOrEmpty(pasteText))
        return;
    pasteText = pasteText.Replace(" ", " ");
    pasteText = pasteText.Replace(' ', ' ');
    pasteText.TrimEnd(new char[] { ' ' });
    string[] lines = pasteText.Split(' ');
    bool isHeader = true;
    foreach (string line in lines)
    {
        if (isHeader)
        {
            isHeader = false;
            continue;
        }
        string[] vals = line.Split(' ');
        if (vals.Length - 1 != DataGridView1.ColumnCount)
            throw new ApplicationException("粘贴的列数不正确。");
        DataGridViewRow row = DataGridView1.Rows[insertRowIndex];
        row.HeaderCell.Value = vals[0];
        for (int i = 0; i < row.Cells.Count; i++)
        {
            row.Cells[i].Value = vals[i + 1];
        }
        insertRowIndex++;
    }
      

  4.   

    但是现在的问题是我在keypress事件里已经做了如下处理
    private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
            {
               if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }ctrl c ctrl v按下后就e.Handled = true;
      

  5.   

    同意楼上的,你在keypress里面判断一下ctrl+c是否被按下不就行了嘛
      

  6.   

    ctrl c ctrl v的keychar是?
      

  7.   

    查了一下是3和22
    但是问题又来了
    前面在keypress里禁止了非数字字符输入,通过复制却可以。