2010-01-18 11 views
9

मैं एक सी # विंडोज़ एप्लिकेशन पर काम कर रहा हूं, और डेटाग्रिड व्यू में एक पंक्ति कॉपी करना और इसे एक नई पंक्ति में पेस्ट करना चाहता हूं। मैं इसे कैसे प्राप्त कर सकता हूं? मैं नेट फ्रेमवर्क 3.5 का उपयोग कर रहा हूँ।डेटाग्रिडव्यू (विंडोज़ एप्लिकेशन) में पंक्ति प्रतिलिपि/पेस्ट कार्यक्षमता

क्या आप मुझे कुछ विचार या कुछ कोड प्रदान कर सकते हैं जो इंगित करेंगे कि मैं इसे कैसे प्राप्त कर सकता हूं?

उत्तर

2

डेटाग्रिड व्यूरो क्लास में एक। क्लोन विधि है जो वर्तमान पंक्ति को क्लोन कर देगी।

अधिक जानकारी के लिए एक नज़र here

+0

मैंने इसे चेक किया। लेकिन जब मैं इसे ग्रिडव्यू की नई पंक्ति में पेस्ट करता हूं तो डेटा कॉपी करने के बाद। यह पिछली पंक्ति के डेटा पेस्ट नहीं करता है, जबकि जब मैं एक टेक्स्टबॉक्स में पेस्ट करता हूं .. ऐसा होता है। मैं डेटाग्रिड व्यू के फ़ंक्शन के बाद बस पेस्ट msdn कोड फ़ंक्शन कॉपी करता हूं। – Programmer

7

मैं एक post कि DataGridView में क्लिपबोर्ड से मूल्यों को चिपकाने के लिए कोड शामिल पाया है लो।

मैं क्लिपबोर्ड से सी # में DataGridView को चिपकाने के लिए कैसे, एक जानकारी, एक्सेल से नकल googling गया था, और पूरा जवाब नहीं मिल रहा था। मंचों से धागे के एकत्रित जोड़े और के साथ इस उत्तर के साथ आया, उम्मीद है कि यह कुछ लोगों को आसान बना देगा। आप करने के लिए है कोड को समझने बस कॉपी और पेस्ट

नीचे थोड़ा संशोधित संस्करण है न। छोटे रिफैक्टरिंग से परे मैं केवल पढ़ने के लिए कोशिकाओं में पेस्ट मना कर देता हूं।

प्रयोग उदाहरण:

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
{ 
    ClipboardUtils.OnDataGridViewPaste(sender, e); 
} 

कोड:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 

namespace Commons 
{ 
    public class ClipboardUtils 
    { 
     public static void OnDataGridViewPaste(object grid, KeyEventArgs e) 
     { 
      if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V)) 
      { 
       PasteTSV((DataGridView)grid); 
      } 
     } 

     public static void PasteTSV(DataGridView grid) 
     { 
      char[] rowSplitter = { '\r', '\n' }; 
      char[] columnSplitter = { '\t' }; 

      // Get the text from clipboard 
      IDataObject dataInClipboard = Clipboard.GetDataObject(); 
      string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text); 

      // Split it into lines 
      string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries); 

      // Get the row and column of selected cell in grid 
      int r = grid.SelectedCells[0].RowIndex; 
      int c = grid.SelectedCells[0].ColumnIndex; 

      // Add rows into grid to fit clipboard lines 
      if (grid.Rows.Count < (r + rowsInClipboard.Length)) 
      { 
       grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count); 
      } 

      // Loop through the lines, split them into cells and place the values in the corresponding cell. 
      for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++) 
      { 
       // Split row into cell values 
       string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter); 

       // Cycle through cell values 
       for (int iCol = 0; iCol < valuesInRow.Length; iCol++) 
       { 

        // Assign cell value, only if it within columns of the grid 
        if (grid.ColumnCount - 1 >= c + iCol) 
        { 
         DataGridViewCell cell = grid.Rows[r + iRow].Cells[c + iCol]; 

         if (!cell.ReadOnly) 
         { 
          cell.Value = valuesInRow[iCol]; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

अच्छा कोड।प्राइम टाइम के लिए तैयार नहीं है लेकिन वहां से अधिकांश रास्ते हैं। मैंने इसे एक निजी प्रोजेक्ट में इस्तेमाल किया और जब तक आप धीरे-धीरे ctrl + v में 'v' को छोड़ देते हैं, और ग्रिड EditOnKeystrokeOrF2 के संपादन मोड में होना चाहिए – Mario

0

कॉपी एक पंक्ति दूसरे करने के लिए? क्यों बस इस

public DataGridViewRow CloneWithValues(DataGridViewRow row) 
{ 
    DataGridViewRow clonedRow = (DataGridViewRow)row.Clone(); 
    for (Int32 index = 0; index < row.Cells.Count; index++) 
    { 
     clonedRow.Cells[index].Value = row.Cells[index].Value; 
    } 
    return clonedRow; 
} 

की तरह इसके लिए जाना नहीं इसके अलावा, आप उल्लेख कर सकते हैं: http://canlu.blogspot.com/2009/06/copying-datagridviewrow-to-another.html

आशा इस मदद करता है! :}

0

नीचे alex2k8 जवाब के अपने संशोधित संस्करण इस बाध्य DataGridView के लिए काम करेगा। कोड vb.net में है:

यहाँ असंशोधित संस्करण

' Add rows into grid to fit clipboard lines 
     if (grid.Rows.Count < (r + rowsInClipboard.Length)) 
     { 
      grid.Rows.Add(r + rowsInClipboard.Length - grid.Rows.Count); 
     } 
यहाँ

मेरी संशोधन हो जाता है आशा किसी को यहाँ मदद की हो गई डीटी एक datatable

' Add rows into grid to fit clipboard lines 
    If grid.Rows.Count < (r + rowsInClipboard.Length) Then 
     Dim workRow As DataRow 
     Dim i As Integer 

     For i = 0 To (r + rowsInClipboard.Length - grid.Rows.Count) 
      workRow = dt.NewRow() 
      workRow(0) = "" 

      dt.Rows.Add(workRow) 
     Next 
    End If 

नोट है http://converter.telerik.com/ का उपयोग इसे वापस करने के लिए C#

संबंधित मुद्दे