• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

How to add an empty col to a grid?

Hi there!

How can i add an empty column to a grid where the data source is an array?

when i get the grid style only the columns with a corresponding mapping name will get displayed in the grid.

So if i don't supply a mapping  name for the empty col when setting the grid style it dosn't display e.g.

Dim gs As System.Windows.Forms.DataGridTableStyle = New DataGridTableStyle
            'Column 1
            Dim colName As DataGridTextBoxColumn = New DataGridTextBoxColumn
            With colName
                .HeaderText = ""
                .Width = 150
                '.MappingName = "DataTypeName"
            End With
            gs.GridColumnStyles.Add(colName )

but because the col does not map to a col in my datasource it won't be displayed.
so what is the way around this, without having an empty col in my array and mapping to that?
0
allff
Asked:
allff
1 Solution
 
amyhxuCommented:
Adding an empty col to the datasource is an easier way. I would suggest you use this way if there's no specific reason not to do this.

But there is a workaround at http://www.syncfusion.com/FAQ/WinForms/   DataGrid section: Q 5.32
I'll just copy it's content here which is written in C#:

The idea is to create the 'bound' table in your dataset, and then add an extra 'unbound' column. The steps are to derive a custom columnstyle that overrides Paint where you calculate and draw the unbound value. You can also override Edit to prevent the user from selecting your unbound column. Then to get your datagrid to use this special column style, you create a tablestyle and add the column styles to it in the order you want the columns to appear in the datagrid. Here are code snippets that derive the column and use the derived column.
 

 
     // custom column style that is an unbound column
 
     public class DataGridUnboundColumn : DataGridTextBoxColumn
 
     {
 
          protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
 
          {
 
               //do not allow the unbound cell to become active
 
               if(this.MappingName == "UnBound")
 
                    return;
 

 
               base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
 
          }
 

 
          protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
 
          {
 
           
 
               //clear the cell
 
               g.FillRectangle(new SolidBrush(Color.White), bounds);
 

 
               //compute & draw the value
 
               //string s = string.Format("{0} row", rowNum);
 
               // col 0 + 2 chars from col 1
 
               DataGrid parent = this.DataGridTableStyle.DataGrid;
 
               string s = parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
 
               Font font = new Font("Arial", 8.25f);
 
               g.DrawString(s, font, new SolidBrush(Color.Black), bounds.X, bounds.Y);
 
               font.Dispose();
 
               
 
          }
 
     }
 

 
     
 
     //code that uses this unbound column
 
     private void Form1_Load(object sender, System.EventArgs e)
 
     {
 
          // Set the connection and sql strings
 
          // assumes your mdb file is in your root
 
          string connString = @"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb";
 
          string sqlString = "SELECT * FROM customers";
 

 
          OleDbDataAdapter dataAdapter = null;
 
          DataSet _dataSet = null;
 

 
          try
 
          {
 
               // Connection object
 
               OleDbConnection connection = new OleDbConnection(connString);
 

 
               // Create data adapter object
 
               dataAdapter = new OleDbDataAdapter(sqlString, connection);
 
               
 
               // Create a dataset object and fill with data using data adapter's Fill method
 
               _dataSet = new DataSet();
 
               dataAdapter.Fill(_dataSet, "customers");
 
               connection.Close();
 
          }
 
          catch(Exception ex)
 
          {      
 
               MessageBox.Show("Problem with DB access-\n\n connection: " 
 
                    + connString + "\r\n\r\n query: " + sqlString
 
                    + "\r\n\r\n\r\n" + ex.ToString());
 
               this.Close();
 
               return;
 
          }
 

 
          // Create a table style that will hold the new column style
 
          // that we set and also tie it to our customer's table from our DB
 
          DataGridTableStyle tableStyle = new DataGridTableStyle();
 
          tableStyle.MappingName = "customers";
 

 
          // since the dataset has things like field name and number of columns,
 
          // we will use those to create new columnstyles for the columns in our DB table
 
          int numCols = _dataSet.Tables["customers"].Columns.Count;
 

 
          //add an extra column at the end of our customers table
 
          _dataSet.Tables["customers"].Columns.Add("Unbound");
 

 
          DataGridTextBoxColumn aColumnTextColumn ;
 
          for(int i = 0; i < numCols; ++i)
 
          {
 
               aColumnTextColumn = new DataGridTextBoxColumn();
 
               aColumnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName;
 

 
               aColumnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName;
 
               tableStyle.GridColumnStyles.Add(aColumnTextColumn);
 

 
               //display the extra column after column 1.
 
               if( i == 1)
 
               {
 
                    DataGridUnboundColumn unboundColStyle = new DataGridUnboundColumn();
 
                    unboundColStyle.HeaderText = "UnBound";
 
                    unboundColStyle.MappingName = "UnBound";
 
                    tableStyle.GridColumnStyles.Add(unboundColStyle);                    }
 
               }
 
               
 
               // make the dataGrid use our new tablestyle and bind it to our table
 
               dataGrid1.TableStyles.Clear();
 
               dataGrid1.TableStyles.Add(tableStyle);
 
               dataGrid1.DataSource = _dataSet.Tables["customers"];
 

 
          }
 
     }
 

 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now