2017-03-06 16 views
0

コンボボックスにストアドプロシージャの名前を設定しようとしています。コンボボックスをストアドプロシージャ内のデータで埋めてください。

public frmAddEdit(Book book, Author author) 
    { 
     _book = book; 
     _author = author; 
     _dataAccess = DataAccess.GetInstance; 
     _dataAccess.SetConnectionString(_dataAccess.GetServerConnectionString()); 
     ComboFill(); 
     InitializeComponent(); 
    } 

これは私がオンラインを見て、それを試してみました私のコンボボックス

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DataRowView view = comboBox1.SelectedItem as DataRowView; 
     string fullName = view["FullName"].ToString(); 


    } 

何か私の負荷フォーム

public void AddEditBook_Load(object sender, EventArgs e) 
    { 

     txtTitle.Text = _book.Title; 
     //comboBox1.DataSource = _book.FullName; 
     //comboBox1.ValueMember = "AuthorId"; 
     //comboBox1.DisplayMember = "FullName"; 
     ComboFill(); 
     //txtAuthor.Text = _author.FullName; 
     //txtAdd.Text = book.Title; 
    } 

です。

  public void ComboFill() 
    { 
     DataRow dr; 
     DataTable dt = new DataTable(); 

     dr = dt.NewRow(); 
     // dr.ItemArray = new object[] { 0, "Select Author" }; 
     dt.Rows.InsertAt(dr, 0); 
     comboBox1.DataSource = _book.FullName; 
     comboBox1.ValueMember = "AuthorId"; 
     comboBox1.DisplayMember = "FullName"; 
    } 
+0

なぜあなたは簡単な方法でそれを行うことができたときに、複雑なのですか? –

+0

'System.Windows.Forms.ComboBox'とタイプし、' ValueMember'または 'DisplayMember'という名前のプロパティはありません。 –

+1

あなたの質問は何ですか?何かエラーがありますか?どうしたの? – Pikoh

答えて

1
//assume this that you have datatable filled from stored procedure 
//This is sample for populating your combo 
DataTable dt = new DataTable(); 
dt.Columns.Add("AuthorID", typeof(int)); 
dt.Columns.Add("FullName", typeof(string)); 

DataRow dr = null; 
for (int i = 0; i < 10; i++) 
{ 
    dr = dt.NewRow(); 
    dr[0] = i; 
    dr[1] = "ABC" + i + 2 * i; 

    dt.Rows.Add(dr); 
} 

comboBox1.DataSource = dt; // this will be populated from SP 
comboBox1.ValueMember = "AuthorID"; 
comboBox1.DisplayMember = "FullName"; 
+0

この部分はデータベースからフルネームを持ちません。 – SMG

+0

DataRow dr = null; (int i = 0; i <10; i ++) { dr = dt.NewRow(); dr [0] = i; dr [1] = "ABC" + i + 2 * i; dt.Rows.Add(dr); } – SMG

+0

私はデータ内の4人の作者の名前をコンボボックス – SMG

1

comboBox1.DataSource = _bool.FullName;が間違っています。それは次のようになります。

comboBox1.DataSource = _book; 
+0

1つの項目でconboboxを充填する際のポイントは何ですか、もし複数の要素がある場合は、 SMTBCJ15の答えです。http://stackoverflow.com/posts/42625890/edit – bradbury9

+0

はい、ムーコンボボックスのデータがありますが、私のデータベースに保存されているフルネームがコンボボックスに表示されるようにします – SMG

+0

だから、人々は助けてくれませんコード全体@SMG。あなたはこの質問が投票されていないことを幸運にも持ちます。上記の答えよりも理解できない場合は、このリンクをご覧くださいhttp://www.c-sharpcorner.com/UploadFile/009464/bind-combobox-in-window-application-using-C-Sharp/ –

関連する問題