views:

1172

answers:

4

here i am not using any database to populate value . i want to add a datagrid to a form .and when i execute it user enters value and i use them . i need a similar implementation of table for eg i have two columns and user enters values i use them for calculation .

here i am not populating values from or to a database

+2  A: 

In a winforms environment, you can bind strongly typed collections as the datasource; Each property of the objects in the collection makes a column (Strictly speaking, I believe it works out the properties for the type that the collection returns, rather than the individual items in it)

Rowland Shaw
+1  A: 

If you are writing a WinForms App then you can use a DataTable to store the data and a DataGridView to display it. Simply create the DataTable:

dataTable = new DataTable();

Create the columns you need programatically:

var columnSpec = new DataColumn();
columSpec.DataType = typeof(decimal);  // If it holds a decimal
columSpec.ColumnName = "Interest Rate";
dataTable.Columns.Add(columnSpec);

Add the DataGridView to the form using the Designer - but don't and then once the table has been created bind it to the grid using:

dataGridView.DataSource = dataTable;

You can set the properties on the grid from the designer view.

I have done this in a read-only case where the DataTable is populated from the program and just displayed it. All the user can do is resize, reorder or set the visibility on the columns. To add new rows you'll need to hook into the RowsAdded event

ChrisF
A: 

Re-wording Rowland Shaw

You need not have a database to bind to the datagrid. If you have the data filled in a strongly typed or a generic collection you can bind the datagrid to the collection. The datagrid will fill the data from the collection.

It will take the property names as the columns, and the rows will display as per the rows in the collection.

If you want user input, then I think you should consider using a better grid control. The datagrid is not suitable for this purpose. I dont' remember if flexgrid (the ocx one) has been redone for .Net.

Cyril Gupta
A: 

You can use a datagridview and build a datatable and add columns through your code and set the datatasource of your datagridview as this datatable and set AllowUserToAddRows in properties window of Dataggridview to true ( true is the default value ).

If you want to make the calculation after a full ro update is made then you can use RowPrePaint event or if you want the calculation to be made after the data in each cell gets updated then you can use the CurrentCellChanged event of the datagridview.

Hope this helps....

rahul