I am generating columns dynamically in my RadGrid, so column count and their names are generated on the fly and so is the datatable that the grid is to be bound to.
Each column either represents an object in my system, or an empty one for user to create a new object. Each object is identified by a unique value property.
I would like to set this value to the column, so, when the grid is edited, I would like to use this value and update the object.
I am open for suggestions. For now I am thinking the options are
assigning the value to a property in GridTemplateColumn(which I dont think is possible)
use a hidden field in header of the column (how do I do it?)
To be more clear, both rows and columns are generated dynamically. I am using DataKeyNames for rows. Wondering how I should be storing unique values for columns!
Any help is appreciated.
Thanks!
I doubt if an official Telerik support exists here for this situation. One quirky solution will be to employ a row of hidden fields, one field per column, each storing unique value for the column.
You can add the unique value to one of the columns in the datatable and bind this column to grid. Make this column invisible so that it is not visible in the UI, but accessible in the backend.
Related
From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/
I have a Telerik RadGrid on my page which is used to display loads of different result sets (one at a time) based on various queries.
I need to default the DataKeyNames property to the first column regardless of what dataset is returned.
The problem I have is the the name of the first column changes from dataset to dataset.
Is there a way to default the property to the first column regardless?
I sorted it myself. Essentially just building the grid manually for each separate report, pain but it's done now.
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.
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 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.