Как связать 2 комбо бокса между собой?
Нужен запрос для связывания двух комбо боксов между собой.В первом должен выводиться автор,а во втором только его книги.
Код:
//заполняем 1-ый combobox
private void button1_Click(object sender, EventArgs e)
{
string connectstring = "Data Source=(LocalDB)\MSSQLLocalDB;" +
"AttachDbFilename=C:\Users\Test\Desktop\SQLTest\SQLTest\Database1.mdf;Integrated Security=True";
using (SqlConnection sqlConnection =
new SqlConnection(connectstring))
{
try
{
sqlConnection.Open();
string commandstring = "SELECT id, name FROM author";
using (SqlCommand myCommand = new SqlCommand(commandstring, sqlConnection))
{
var SelectTable = new System.Data.DataTable();
SelectTable.Load(myCommand.ExecuteReader());
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
comboBox1.DataSource = SelectTable;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
//при выборе строки в combobox1 заполняется combobox2
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectstring = "Data Source=(LocalDB)\MSSQLLocalDB;" +
"AttachDbFilename=C:\Users\Test\Desktop\SQLTest\SQLTest\Database1.mdf;Integrated Security=True";
using (SqlConnection sqlConnection =
new SqlConnection(connectstring))
{
try
{
sqlConnection.Open(); //открываем подключение
string commandstring = "SELECT title FROM book WHERE author_id = '" + comboBox1.SelectedValue + "'";
using (SqlCommand myCommand = new SqlCommand(commandstring, sqlConnection))
{
var SelectTable = new System.Data.DataTable();
SelectTable.Load(myCommand.ExecuteReader());
comboBox2.DisplayMember = "text";
comboBox2.DataSource = SelectTable;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string connectstring = "Data Source=(LocalDB)\MSSQLLocalDB;" +
"AttachDbFilename=C:\Users\Test\Desktop\SQLTest\SQLTest\Database1.mdf;Integrated Security=True";
using (SqlConnection sqlConnection =
new SqlConnection(connectstring))
{
try
{
sqlConnection.Open();
string commandstring = "SELECT id, name FROM author";
using (SqlCommand myCommand = new SqlCommand(commandstring, sqlConnection))
{
var SelectTable = new System.Data.DataTable();
SelectTable.Load(myCommand.ExecuteReader());
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
comboBox1.DataSource = SelectTable;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
//при выборе строки в combobox1 заполняется combobox2
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string connectstring = "Data Source=(LocalDB)\MSSQLLocalDB;" +
"AttachDbFilename=C:\Users\Test\Desktop\SQLTest\SQLTest\Database1.mdf;Integrated Security=True";
using (SqlConnection sqlConnection =
new SqlConnection(connectstring))
{
try
{
sqlConnection.Open(); //открываем подключение
string commandstring = "SELECT title FROM book WHERE author_id = '" + comboBox1.SelectedValue + "'";
using (SqlCommand myCommand = new SqlCommand(commandstring, sqlConnection))
{
var SelectTable = new System.Data.DataTable();
SelectTable.Load(myCommand.ExecuteReader());
comboBox2.DisplayMember = "text";
comboBox2.DataSource = SelectTable;
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}