Defaulting the DataKeyNames on a RADGrid to the first column - c#

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.

Related

Telerik RadGrid using Multiple Columns

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.

Telerik Radgrid Can we save a unique value against each column?

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.

hide columns by Id in gridview in asp.net

I have the asp.net application where I am having the editable grid view with the edit,Delete,Add options. this grid having as usual Template fields . I want to hide some columns. I know i can do that by using columns index. but i don't want to follow it. instead I want to hide columns by Id. this is because if in my application further I need to add more columns then there is need to change the code gain and again in the core. so I am choosing this way. Bu as i found <asp:TemplateField /> does not contain Id attribute. so it is become impossible for me to hide <asp:TemplateField/> by Id. any remedy for this ?
You can always do it using column header text. Run a loop through columns of the grid view and compare header text of the column with the constant declared and hide the column, this way, if you are adding any number of columns before or after the column will not affect the code.
Maybe create a div with an id inside a template field and hide that div with client code.
Perhaps just have a different grid markup for each "view" you want to show to the user, so one grid with all the columns, and another with limited columns.
I am assuming you would always be binding the same data to the grid (regardless of whether you are hiding/showing columns), so then you would just need some logic in your code to determine what "view" you want to show the user (i.e. what grid you want to bind to).

Customise a bound datagridview

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....

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.

Categories

Resources