2017-11-02 6 views
0

"NUL"文字(ASCIIコード= 0)が存在すると、datagridviewのコピー/貼り付けの形式が機能しないのはなぜですか?データに「NUL」文字がある場合、datagridviewはそれらをスペースとして表示しますが、それらのインスタンスを超えてコピーしません(または少なくとも貼り付けません)。バグが見つかったのか、単にクリップボード/コピー/ペーストの動作を理解していないのか分かりません。手動コピー/貼り付けとプログラムによる方法の両方で同じ結果が得られます。 1つを手動で選択すると、複数の行が選択されて表示されますが、1つがペーストされると、最初の「NUL」文字の前のデータのみがペーストされます。その行と残りの行の残りのデータは貼り付けられません。NAG文字がDataGridview選択に存在するときに貼り付け作業をコピーしないのはなぜですか?

行/編集の設定は重要ではないようです。セル内のデータを生成する方法は重要ではないようです。私はGoogleとS.Oを検索しました。無駄な答え。はい、私はS.O.を見ました。投稿Copy string data with NULL character inside string to char arrayNULL in a string can't copy contents after NULと同様の投稿がありますが、NUL文字の置き換えに関する回避策しか提供していません。私は "なぜ"を理解し、NUL文字を含むコピー/ペーストの方法を見つけたいと考えています。

純粋にテストシナリオを作成しました。私があまりにも多くを含むならば、謝罪してください。私は何が必要なのか完全にはわからないので、私はあまりにも多くの側面で誤りを犯しました。

using System; 
using System.Data; 
using System.Windows.Forms; 

namespace GQ 
{ 
    public partial class frmTestOfDataGrid : Form 
    { 
     public frmTestOfDataGrid() 
     { 
      InitializeComponent(); 
     } 

     private void buttonRunTest_Click(object sender, EventArgs e) 
     { 
      BindingSource myBindingSource = new BindingSource(); 
      myBindingSource.DataSource = CreateTestDataTable(); 
      dataGridView1.DataSource = myBindingSource; 
      dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 
     } 
     private DataTable CreateTestDataTable() 
     { 
      char Nul = (char)0; 
      string myTestString1 = "The Nul characters are between these >" + Nul + Nul + "< and you can't copy anything after them."; 
      string myTestString2 = "Second line of test data will not end up being copied to clipboard if above Nul characters are included in selection."; 
      DataTable tbl = new DataTable(); 
      tbl.Columns.Add("RowIndex", typeof(Int32)); 
      tbl.Columns.Add("RowValue", typeof(String)); 
      DataRow dr1 = tbl.NewRow(); 
      dr1[0] = 0; 
      dr1[1] = myTestString1; 
      tbl.Rows.Add(dr1); 
      DataRow dr2 = tbl.NewRow(); 
      dr2[0] = 1Y; 
      dr2[1] = myTestString2; 
      tbl.Rows.Add(dr2); 
      return tbl; 
     } 

     private void buttonCopyToClipboard_Click(object sender, EventArgs e) 
     { 
      if (this.dataGridView1 
       .GetCellCount(DataGridViewElementStates.Selected) > 0) 
      { 
       Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent()); 
      } 
     } 
    } 
} 
namespace GQ 
{ 
    partial class frmTestOfDataGrid 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.dataGridView1 = new System.Windows.Forms.DataGridView(); 
      this.buttonRunTest = new System.Windows.Forms.Button(); 
      this.buttonCopyToClipboard = new System.Windows.Forms.Button(); 
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // dataGridView1 
      // 
      this.dataGridView1.AllowUserToAddRows = false; 
      this.dataGridView1.AllowUserToDeleteRows = false; 
      this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 
      this.dataGridView1.Location = new System.Drawing.Point(10, 43); 
      this.dataGridView1.Name = "dataGridView1"; 
      this.dataGridView1.ReadOnly = true; 
      this.dataGridView1.Size = new System.Drawing.Size(963, 313); 
      this.dataGridView1.TabIndex = 0; 
      // 
      // buttonRunTest 
      // 
      this.buttonRunTest.Location = new System.Drawing.Point(753, 371); 
      this.buttonRunTest.Name = "buttonRunTest"; 
      this.buttonRunTest.Size = new System.Drawing.Size(146, 27); 
      this.buttonRunTest.TabIndex = 1; 
      this.buttonRunTest.Text = "Run Test"; 
      this.buttonRunTest.UseVisualStyleBackColor = true; 
      this.buttonRunTest.Click += new System.EventHandler(this.buttonRunTest_Click); 
      // 
      // buttonCopyToClipboard 
      // 
      this.buttonCopyToClipboard.Location = new System.Drawing.Point(753, 403); 
      this.buttonCopyToClipboard.Name = "buttonCopyToClipboard"; 
      this.buttonCopyToClipboard.Size = new System.Drawing.Size(146, 27); 
      this.buttonCopyToClipboard.TabIndex = 2; 
      this.buttonCopyToClipboard.Text = "Then Copy To Clipboard"; 
      this.buttonCopyToClipboard.UseVisualStyleBackColor = true; 
      this.buttonCopyToClipboard.Click += new System.EventHandler(this.buttonCopyToClipboard_Click); 
      // 
      // frmTestOfDataGrid 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(976, 442); 
      this.Controls.Add(this.buttonCopyToClipboard); 
      this.Controls.Add(this.buttonRunTest); 
      this.Controls.Add(this.dataGridView1); 
      this.Name = "frmTestOfDataGrid"; 
      this.Text = "frmTestOfDataGrid"; 
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); 
      this.ResumeLayout(false); 
     } 

     #endregion 

     private System.Windows.Forms.DataGridView dataGridView1; 
     private System.Windows.Forms.Button buttonRunTest; 
     private System.Windows.Forms.Button buttonCopyToClipboard; 
    } 
} 

答えて

0

(char)0または '\ 0'は基本的に文字列の最後です。それで、コピーは後で何かを取っていないのです。

私はそれが2つの文字enter image description here

enter image description here

を示し、グリッドから、それは/貼り付けをコピーすることは可能です年文で改行を入れます。

enter image description here

それは(「\ 0」)を使用することは珍しいですが、私はそれを使用しての背後にある理由は何を知って興味が?

あなたは、私は/私たちはそこに入れていないWhy do we need to add a '\0' (null) at the end of a character array in C?

+0

上の議論を参照することができます。時々、奇妙な文字が顧客のファイルに入ってくる。 \ 0、\ r、\ n、 "NULL"という単語はすべてデータ内にあります。ヤックしかし、場合によっては、何が入ってくるのかを正確に保持する必要があります。なぜ、これらの特定の文字が顧客データのいくつかのフィールドに存在する理由はありません。 – AndrewBanjo1968

+0

私は参照してください。私は、彼らが時には複数のフィールドを結びつけていると思います。その場合は、string.emptyで置き換えることができます –

関連する問題