I have a DataSet that I create at runtime. I would like to display this information in a datagrid using wpf and have it display certain columns chosen at runtime.
I can get the data to display using:
datagrid.ItemsSource = dataset.Tables[0].DefaultView;
datagrid.DataContext = dataset.Tables[0];
How do I use DataGridTemplateColumn to add columns to the data grid and have my dataset's information displayed in them or other arbitrary data (numeration, etc)?
Thank you.
Check if these previous StackOverflow questions answer yours:
Generating columns dynamically in the WPF DataGrid?
programatically add column & rows to WPF Datagrid
(Here is the search I used).
Basically you need to add columns to the datagrid, and set their binding (using the Binding property). There are a variety of columns to choose from, you want one that derives from DataGridBoundColumn such as DataGridTextColumn.
Related
i have that code in my desktop application that fill the datagrid from mysql database
var mA = new MySql.Data.MySqlClient.MySqlDataAdapter("SELECT * FROM items", DataHolder.MySqlConnection);
var mT = new System.Data.DataTable();
mA.Fill(mT);
dataGrid.ItemsSource = mT.DefaultView;
but i can't figure out how to bind it to change certain cells in certain column to change it's background color when it's equl to certain values.
I have read Change DataGrid cell colour based on values, but in my case there is no columns to bind in the Data Grid until the code is executed. What I am asking is "how to make it work with that"?!
i know how to change the background color for datagrid that already have columns in it but in that case there isn't no columns to bind in the datagrid until the code is executed
This implies that you are using the auto generated columns. You just need to hook AutoGeneratingColumn or AutoGeneratedColumns, which give you access to the new columns in the event args, which you then can style accordingly (would recommend defining styles in XAML resources somewhere).
Background to the Question
Working in C#
I have a datagridview that is populated by a generic db method that exposes a data adapter and a binding source. The binding source is the datasource for the datagrid.
e.g. dgrid.DataSource=bindingSource;
I use the same generic db method to retrieve data from multiple tables and the same datagrid to display the data retrieved - so by varying the sqlcommand the datagrid display data from any specified db table on a SINGLE form using a SINGLE datagrid.
This reusable method allows me to displays data from any table in a single datagrid on a single form.
Logical Flow
Data from DB-->Assigned To BindingSource-->Assigned To DataGrid
The QUESTION
Typically I would reorder the columns on the datagrid using the 'Edit Columns' dialog but the datagridview in this case only exists at run-time.
Since the datagrid column names are not known until runtime ie until the binding source is invoked how do I programmatically access the column names, once the binding is complete, in order to reorder the columns and to facilitate further data manipulation?
Thanks in advance.
Just to know the column name after binding has been done, you can use :
GridView1.Columns[Index].HeaderText
I am having a problem with the RadGrid using Multiple Columns when I Bind a DataSource to the RadGrid. Firstly let me show some screenshots of what I am trying to do.
This is an example from Teleriks website under demos and the multiple columns (summary column) is what I am trying to achieve
This is the exception I am getting when I try and bind the control to a DataSource
This is the RadGrid I have set up in my ASP page.
Firstly I don't want to bind the datasource in my asp page. Secondly I wanted the "Product" and "Other" ColumnGroups to be used only as a Summary column and not to be bound to a datasource since these ColumnGroups are only used as headers for the other Columns.
Is there a workaround to not allow the ColumnGroups not to be bound to a DataSource while the normal columns are?
Firstly, each one of your columns is specifying a ColumnGroupName that does not exist. Your two groups are called OtherDetails and ProductDetails not Product and Other. Change them to resolve the exception.
Secondly, the GridColumGroups are simply groups, as they are not bound to any data from your datasource; they are simply for aesthetics.
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....
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....