Cascading ComboBoxes within a Telerik GridView based on a SQL Server table

bf1o4zei  于 12个月前  发布在  SQL Server
关注(0)|答案(1)|浏览(136)

I am using Telerik in a WPF project. I have the following table:
| ID | Country | Plant | Area | Machine |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | DE | MUC | BA | A |
| 2 | DE | MUC | BA | B |
| 3 | AT | VIE | BE | 1 |
| 4 | AT | VIE | BE | 2 |

Now I would like to fill the ComboBox Plant in a cascading way, after updating the country looking up what Plants are possible for country DE f.e.

The same for the Area and Machine ComboBox.

I already tried several ways but I couldn't find a solution.

I have the following code:

public partial class MainWindow : Window
{
    private SqlConnection connection;
    private string dbName = string.Empty;
    
    public MainWindow()
    {
        InitializeComponent();
        LoadData();
    }

    private void LoadData()
    {
        try
        {
            string connectionString = "Data Source=localhost;Initial Catalog=Test_DB;Integrated Security=True";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM dbo.MachineAreas";
                SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
                DataTable dataTable = new DataTable();
                adapter.Fill(dataTable);

                gridMachineAreas.ItemsSource = dataTable.DefaultView;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

    private void GridMachineAreas_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {            
        switch ((e.Column as GridViewDataColumn).DataMemberBinding.Path.Path)
        {
            case "ID":
                var newColumn1 = new GridViewDataColumn();
                newColumn1.CopyPropertiesFrom(e.Column);
                newColumn1.Header = "ID";
                newColumn1.Width = 60;
                e.Column = newColumn1;
                break;

            case "Country":
                var newColumn2 = new GridViewComboBoxColumn();
                newColumn2.CopyPropertiesFrom(e.Column);
                newColumn2.Header = "Country";
                newColumn2.Width = 60;
                newColumn2.UniqueName = "Country";
                newColumn2.IsComboBoxEditable = false;
                newColumn2.ItemsSource = MakeCountryCollection();                    
                
                e.Column = newColumn2;
                break;

            case "Plant":
                var newColumn3 = new GridViewComboBoxColumn();
                newColumn3.CopyPropertiesFrom(e.Column);
                newColumn3.Header = "Plant";
                newColumn3.Width = 60;                    
                e.Column = newColumn3;
                break;

            case "Area":
                var newColumn4 = new GridViewComboBoxColumn();
                newColumn4.CopyPropertiesFrom(e.Column);
                newColumn4.Header = "Area";
                newColumn4.Width = 60;
                e.Column = newColumn4;
                break;

            case "Machine":
                var newColumn5 = new GridViewComboBoxColumn();
                newColumn5.CopyPropertiesFrom(e.Column);
                newColumn5.Header = "Machine";
                newColumn5.Width = 60;
                e.Column = newColumn5;
                break;
        }
    }

    private List<string> MakeCountryCollection()
    {
        List<string> countryCollection = new List<string>();
        string query = "SELECT Country FROM dbo.MachineAreas Group By Country";
        
        try
        {
            string connectionString = "Data Source=localhost;Initial Catalog=Test_DB;Integrated Security=True";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string value = reader.GetString(0);
                            countryCollection.Add(value);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }

        return countryCollection;
    }
}

Any help or hint is very appreciated.

jgovgodb

jgovgodb1#

Telerik has actually addressed this directly themselves and have provided a great "How-To" type article here:

https://docs.telerik.com/devtools/winforms/knowledge-base/cascading-comboboxes-in-radgridview

相关问题