2016-08-05 5 views
0

リストボックスで選択されているものに応じて、3つのデータグリッドビューをプログラムで作成しています。プログラムで作成されたデータグリッドビューからテキストボックスに表示するデータを取得できません

私はフォーム上の多くのオブジェクトを扱うのが面倒で難しいので、この方法で作成しています。これは多くのクリーナーです。

しかし、listBox1_SelectedIndexChangedイベントの外部でどのような種類のイベントを導入しても、私は多くのWebサイトを見て、半ダースの方法で試してみましたが、テキストを表示するために作成したテキストボックスを取得できませんこれらのデータグリッドビュー。 (私はこれまでのところgridCのデータでしか作業していませんでした)

グリッドの作成方法はこれと関係があります。誰もこれを前にしたことがありますか?可能であれば、それらをオンザフライで作成することを本当に忠実にお勧めします。

申し訳ありませんが、私は誰かが知っている必要があるかもしれません。ありがとうございました!!

コード: (文場合、私は他の二つの内容を削除した):

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.IO; 
using System.Configuration; 


namespace MyWiki_8_4_16 
{ 
    public partial class Form1 : Form 
    { 
     SqlConnection conn = new SqlConnection("<Connection String>"); 
     string qryQuery = null; 
     DataGridView gridC = new DataGridView(); 
     DataGridView gridG = new DataGridView(); 
     DataGridView gridF = new DataGridView(); 

     public Form1() 
     { 
      InitializeComponent(); 
      conn.Open(); 
      DataSet ds = new DataSet(); 
      SqlDataAdapter adapter = new SqlDataAdapter(
      "select Stuff(table_name, 1, 4, '') as table_name from INFORMATION_SCHEMA.Tables where table_name not like 'Ref%'", conn); 
      adapter.Fill(ds); 
      this.listBox1.DataSource = ds.Tables[0]; 
      this.listBox1.DisplayMember = "table_name"; 
      conn.Close(); 
      gridC.Visible = false; 
      gridC.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
     } 

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      string text = listBox1.GetItemText(listBox1.SelectedItem); 
      string constring = ("Data Source=DSPL7722\\KATMO;Initial Catalog=Physics;Integrated Security=True"); 
      SqlConnection con = new SqlConnection(constring); 
      if (text == "Categories") 
      { 
       con.Open(); 
       qryQuery = ("select Category from tbl_categories"); 
       SqlCommand cmd = new SqlCommand(qryQuery, con); 
       cmd.CommandType = CommandType.Text; 
       SqlDataAdapter sda = new SqlDataAdapter(cmd); 
       DataTable dt = new DataTable(); 
       this.Controls.Add(gridC); 
       gridC.DataSource = dt; 
       sda.SelectCommand = cmd; 
       sda.Fill(dt); 
       gridC.Visible = true; 
       gridF.Visible = false; 
       gridG.Visible = false; 
       gridC.Location = new Point(0, 0); 
      } 
      else if (text == "Glossary") 
      { 

      } 
      else if (text == "Formulas") 
      { 

      } 
     } 

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.RowIndex >= 0) 
      { 
       DataGridViewRow row = this.gridC.Rows[e.RowIndex]; 
       if (row != null) 
        txt_Col1.Text = row.Cells["Category"].Value.ToString(); 
      } 
     } 
    }//class 
}//namespace 

もう1:

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
{ 
    DataGridViewCell cell = null; 
    foreach (DataGridViewCell selectedCell in gridC.SelectedCells) 
    { 
     cell = selectedCell; 
     break; 
    } 
    if (cell != null) 
    { 
     DataGridViewRow row = cell.OwningRow; 
     txt_Col1.Text = row.Cells["Category"].Value.ToString(); 
    } 
} 
private void Form1_Load(object sender, EventArgs e) 
{ 

} 
+4

は "これは多くのクリーナーです" してみてください。あなたの精神科医に言わないでください。彼らはあなたをロックされた病棟に永遠に閉じ込めるでしょう。その一般的なテーマで続けて、あなたの質問はテキストボックスに何かを表示することですが、コードにテキストボックスはありません。このすべてのコンテキストは素晴らしいですが、理想的には、正しく動作していない部分も含めるべきです。 –

+0

非常に多くのコードがあって、私はそれらをすべて取り出しました! – CatEight

+0

//プライベートボイドdataGridView1_CellContentClick(オブジェクト送信者、DataGridViewCellEventArgs E) // {// IF(e.RowIndex> = 0) // {// DataGridViewRow行= this.gridC.Rows [e.RowIndex] ; // if(row!= null) // txt_Col1.Text = row.Cells ["Category"]。Value.ToString(); //} //} – CatEight

答えて

0

あなたが最初にしてgirdviewデータソースにデータテーブルを設定していますそれを補充してください。それは正しいものではなく、他の方法でなければなりません。

  SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      DataTable dt = new DataTable(); 
      this.Controls.Add(gridC); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      gridC.DataSource = dt; 

さらに、ifブロックの条件に基づいて1つのgridviewしか使用していないときは、いつでも同じように見えますが、3つのgridviewを持つ必要はなく、設計時に宣言することもできません。

0

これはどういう意味ですか?

あなたのテキストボックスに行くあなたのDataGridからテキスト/文字列の値を渡したいと思います。

はこの1

int index = gridC.CurrentRow.Index; 
txt_Col1.Text = gridC.Rows[index].Cells["Category"].Value.ToString(); 
関連する問題