Hi guys,
The code below is RUNNING Okay, but since it made me suffer more than usual, I want to share it.
I am just a beginner, so you can find the code primitive, but I hope it will help others.
I wanted to:
. - Programmatically create a System.Data.DataTable, adding Columns and Rows.
. - Bind a DataGridView to that DataTable.
. – Replace some columns of type DataGridViewTextBoxColumn by columns of type DataGridViewComboBoxColum.
. - Be able to sort the data in the DataGridView by any column (or the combination of them).
Thanks for the help.
Aldo.
Below the code:
| void ManageDGV() |
| { |
| InitializeComponent(); |
| |
| // Create new binding source, based on programmatically created DataTable. |
| BindingSource bindingSource = new BindingSource(); |
| bindingSource.DataSource = CreateDataTable("FieldsConfig", originalDataGridView); |
| |
| // Bind DataGridView to the BindingSource. |
| dgvPT.DataSource = bindingSource; |
| |
| // Replace DataGridViewTextBoxColumn by DataGridViewComboBoxColumn |
| ReplaceTextBoxColByComboBoxColumn(originalDataGridView, dgvPT, "Orientation", 2); |
| ReplaceTextBoxColByComboBoxColumn(originalDataGridView, dgvPT, "Position", 3); |
| ReplaceTextBoxColByComboBoxColumn(originalDataGridView, dgvPT, "Function", 4); |
| |
| // Sort data in DataGridView "PivotTable". |
| SortBindingSource(dgvPT, "Orientation, Position, Caption asc"); |
| } |
| private System.Data.DataTable CreateDataTable(string tableName, DataGridView dgv) |
| { |
| // Create DataTable. |
| System.Data.DataTable table = new System.Data.DataTable(tableName); |
| |
| // Create DataColumns and add them to DataTable. |
| table.Columns.Add(CreateDataColumn("System.String", "Column", "Column", false, true, true)); |
| table.Columns.Add(CreateDataColumn("System.String", "Caption", "Caption", false, false, false)); |
| table.Columns.Add(CreateDataColumn("System.String", "Orientation", "Orientation", false, false, false)); |
| table.Columns.Add(CreateDataColumn("System.Int32", "Position", "Position", false, false, false)); |
| table.Columns.Add(CreateDataColumn("System.String", "Function", "Function", false, false, false)); |
| |
| // Create rows and add them to the DataTable. |
| AddRowsToTable(dgv, table); |
| |
| // return DataSet. |
| return table; |
| } |
| System.Data.DataColumn CreateDataColumn(string colType, string name, string caption, bool autoInc, bool readOnly, bool unique) |
| { |
| DataColumn column = new DataColumn(); |
| column.DataType = System.Type.GetType(colType); |
| column.ColumnName = name; |
| column.Caption = caption; |
| column.AutoIncrement = autoInc; |
| column.ReadOnly = readOnly; |
| column.Unique = unique; |
| |
| return column; |
| } |
| void AddRowsToTable(DataGridView dgv, System.Data.DataTable table) |
| { |
| // Generate new row for each column in the original DGV. (Each column with it's own config). |
| for (int rowNo = 0; rowNo < dgv.Columns.Count; rowNo++) |
| { |
| // Generate row with values for each column. |
| DataRow row = table.NewRow(); |
| row["Column"] = dgv.Columns[rowNo].HeaderText; |
| row["Caption"] = dgv.Columns[rowNo].HeaderText; |
| row["Orientation"] = "xlRowField"; |
| row["Position"] = rowNo + 1; |
| row["Function"] = "xlNone"; |
| |
| // Add row to table. |
| table.Rows.Add(row); |
| } |
| } |
| void ReplaceTextBoxColByComboBoxColumn(DataGridView originalDGV, DataGridView dgvPivotTable, string colName, int positionInDGV) |
| { |
| // Remove the column. |
| dgvPivotTable.Columns.Remove(colName); |
| |
| // Comienza el cambio de tipo |
| DataGridViewComboBoxColumn comboBoxColumn = CreateComboBoxColumn(colName); |
| |
| // Generate combo box items for the diferent columns. |
| SetAlternativeChoicesUsingItems(originalDGV, comboBoxColumn); |
| |
| // Set column header text |
| comboBoxColumn.HeaderText = colName; |
| |
| // Insert the new DataGridViewComboBoxColumn into the DataGridView. |
| dgvPivotTable.Columns.Insert(positionInDGV, comboBoxColumn); |
| } |
| DataGridViewComboBoxColumn CreateComboBoxColumn(string colName) |
| { |
| // Create new combo box column. |
| DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); |
| |
| // Set properties. |
| column.DataPropertyName = colName; |
| column.Name = colName; |
| column.HeaderText = colName; |
| column.DropDownWidth = 160; |
| column.Width = 90; |
| column.MaxDropDownItems = 5; |
| column.FlatStyle = FlatStyle.Standard; |
| |
| return column; |
| } |
| void SetAlternativeChoicesUsingItems(DataGridView originalDGV, DataGridViewComboBoxColumn comboBoxColumn) |
| { |
| switch (comboBoxColumn.Name) |
| { |
| case "Orientation": |
| comboBoxColumn.Items.AddRange("xlPageField", "xlRowField", "xlColumnField", "xlDataField"); |
| break; |
| case "Position": |
| for (int rowNo = 0; rowNo < originalDGV.Columns.Count; rowNo++) { comboBoxColumn.Items.Add(rowNo + 1); } |
| break; |
| case "Function": |
| comboBoxColumn.Items.AddRange("xlAverage", "xlCount", "xlCountNums", "xlMax", "xlMin" |
| , "xlProduct", "xlStDev", "xlStDevP", "xlSum", "xlUnknown" |
| , "xlVar", "xlVarP", "xlNone"); |
| break; |
| default: |
| break; |
| } |
| } |
| void SortBindingSource(DataGridView dgvToSort, string sortString) |
| { |
| BindingSource bindingSource = (BindingSource)dgvPT.DataSource; |
| bindingSource.Sort = "Orientation, Position asc"; |
| dgvPT.DataSource = bindingSource; |
| } |