Home Forums WinForms controls Xceed Grid for WinForms Filtering one ComboBox cell by another in a grid

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17311 |

    Hi all,

    We are currently evaluating Xceed DataGrid for a project and have a requirement to have one ComboBoxEditor/Viewer cell in a DataGrid row filter another ComboBoxEditor/Viewer cell in the same row on item selection. i.e. user selects an item from the first cell combo drop down which then causes the second cell combo to update its items accordingly without a row leave. The test we came up with is rough and ready but works (other than the filtered combo cell updating to the first of the new items on filtering), but we wondered if there’s a better solution that we’ve missed. We use a DataView and filter that using the Activate and Deactivate events of the ComboBoxEditor/Viewer. Here’s what we came up with, can anyone se a better solution than this? Thanks in advance [:)]

    ComboBoxEditor masterComboBoxEditor, filteredComboBoxEditor;

    DataView dataView;

    DataTable filterEditorDataTable, filterViewerDataTable;

    public TestForm()

    {

    InitializeComponent();

    // Table for data grid

    DataTable gridDataTable = new DataTable();

    gridDataTable.Columns.Add(“col1”);

    gridDataTable.Columns.Add(“col2”);

    gridDataTable.Rows.Add(1, 1);

    gridDataTable.Rows.Add(2, 3);

    gridDataTable.Rows.Add(3, 5);

    gridControl.DataSource = gridDataTable;

    // Table for master combo

    DataTable masterDataTable = new DataTable();

    masterDataTable.Columns.Add(“key1”);

    masterDataTable.Columns.Add(“val1”);

    masterDataTable.Rows.Add(1, “a”);

    masterDataTable.Rows.Add(2, “b”);

    masterDataTable.Rows.Add(3, “c”);

    masterComboBoxEditor = new ComboBoxEditor(masterDataTable, string.Empty, “key1”);

    masterComboBoxEditor.TemplateControl.DisplayFormat = “%val1%”;

    masterComboBoxEditor.TemplateControl.Columns[“key1”].Visible = false;

    ComboBoxViewer masterComboBoxView = new ComboBoxViewer(masterDataTable, string.Empty, “key1”, “%val1%”);

    gridControl.Columns[“col1”].CellEditorManager = masterComboBoxEditor;

    gridControl.Columns[“col1”].CellViewerManager = masterComboBoxView;

    masterComboBoxEditor.DeactivatingControl += new CellEditorEventHandler(masterComboBoxEditor_DeactivatingControl);

    // Table for filtered combo

    filterEditorDataTable = new DataTable();

    filterEditorDataTable.Columns.Add(“key2”);

    filterEditorDataTable.Columns.Add(“fkey1”);

    filterEditorDataTable.Columns.Add(“val2”);

    filterEditorDataTable.Rows.Add(1, 1, “X”);

    filterEditorDataTable.Rows.Add(2, 1, “Y”);

    filterEditorDataTable.Rows.Add(3, 2, “D”);

    filterEditorDataTable.Rows.Add(4, 2, “E”);

    filterEditorDataTable.Rows.Add(5, 3, “F”);

    // Use data view to control filtering in 2nd combo box

    dataView = filterEditorDataTable.DefaultView;

    filteredComboBoxEditor = new ComboBoxEditor(filterEditorDataTable, string.Empty, “key2”);

    filteredComboBoxEditor.TemplateControl.DisplayFormat = “%val2%”;

    filteredComboBoxEditor.TemplateControl.Columns[“fkey1”].Visible = false;

    filteredComboBoxEditor.TemplateControl.Columns[“key2”].Visible = false;

    gridControl.Columns[“col2”].CellEditorManager = filteredComboBoxEditor;

    filteredComboBoxEditor.ActivatingControl += new CellEditorEventHandler(filteredComboBoxEditor_ActivatingControl);

    filteredComboBoxEditor.DeactivatingControl += new CellEditorEventHandler(filteredComboBoxEditor_DeactivatingControl);

    filterViewerDataTable = new DataTable();

    filterViewerDataTable.Columns.Add(“key2”);

    filterViewerDataTable.Columns.Add(“fkey1”);

    filterViewerDataTable.Columns.Add(“val2”);

    filterViewerDataTable.Rows.Add(1, 1, “X”);

    filterViewerDataTable.Rows.Add(2, 1, “Y”);

    filterViewerDataTable.Rows.Add(3, 2, “D”);

    filterViewerDataTable.Rows.Add(4, 2, “E”);

    filterViewerDataTable.Rows.Add(5, 3, “F”);

    ComboBoxViewer filteredComboBoxViewer = new ComboBoxViewer(filterViewerDataTable, string.Empty, “key2”, “%val2%”);

    gridControl.Columns[“col2”].CellViewerManager = filteredComboBoxViewer;

    }

    void masterComboBoxEditor_DeactivatingControl(object sender, CellEditorEventArgs e)

    {

    // Set filter for filtered combo using selected key from master combo

    dataView.RowFilter = string.Format(“fkey1 = {0}”, e.Cell.Value);

    }

    void filteredComboBoxEditor_ActivatingControl(object sender, CellEditorEventArgs e)

    {

    dataView.RowFilter = string.Format(“fkey1 = {0}”, ((XceedDataRow)gridControl.CurrentRow).Cells[“col1”].Value);

    }

    void filteredComboBoxEditor_DeactivatingControl(object sender, CellEditorEventArgs e)

    {

    // Reset filter on leaving filtered combo

    dataView.RowFilter = string.Empty;

    }

    Imported from legacy forums. Posted by Dominic (had 1956 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,

    I think I have found a sample project (in VB.NET) that does what you want.  I will let you be the judge.  I’ve just realized that there is no grid in this is example, only two wincombobox linked together.  I’ve included the complete project. 

    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    Imports Xceed.Editors

    Namespace WindowsFormsApplication35
      Partial Public Class Form1
       Inherits Form
     Public Sub New()
       InitializeComponent()
     End Sub

     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
       Dim dataSet As New DataSet()

       Dim categories As New DataTable(“Categories”)
       Dim subCategories As New DataTable(“SubCategories”)

       categories.Columns.Add(New DataColumn(“PK”, GetType(Integer)))
       categories.Columns.Add(New DataColumn(“Name”, GetType(String)))

       subCategories.Columns.Add(New DataColumn(“PK”, GetType(Integer)))
       subCategories.Columns.Add(New DataColumn(“FK”, GetType(Integer)))
       subCategories.Columns.Add(New DataColumn(“Name”, GetType(String)))

       Dim z As Integer = 0
       For i As Integer = 0 To 2
      categories.Rows.Add(i, “Category #” & i.ToString())

      For j As Integer = 0 To 4
        subCategories.Rows.Add(z, i, “SubCategory #” & j.ToString() & ” of cat# ” & i.ToString())
        z += 1
      Next j
       Next i


       dataSet.Tables.Add(categories)
       dataSet.Tables.Add(subCategories)

       dataSet.Relations.Add(New DataRelation(“Cat_SubCat”, categories.Columns(“PK”), subCategories.Columns(“FK”)))

     


       m_winComboBoxMaster.Location = New Point(0, 50)
       m_winComboBoxChild.Location = New Point(300, 50)

     

       Me.Controls.Add(m_winComboBoxMaster)
       Me.Controls.Add(m_winComboBoxChild)


       m_winComboBoxChild.DropDownControl.BindingContext = m_winComboBoxMaster.DropDownControl.BindingContext

       m_winComboBoxMaster.SetDataBinding(dataSet, “Categories”)
       m_winComboBoxMaster.DisplayFormat = “%Name%”

       AddHandler m_winComboBoxMaster.SelectedItemChanged, AddressOf winComboBoxMaster_SelectedItemChanged

       m_winComboBoxChild.SetDataBinding(dataSet, “Categories.Cat_SubCat”)
       m_winComboBoxChild.DisplayFormat = “%Name%”

       m_winComboBoxMaster.SelectedIndex = 0
     End Sub

     Private m_winComboBoxMaster As New WinComboBox()
     Private m_winComboBoxChild As New WinComboBox()

     Private Sub winComboBoxMaster_SelectedItemChanged(ByVal sender As Object, ByVal e As EventArgs)
       m_winComboBoxChild.TextBoxArea.Text = TryCast(m_winComboBoxChild.Items(0).Values(2), String)
     End Sub
      End Class
    End Namespace

    Regards,

    Imported from legacy forums. Posted by Ghislain (had 2072 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.