PDA

View Full Version : سوال: bind کردن یه combo box ?



edennvl
جمعه 27 شهریور 1388, 14:38 عصر
سلام دوستان
میخواستم بپرسم چطور میشه یه combobox رو به یه بانک bind کرد که اطلاعاتشو نمایش بده؟
خودم تا bind رو بلدم ولی بقیش رو بلد نیستم اینطوری هم بجای اسم "System.Data.DataRowView" اینو مینویسه
این نوشته منه:



comboBox2.DataBindings.Clear();
con.Open();
SqlDataAdapter Da = new SqlDataAdapter();
string str = "select IDS from Farm";
str += comboBox1.Text;
Da = new SqlDataAdapter(str, con);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Table");
comboBox2.DataBindings.Add("datasource", Ds, "Table");

con.Close();





لطفا کمک کنید

corona
جمعه 27 شهریور 1388, 15:18 عصر
دوست من این کدیه که الان نوشتم ولی مسلما تنها راه نیست



private void btnfill_Click(object sender, EventArgs e)
{
try
{
SqlConnection sc = new SqlConnection(@"data source = MOYA-LAP\MOHAMMADSERVER ; database = test; user id = sa ; password = moyamoya");//@"data source = moya-pc\mohammadserver ; database =test;integrated security = true");
SqlCommand cmd = new SqlCommand("select name from student", sc);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
sda.Fill(table);
foreach (DataRow dr in table.Rows)
{
cbox1.Items.Add(dr[0]);
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Reza_Yarahmadi
جمعه 27 شهریور 1388, 18:26 عصر
به روش زیر هم میشه!
string str = "select IDS from Farm";
str += comboBox1.Text;
SqlDataAdapter Da = new SqlDataAdapter(str, con);
DataSet Ds = new DataSet();
Da.Fill(Ds, "Table");
comboBox2.DataSource = Ds;
comboBox2.DisplayMember = "Table.IDS";