Monday, April 21, 2008

Excel Like Filters using C1 True DBGrid

I have been exploring using Excel Style filters using the Component One (C1) True DBGrid. The samples that ship with the Grid show how to do this for one column at load time. The following code automatically generates Excel Like AutoFilter combo boxes for every column in your data grid. For best results, add this code in the DataSourceChanged event so that the filters get set up properly:


For each dc as C1.Win.C1TrueDBGrid.C1DataColumn in Me.C1TrueDBGrid1.Columns
Dim c As C1.Win.C1TrueDBGrid.C1TrueDBDropdown
c = New C1.Win.C1TrueDBGrid.C1TrueDBDropdown
CType(c, System.ComponentModel.ISupportInitialize).BeginInit()
' c
c.AllowColMove = True
c.AllowColSelect = True
c.AllowRowSizing = C1.Win.C1TrueDBGrid.RowSizingEnum.AllRows
c.AlternatingRows = False
c.ColumnCaptionHeight = 20
c.ColumnFooterHeight = 20
c.DataSource = Me.C1TrueDBGrid1.DataSource
c.DataMember = Me.C1TrueDBGrid1.Columns(c).DataField
c.ExtendRightColumn = True
c.FetchRowStyles = False
c.Location = New System.Drawing.Point(208, 112)
c.Name = "ddwn" & c
c.RowDivider.Color = System.Drawing.Color.DarkGray
c.RowDivider.Style = C1.Win.C1TrueDBGrid.LineStyleEnum.[Single]
c.RowSubDividerColor = System.Drawing.Color.DarkGray
c.ScrollTips = False
c.Size = New System.Drawing.Size(100, 88)
c.TabIndex = 1
c.Text = "ddwn" & c
c.Visible = False
Me.C1TrueDBGrid1.Controls.Add(c)
CType(c, System.ComponentModel.ISupportInitialize).EndInit()
With Me.C1TrueDBGrid1.Columns(c)
.DropDown = c
.FilterDropdown = True
End With
' turn on the dropdown button in the filterbar
Me.C1TrueDBGrid1.Splits(0).DisplayColumns(c).FilterButton = True
' turn it off for regulars cells in the grid
Me.C1TrueDBGrid1.Splits(0).DisplayColumns(c).Button = False
c.ColumnHeaders = False
Next



Now when you assign a DataSource to your grid, you will automatically get Excel Style filters on your True DBGrid.

Hope this helps someone.

1 comment:

Anonymous said...

Do you have more code using the C1trueDBGrid?

I would like to see how to bind it to a dataset and set columns in code. Also how to update the data back to the source DataBase.

Thanks.