DataGridView CodeSample - Population of DataGridViewComboBoxColumn
From Visual WebGui Wiki
| This article will have a few sections added to it soon, based on the following article type skeleton: NewCodeSampleTemplate |
Contents |
Overview
In this DataGridView codesample we demonstrate how you populate the combobox, and then a very simple implementation of how you can populate individual ComboBox cells with different datasources than is default for the column. Remember that the structure of the DataGridView relies heavily on inheritance of default values. In this particular case, setting the DataSource for the ComboBoxColumn sets the default for that column for all rows. By setting specific values on individual cells, you override that default. There will be an overhead of course, but it is perfectly ok to do it like that.
Version specifics
Note that setting non-default for DataSource is not supported until version 6.4
VB.NET Code
Imports Gizmox.WebGUI.Forms Public Class PopulateComboBox Private Class Country Private Name As String Public Sub New(ByVal sName As String) Name = sName End Sub Public Overrides Function ToString() As String Return Name End Function End Class Private Class Person Private Name As String Public Sub New(ByVal sName As String) Name = sName End Sub Public Overrides Function ToString() As String Return Name End Function End Class Private WithEvents DataGridView1 As Gizmox.WebGUI.Forms.DataGridView Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim personArr() As Person = {New Person("Alfred"), New Person("Boris"), New Person("Catherine")} Dim countryArr() As Person = {New Person("Israel"), New Person("Iceland"), New Person("US")} DataGridView1 = New Gizmox.WebGUI.Forms.DataGridView CType(DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit() DataGridView1.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.FixedSingle DataGridView1.Location = New System.Drawing.Point(40, 31) DataGridView1.Name = "DataGridView1" DataGridView1.RowHeadersWidth = 50 DataGridView1.Size = New System.Drawing.Size(248, 334) DataGridView1.TabIndex = 0 DataGridView1.TotalItems = 1 Me.Controls.Add(DataGridView1) Dim DataGridViewComboBoxColumn1 As Gizmox.WebGUI.Forms.DataGridViewComboBoxColumn DataGridViewComboBoxColumn1 = New Gizmox.WebGUI.Forms.DataGridViewComboBoxColumn DataGridViewComboBoxColumn1.AutoSizeMode = Gizmox.WebGUI.Forms.DataGridViewAutoSizeColumnMode.NotSet DataGridViewComboBoxColumn1.DefaultHeaderCellType = GetType(Gizmox.WebGUI.Forms.DataGridViewColumnHeaderCell) DataGridViewComboBoxColumn1.DisplayStyle = Gizmox.WebGUI.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton DataGridViewComboBoxColumn1.FlatStyle = Gizmox.WebGUI.Forms.FlatStyle.Standard DataGridViewComboBoxColumn1.Name = "DataGridViewComboBoxColumn1" DataGridViewComboBoxColumn1.Resizable = Gizmox.WebGUI.Forms.DataGridViewTriState.[True] DataGridViewComboBoxColumn1.SortMode = Gizmox.WebGUI.Forms.DataGridViewColumnSortMode.NotSortable DataGridViewComboBoxColumn1.Width = 100 DataGridView1.Columns.AddRange(New Gizmox.WebGUI.Forms.DataGridViewColumn() {DataGridViewComboBoxColumn1}) Dim bndPersons As BindingSource = New BindingSource CType(bndPersons, System.ComponentModel.ISupportInitialize).BeginInit() bndPersons.DataSource = personArr DataGridViewComboBoxColumn1.DataSource = bndPersons CType(bndPersons, System.ComponentModel.ISupportInitialize).EndInit() Dim bndCountries As BindingSource = New BindingSource CType(bndCountries, System.ComponentModel.ISupportInitialize).BeginInit() bndCountries.DataSource = countryArr CType(bndCountries, System.ComponentModel.ISupportInitialize).EndInit() CType(DataGridView1, System.ComponentModel.ISupportInitialize).EndInit() DataGridView1.Rows.Add() DataGridView1.Rows.Add() ' Hard coded different datasource for second row in DataGridView CType(DataGridView1.Rows(1).Cells(0), DataGridViewComboBoxCell).DataSource = bndCountries DataGridView1.Update() AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged End Sub Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As Gizmox.WebGUI.Forms.DataGridViewCellEventArgs) Try MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString) Catch End Try End Sub End Class
C# Code
| The conversion of this code to C# has not been completed yet. Please feel free to contribute and add contents to the Wiki |
