有一个表,如下:
Create Table WS_Message
(
StockManufacturer VarChar(30) Not Null,--采购商
Re VarChar(200),--备注
)我知道combobox是这样加进去的
combobox.Items.AddRange(new object[] {"SONY","LG"});
连接数据库如下:SqlConnection sqlcon = null;
SqlDataAdapter da = null; try
{
sqlcon = new SqlConnection(server = local;database = mxsm;user id = sa;password = sa;pooling = true);
sqlcon.Open();
DataSet ds = new DataSet(); da = new SqlDataAdapter(select StockManufacturer from WS_Message,sqlcon);
da.Fill(ds,Table);
sqlcon.Close();
}
finally
{
sqlcon.Close();
}
我点combobox的时候,就会发生SelectedIndexChanged 事件,怎么样,才能把数据库中的StockManufacturer列的值一个一个,加到combobox的?
Create Table WS_Message
(
StockManufacturer VarChar(30) Not Null,--采购商
Re VarChar(200),--备注
)我知道combobox是这样加进去的
combobox.Items.AddRange(new object[] {"SONY","LG"});
连接数据库如下:SqlConnection sqlcon = null;
SqlDataAdapter da = null; try
{
sqlcon = new SqlConnection(server = local;database = mxsm;user id = sa;password = sa;pooling = true);
sqlcon.Open();
DataSet ds = new DataSet(); da = new SqlDataAdapter(select StockManufacturer from WS_Message,sqlcon);
da.Fill(ds,Table);
sqlcon.Close();
}
finally
{
sqlcon.Close();
}
我点combobox的时候,就会发生SelectedIndexChanged 事件,怎么样,才能把数据库中的StockManufacturer列的值一个一个,加到combobox的?
在你读取数据库表的时候,就将每条记录(之中的各字段内容用*隔开)作为一个整体存入string 数组变量Array中; 然后利用Split("*",表中的列数目)分割该Array中的各个元素最后在SelectedIndexChanged事件中绑定既可!
myDataAdapter.Fill(myDataset,"student");
foreach (DataRow myRow in myDataset.Tables["student"].Rows)
{
strStudentID =(string) myRow["studentID"];
strStudentSubject=(string) myRow["StudentSubject"];
strStudentName=(string) myRow["StudentName"];
strTemp=strStudentID+"*"+strStudentSubject+"*"+strStudentName;
//put strTemp into string strArraylist with int i(index for strArraylist)
this.strArraylist[i++]=strTemp;
//display in the ComboBox only strStudentID
this.comboBox1.Items.Add(strStudentID);
}
// Close the connection to the database.
myConn.Close();
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string delimstr="*";
char [] delimeter=delimstr.ToCharArray();
string [] splitstr=null;
splitstr=this.strArraylist[this.comboBox1.SelectedIndex].Split(delimeter,3);
//下面根据需要修改
this.textBox1.Text=splitstr[0];
this.textBox2.Text=splitstr[1];
this.textBox3.Text=splitstr[2];
.net的控件大部分都有databind事件,应该可以直接绑定,不需要那么麻烦