如何使用项目中的所有数据集填充我的ComboBox
我的代码
foreach (DataTable table in DataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
object item = row[column];
}
}
comboBox1.Items.Add(table.TableName);
}
答案 0 :(得分:0)
您需要仅使用tableNames填充组合框。
foreach (DataTable table in DataSet.Tables)
{
comboBox1.Items.Add(table.TableName);
}
然后你需要编写组合框的Select事件,它可能看起来像这样。
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//fill listview with items that belong to the combobox.SelectedItem
//since you filled the combobox with table.TableName, the name will be in .SelectedItem
}
不要忘记
this.comboBox1.SelectedIndexChanged += new System.EventHandler(comboBox1_SelectedIndexChanged);
答案 1 :(得分:0)
修改您的查询以获取sql命令结果中的所有数据,例如
string connetionString = null;
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
connetionString = "Data Source=.;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
sql = "select au_id,au_lname from authors";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "au_id";
comboBox1.DisplayMember = "au_lname";
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}