Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add new column to DataGridView

I have a DataGridView bound to a DataTable. The DataTable is populated from a database query. The table contains a column named BestBefore. BestBefore is a date formatted as a string (SQLite doesn't have date types).

I would like to programmatically add a new column to the DataGridView called Status. If BestBefore is less than the current date, Status value should be set to OK, otherwise Status value should be set to NOT OK.

I'm very new to Winforms, so some example code would be greatly appreciated.

UPDATE:

I think DataColumn.Expression is okay for doing simple calculations such multiplying a column's integer value by another value, but what about doing what I need to do? That is, calculate the difference between now and the date (string formatted) in the BestBefore column to determine what value to give the new status column. Example code would be appreciated.

like image 401
halfpint Avatar asked Apr 02 '11 15:04

halfpint


People also ask

How to add new column in DataGridView?

Right-click on the DataGridView in the designer and select Add Column. The Add Column dialog will appear. Select "Unbound Column." Change the Name of the column to "itemTotalDataGridViewTextBoxColumn".

How to add column to DataGridView dynamically in c#?

The first column should display the measurement variable(height, weight, plood pressure, etc). To start the examination user click on start examination button. On click of start new examination a new column should be added to grid to enter examination data against measurement variable.


1 Answers

Keep it simple

dataGridView1.Columns.Add("newColumnName", "Column Name in Text"); 

To add rows

dataGridView1.Rows.Add("Value for column#1"); // [,"column 2",...] 
like image 152
Rusty Avatar answered Oct 07 '22 04:10

Rusty