C# .NET - C# - Just a Comment - Bind DataGridView to DataTable, programmatically create DataTable, convert DataGridViewTextBoxColumn to DataGridViewComboBoxColumn, and Sort DataGridView by columns

Asked By Aldo Liaks on 16-Jul-08 07:34 AM

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"falsetruetrue));  
   table.Columns.Add(CreateDataColumn("System.String""Caption""Caption"falsefalsefalse));  
   table.Columns.Add(CreateDataColumn("System.String""Orientation""Orientation"falsefalsefalse));  
   table.Columns.Add(CreateDataColumn("System.Int32""Position""Position"falsefalsefalse));  
   table.Columns.Add(CreateDataColumn("System.String""Function""Function"falsefalsefalse));  
 
   // 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;  

re Just a Comment - Bind DataGridView to DataTable, programmatically create DataTable, convert DataGridViewTextBoxColumn to DataGridViewComboBoxColumn, and Sort DataGridView by columns

Sakshi a replied to Aldo Liaks on 16-Jul-08 07:41 AM
Though a startup, your work is appreciatable.

Thanks! :-))

Aldo Liaks replied to Sakshi a on 16-Jul-08 07:54 AM
end of post

comment

zafar iqbal replied to Aldo Liaks on 16-Jul-08 09:05 AM

good approach being a beginer.

Thanks,

zafar

:-))
Aldo Liaks replied to zafar iqbal on 16-Jul-08 09:09 AM
end of post
Its great aldo
alice johnson replied to Aldo Liaks on 16-Jul-08 01:00 PM
end of post
Thanks Alice.
Aldo Liaks replied to alice johnson on 16-Jul-08 01:16 PM

Thanks to you! :-)))

\Can you give a hand with this one?

http://eggheadcafe.com/community/aspnet/2/10044253/c--cell-value-change-af.aspx

Thanks.

Zeno Z replied to Aldo Liaks on 14-Jun-10 06:48 PM
Wow, you just saved my ass. God bless.
Dominic replied to Aldo Liaks on 06-Sep-11 07:40 PM
I copied the code into VS2008. Created two datagridview in a form (called them originalDataGridView and dgvPT), ran your code, but crashed. Any idea please?

System.Data.ConstraintException: Column 'Column' is constrained to be unique. Value " is already present......

Thank you.