This is a migrated thread and some comments may be shown as answers.

Need just first row in grid to be all comboboxes

5 Answers 193 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 28 Sep 2011, 05:39 PM
I've been looking at RadGridView for a couple of hours now and don't yet see where to start on this issue. Here's what I want to do...

I have a grid that I am using to display objects in a  BindingList. My view is watching the BindingList and creating grid rows as required. There are 7 objects in the list with 4 text fields on each. Right now, the values display as text in the grid with 7 rows x 4 columns. I need to add a special row to the top of the list that will be frozen, i.e., will not be scrolled when the user scrolls the records vertically. This row will have all comboboxes that will have a fixed number of items and will be presented as a drop list and not be editable. (As soon as I get this row in, I will have to add a second special row with all comboboxes but they will be edtiable.) The grid then should show 8 rows - my special row and the 7 rows for the items.

This all has to be done programmatically. Consider the concrete example: The program will read a delimited file and determine the number of columns that it has. It creates a new GridViewDataColumn for each. It will add the special row with each combobox having the options { int, double, string, DateTime }. And then it read the lines from the delimited file and displays them in the grid.

What I was hoping to see was a GridViewComoboxCell class so I could create a new GridViewRow and add GridViewComoboxCell(s) to the row.Items list. Alas, no such class. Also, I have set binding through the column definition as follows:

gridViewColumn.DataMemberBinding = new Binding(string.Format("[{0}]", i));

When I try to create the special row (that I cannot get to display), I get the following error when adding the row to the grid:

Indexer with specified arguments cannot be found on type GridViewRow
Parameter name: indexerArguments

This indicates that it is using the defined column binding on my new cells - and that won't work.

Suggestions on how to approach this problem would be very much appreciated.

5 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 28 Sep 2011, 08:51 PM
I've done this twice in the last year, once with MS DataGridView and with XtraGrid. I could kick the grid into virtual mode so it would call me back when it needed a cell value. I just told the grid it had an extra line and I handled the data for row 0 differently than the rest of the lines. I could easily put my edit/combobox control into place and all would work well.

I suspect that I am missing something quite simple with this grid. I get things to work when adding data items directly to the grid (Items[]) and setting the DataMemberBinding for the column to the item on the object that I want to display. What I cannot do is create a GridViewRow with GridViewCell(s) and get the grid to display anything. I see the rows but I cannot get the data to render. My thinking is if I can get control down to the cell level then maybe I can find a path to what I want to do.

This shouldn't be this hard. I'm trying to think laterally, e.g., maybe there is another approach. It really makes the most sense to keep the assignments of the attributes in the grid so the user can scan the data and make the assignments.
0
Maya
Telerik team
answered on 29 Sep 2011, 09:51 AM
Hi Scott,

Generally, it is not recommended to work with the visual elements as the virtualization of the grid is turned on by default and all those elements will be recycled and reused on scrolling. That being said, you cannot add a row to the grid, you should always work with the data items instead and add an new item, not a new row.
Considering your specific scenario, what I could suggest is to create a template selector and define a specific template for the cells of a particular row that you want to have all those combo boxes. 
As for pinning this row to the top, you can run through this blog post for a reference.
 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Scott
Top achievements
Rank 1
answered on 29 Sep 2011, 02:47 PM
Maya,

I understand your comments on virtualization being turned on by default and how the cells are reused during scrolling. I used to count on that behavior in some other grid-related work. That's exactly how I got control of what was being displayed for each cell - I supplied it during the OnCellValueNeeded call.

I almost have the method of creating a GridViewRow with attached GridViewCells working. My difficulty is controlling what is being displayed as text in the cell. I get the following: Telerik.Windows.Controls.GridView.GridViewCell: 1/1/2011 01:01:01. The date/time portion of the string is correct but the GridViewCell seems to be injecting its type as a prefix to every cell text.

Since you are recommending allowing the grid control to create rows based on my observable collection, I will abandon this approach and look at your suggested approach. I anticipate a significant problem in having to add a dummy record into the collection. And, I will have to make sure that the corresponding record appears at the top of the grid and then freeze/pin the row so the other data rows will appear below it and it can be scrolled. This is quite a code smell. Again, with all the filtering and sorting functionality, I am skeptical that I can come up with a solution that is robust.

I was given a second suggestion elsewhere (http://stackoverflow.com/questions/7589912/setting-controls-in-a-telerik-gridview-cell). The solution is to create a custom column and override the request for the editor. I may be able to get comboboxes along the first row but still have the same problem of creating a dummy data record and making sure that it is always associated with the first row.

Are these really my choices, or am I missing some functionality of the grid? Is there a header-row collection? E.g., can I add rows that will not be bound to the Items/ItemsSouce and they will appear at the top?

Thanks!


Whoa! There is no standard way of freezing a row?! Since there's no row collection, this won't work: _grid.Rows[0].IsPinned = true;

IMO, these are two very basic features of a grid. At this point, I want the grid to get out of my way. I want to put it into a mode where I tell it the number of records that it has. I then want it to call me back for the cell content when it is needed. I want to be able to put whatever control I want in any cell. I want to be able to tell the grid to pin the first n rows at the top. Yes, this certainly breaks my MVVM pattern but, between getting the job done and philosophy, the job needs to get done. Will post whatever workaround I can put into place.
0
Scott
Top achievements
Rank 1
answered on 29 Sep 2011, 08:14 PM
I've evaluated the custom column method and it gets 80% of the way but has one big drawback. It only shows the combobox when the cell is in edit mode. I want the combobox to always be visible as a visual cue. This cue tells the user that the 0th row is unique and that he may interact with it. I've discussed this a little bit at StackOverflow.

I've worked through quite a bit with the DataTemplateSelector logic. It appears that it just might work as expected. Looks like a rather complex solution. The complexity arises since the control binding is dynamic since the columns are dynamic. I had to create the two templates (one based on ComboBox, the other on TextBlock), create the selector and attach it to the cell via CellTemplateSelector. I'll post the gist of it when I get it all sorted out.
0
Maya
Telerik team
answered on 04 Oct 2011, 12:44 PM
Hi Scott,

Would take a look at this forum thread ? Will that approach be somehow helpful for the scenario that you want to achieve ? 

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Maya
Telerik team
Share this question
or