Customise a bound datagridview - c#

Howdy, using vs2008 winforms.
I want to be able to use a slightly customised datagridview but cant think of a way to do it.
i have
1. a sqldataadaptor fill a dataset
2. a binding source bound to the dataset
3 a datagridview with the bindingsource set as the datasource.
I want the binding to allow sync between the dataset and datagridview, so i can edit data and then update to database with sqldataadaptor. update.
I want to show some custom columns that are calculated results.
And i want to show a final bottom row that is totals of all the columns in the DVG.
My problem is once the DGV is bound i cant add a custom column or row, it wont let me.
I know i could add it directly to the dataset that is the underlying datasource but then by changing the structure of the dataset i cant update to a database once edits have taken place.
or can i ???
Can someone tell me how i can add my custom columns and a final total row to a bound DGV.
Also while im here, if i click on the top of a column to sort on it, in a bound DGV, will it also re-sort the underlying dataset, so i i edit things will still stay synced ?
thanks in advance for any help

Yes, you can.
The adapter does not care about structure. It only cares about column names used in the Select/Insert/Update/Delete commands. You can add custom columns, expression columns, columns for subtotals, columns for totals, or whatever you need. Even change the order of the columns. I do advise adding a sort column, so you can keep the custom rows in the proper place when you or the user sorts.
You can do the same thing for custom rows. When you update through the adapter, you handle the RowUpdating event and set the SqlRowUpdatingEventArgs.Status to SkipCurrentRow for these custom rows. I strongly advise creating a row type column so you know which rows to skip when updating. (You can also use the sort column's value as a key to skip rows.)
There are a couple of articles on MSDN or KB that illustrate how to add total rows.
"How to sum the fields in a Windows Forms DataGrid control and then display the calculated totals in a footer by using Visual Basic .NET" at http://support.microsoft.com/kb/836672.
In my experience, manipulate the data table first before binding. Do what you need to do in order to support the grid and the grid's interface. It is okay to manipulate the data, add/change/remove columns and rows, then rebind when needed.

To be able to add additional unbound columns to your DataGridView you could also set its AutoGenerateColumns property to false and then add custom columns with the following method:
dataGridView->Columns->Add(...)
To calculate the contents of the unbound columns you can handle the CellValueNeeded and CellValuePushed events but you must set the DataGridView into 'virtual mode' by setting the VirtualMode property to true in order for the eventhandlers to be invoked.
Hope this helps a little....

Related

DomainUpDown control inside the GridView

I want to add DomainUpDown control inside a DataGridView as a separate column. Each time a new row is added, I want that DomainUpDown control also be added. I also want to know when the user click the DomainUp or DomainDown button in DomainUpDown control for each row. Is it possible or not? I am using language vb.net/C#. I want to do this in winform not in ASP. Any help or suggestion please!!!
All you need to do/know is how to add an unbound column to an already data bound DataGridView, because from your description you are trying to add a column to a grid which already contains other data bound columns. In fact it would be easier if you had such column in the DataTable, also because with unbound columns you need to make a slightly bigger effort to populate and save the data from/to somewhere.
anyway it should be possible, MSDN explains it here: How to: Add an Unbound Column to a Data-Bound Windows Forms DataGridView Control

about repositoryItemCheckEdit

I've added repositoryItemCheckEdit in a xtragrid control's column. And i'm having total 3 columns in my xtragridview. From which two columns will be filled with the help of fields from my table when i'm setting datasource property of gridcontrol.
problem which is i'm facing is that when i'm changing the status of checkbox from xtragrid to checked for one time it works but when i'm checking from next row that time the previous selection of checkbox from gridview lost. It only maintain for only one row. and i want it two remain for multiple row.
how to do this?
my datatable contains two fields as TemplateTitle & TemplateBody. And my xtragrid contains three columns as two from datatable & additional column as a repositoryItemCheckEdit which is unbound column. I created columns of gridview at design time. and i've set just datasource property of gridcontrol to datatable.
thanks.
If the repositoryItemCheckEdit is unbound you have to control the CheckEdit's checked state manually using the grid's CustomUnboundColumnData event otherwise the state is lost when you move to a different row.
You must search DevExpress support center for these type of issues. These are pretty well documented and can be found easily there.
http://www.devexpress.com/Support/Center/p/B135631.aspx
http://www.devexpress.com/Support/Center/p/Q254784.aspx
Using BindingList and not DataTable or ObservableCollection solves the problem for booleans bound to a checkbox in a (DevExpress) GridView/GridControl ( Windows Forms ).

DataGridView Custom Sorting With Datasource

In my C# .NET application, I have a DataGridView. The grid's DataSource is a BindingSource, and the BindSource is bound to a filtered DataView of a DataTable that is requeried from SQL frequently.
I can already sort columns in the grid, but the sort is always done alphabetically on the string version of whatever data value is showing in the cells. I have some fields that display a name, but need to be ordered by a specific value instead of alphabetically. I also have a field that can either be a number or the word "Unknown". I need to be able to customize the logic behind the sort.
I know I can't use the grid's SortCompare event because I am using a bound DataSource. I have tried creating columns that display a custom class that derive from IComparable, but sorting on that column still always sorts alphabetically instead of using my Compare() method.
I found one possible solution that involves keeping a hidden DataGridViewColumn that stores a number for sorting on, and using the grid's ColumnHeaderMouseClick event to force it to sort on that hidden "sort" column when the user clicks on the "display" column. However, the column header's "sort glyph" (the little up/down arrow icon that shows up on the sorted column) won't shot up because the actual sorted column is hidden. I have tried manually setting the column's HeaderCell.SortedGlyphDirection property, but the icon will always be hidden if the BindingSource's sort is not for that column.
I'm pretty sure I could get this working if, instead of a DataView of a DataTable, I used a sortable List of a custom wrapper objects I create for each data row, but I don't want to resort to that if possible. But if that's my only option, that's what I'll do.

How to control Column Type in DataGridView that is bound to a CustomObject?

I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?
The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.
As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.
You can pre-create your columns in the designer. If the name of the column matches the name of the property the column will end up bound to, the databinding will take care of the DGV population for you as before.

How do I create a DataGrid in C#?

I want to add a DataGrid to a Form. When the program executes, the user enters values and I use those values in the problem. I need a similar implementation for a table with two columns and when the user enters values I use them to for calculation in the program.
There is no requirement to save these values to a database, they are just going to be used in the program.
How do I do this in C#?
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)
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
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.
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....

Categories

Resources