none
Is a calculated column possible in a DataGridView bound via DataContext? RRS feed

  • Question

  • I use a Linq to Sql query to fill a DataGridView, but I want to add a calculated column to the control whose value is based on another column in the DataGridView, basically a metric to Imperial conversion, so when one column displays a measurement value like Imperial Gallons, I would like the adjacent column to display the metric equivalent. I don't want to have to perform the calculation within the query itself, and would prefer if this could be set up at design time....can something like this be done?  Any and all suggestions gratefully accepted.

    Friday, July 22, 2011 11:45 PM

Answers

  • just add a column and insert value to it (and hide the original column):

    here I replace column status (which contains enums) with a nice string represtative of the enums

           if (gridview.Columns.Contains("Status"))
                {
                    gridview.Columns["Status"].Visible = false;

                    //replace with a nice name of status
                    gridview.Columns.Add("Formatted_status""current status");

                    foreach (DataGridViewRow dgvr in gridview.Rows)
                    {
                        TaskStatus ts = (TaskStatus)dgvr.Cells["Status"].Value;
                       dgvr.Cells["Formatted_status"].Value = TaskEnumsDisplayForGUI.GetNiceNameOfTaskStatus(ts);

                    }
                }
    do this after you binds the datagridview to a datasource.




    • Marked as answer by Larcolais Gong Thursday, August 4, 2011 7:02 AM
    • Unmarked as answer by jabberpunch Monday, August 8, 2011 5:14 PM
    • Marked as answer by jabberpunch Monday, August 8, 2011 5:14 PM
    Tuesday, July 26, 2011 8:04 PM

All replies

  • What you want to do is dynamically add a column to a data grid view after it has been bound to a data source. I don't know how to do that. But the solution of modifying the query is very clear and simple as I illustrate below. So why do you say "I don't want to have to perform the calculation within the query itself?  I cannot imagine the code to manipulate the data grid will be clearer and simpler than this.

     

    using System.Linq;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                PlaySQLDataContext dbContext = new PlaySQLDataContext();
                var barrels = from barrel in dbContext.Barrels
                              select new { 
                                  barrel.Description, 
                                  CapacityInImperialGallons = barrel.CapacityInImperialGallons.GetValueOrDefault(),
                                  CapacityInLiters = ConvertImperialGallonsToLiters(barrel.CapacityInImperialGallons.GetValueOrDefault())};
                dataGridView1.DataSource = barrels;
            }
     
            private double? ConvertImperialGallonsToLiters(double capacityInImperialGallons)
            {
                return capacityInImperialGallons*4.54609188;
            }
        }
    }
    

    Best Regards, David K Allen http://codecontracts.info Minneapolis, Minnesota, USA
    Sunday, July 24, 2011 6:45 PM
  • Hi David and thanks for the response. You're completely correct in saying that it's far easier to handle the conversion within the query itself, however, when I used "select new" in my original query I lost the ability to perform add/delete/update operations on the data within the datagridview, and with all the trouble I've had trying to bind a slightly more complex linq query to a datagridview control I felt that the tradeoff wasn't worth it.  For now I've implemented this as a Tool Tip Text that displays the conversion value when the mouse is held over a a particular column/row value.

    Monday, July 25, 2011 11:07 PM
  • just add a column and insert value to it (and hide the original column):

    here I replace column status (which contains enums) with a nice string represtative of the enums

           if (gridview.Columns.Contains("Status"))
                {
                    gridview.Columns["Status"].Visible = false;

                    //replace with a nice name of status
                    gridview.Columns.Add("Formatted_status""current status");

                    foreach (DataGridViewRow dgvr in gridview.Rows)
                    {
                        TaskStatus ts = (TaskStatus)dgvr.Cells["Status"].Value;
                       dgvr.Cells["Formatted_status"].Value = TaskEnumsDisplayForGUI.GetNiceNameOfTaskStatus(ts);

                    }
                }
    do this after you binds the datagridview to a datasource.




    • Marked as answer by Larcolais Gong Thursday, August 4, 2011 7:02 AM
    • Unmarked as answer by jabberpunch Monday, August 8, 2011 5:14 PM
    • Marked as answer by jabberpunch Monday, August 8, 2011 5:14 PM
    Tuesday, July 26, 2011 8:04 PM