2017-10-16 5 views
0

私はWindowsフォームアプリケーションで作業しています。私のデータを表示するためにdatagridviewを作成しましたが、Datagridviewから特定の列を選択し、 。DataGridViewデータを選択して1つのテキストボックスに表示するには

私は試しましたが、最後のColumnは "SEIKEN_NO"という名前のテキストボックスになっていますが、複数のカラム値を1つのTextBoxに表示します。

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     // to set oem no, fic_no and seiken_no to textbox named particular1Txt. 

     dataGridView1.Refresh(); 

     try 
     { 
      if (RB1.Checked == true) 
      { 
       int i; 
       i = dataGridView1.SelectedCells[0].RowIndex; 

       Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); 
       Particular1Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(); 
       Particular1Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString(); 
      } 
      else if (RB2.Checked == true) 
      { 
       int i; 
       i = dataGridView1.SelectedCells[0].RowIndex; 
       Particular2Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); 
       Particular2Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(); 
       Particular2Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString(); 
      } 
+1

通常、 'TextBox'は単一のフィールドを表示することになっています。しかし何らかの理由で複数のフィールドを表示したい場合は、 '、'のような区切り文字で文字列を結合し、その結果を 'TextBox'に表示してください。 –

+1

数週間前に[非常によく似た質問](https://stackoverflow.com/q/46539704/472495)に尋ねられたようですが、奇妙なことに、あなたを手伝った人には返信していないようです。あなたが援助に来る人々に反応するように努力していることに気づいた場合は、ここで援助を受ける可能性が高くなります。 – halfer

答えて

2

フォアを使用すると、テキストボックスにデータを割り当てる各ライン:

Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();

変化に:

Particular1Txt.Text += dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();

ノート+=

がそうでなければ、あなたがoverwありますすることができます、書式設定値の連結用のTextBox

の内容を書くと:

+= " " + dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); // very basic way to do it

String.Formatのと別の方法の一例()

Particular1Txt.Text=String.Format({0} {1} {2}), dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(),dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(),dataGridView1.Rows[i].Cells["Seiken_No"].Value.ToString(); 

はあなたにも使用することができます:文字列。文字列配列、またはStringBuilderクラスを使用してJoin()を実行します。

このヘルプが表示されます

+0

どうもありがとうございますが、テキストボックスの各列の値の間にスペースを入れるにはどうすればいいですか?あなたはPLZに話すことができますか? –

+0

これで問題が解決した場合は、正しい答えとしてマークしてください – Ferus7

1
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
{ 
    // to set oem no, fic_no and seiken_no to textbox named particular1Txt. 
    dataGridView1.Refresh(); 
    try 
    { 
     if (RB1.Checked == true) 
     { 
      int i; 
      i = dataGridView1.SelectedCells[0].RowIndex; 
      Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString(); 
     } 
     else if (RB2.Checked == true) 
     { 
      int i; 
      i = dataGridView1.SelectedCells[0].RowIndex; 
      Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString(); 
     } 
    } 
} 
関連する問題