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

Binding Dataset To RadGrid & Manipulating

6 Answers 233 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 10 Nov 2011, 12:43 PM
Hi there,

I've inherited a project that makes use of various Telerik controls throughout, but none moreso than RadGrids. For the most part the grids are declared and defined in the ASPX files and all their formatting set there, however what I want to do is move away from that and instead set them up in the code behind. I've spent the last hour looking through your site and the various pages, however nothing adequately provides answers to what I'm trying to do.

I call a stored procedure from a database and store the result in the dataset. The dataset therefore contains all the information I need and I can easily dump this out in to an ASP GridView control and manipulate it however I want. The rest of the project however is using the RadGrids, so I'm trying to do the very same thing with a RadGrid. To do this I did the following:

rgdTable.DataSource = myDataSet.Tables("myTable")
rgdTable.DataBind()

The problems I have began when I found the dataset I get back from the database had two columns in it that I didn't want to display. I assumed it would be as simple as setting the .Visible property on the two columns to false so the Grid wouldn't display them, however every attempt I've made to find exactly WHERE these columns are bound has resulted in errors. I've tried rgdTable.Columns and rgdTable.MasterTableView.Columns, along with looking in the DetailTables, and they always return a count of 0. If the columns ARE being bound and ARE being displayed, why is it that I can't access them programmatically?

DisplayInfo.InnerHtml = "<p>Master Table Column Count: " & rgdTable.MasterTableView.Columns.Count & "</p>"
DisplayInfo.InnerHtml = "<p>RadGrid Column Count: " & rgdTable.Columns.Count & "</p>"

Both of these (run individually, not together obviously) return a column count of 0. So where ARE these columns being bound to exactly? I need to be able to access them once they're bound to the grid to be able to change them as I see fit.

Any help you could provide would be appreciated.

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Nov 2011, 01:56 PM
Hello,

protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
    {
        if (e.Column.UniqueName == "your Compared name")
        {
 
        }
    }
//or
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach(GridColumn column in RadGrid1.MasterTableView.Columns)
        {
 
        }
    }

If you not able to get the autogenerated column then use below code.

RadGrid1.MasterTableView.AutoGenerateColumns

And you can also get column count in Page_prerender and Radgrid_Prerender.

Thanks,
Jayesh Goyani
0
Pete
Top achievements
Rank 1
answered on 10 Nov 2011, 02:31 PM
Hey Jayesh,

Thanks for that. Using the second method of your two didn't work, but the first one seems to have worked and finally gives me the control that I was looking for. Just a quick question though, is there a specific reason why you can't do manipulation on the columns BEFORE trying to render them? Will it ever be possible in a future version?

Thanks.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Nov 2011, 05:05 PM
0
Pete
Top achievements
Rank 1
answered on 16 Nov 2011, 04:36 PM
Hey again,

Thanks for all your help so far. That Events sequence was incredibly useful and I've been able to solve most queries with it, but I have just two more. The first I've got a work around for so it's not that big a deal, but it's annoying and I'd like to know if there's a proper way. The second is just bugging me and I can't solve it, but they're both related.

I've done all the databinding in the code behind and gotten the initial grid to display exactly as I want it. Now I'm looking to add a detail table on one of the grids. The problem is to generate the content of that detail table I need to run a stored procedure, and to run the stored procedure I need to get one of the values from the grid, so if I add it like this it works somewhat:

Dim myDetailTable As Telerik.Web.UI.GridTableView = New GridTableView
  
myDetailTable.Width = Unit.Percentage(100)
myDetailTable.Name = "DetailTable"
  
myRadGrid.MasterTableView.DetailTables.Add(myDetailTable)

That gives me a detail table that contains the same as my original table, which is useless until I run this:

Select Case e.CommandName
    Case RadGrid.ExpandCollapseCommandName
        Dim gdi As GridDataItem = e.Item.OwnerTableView.Items(e.Item.ItemIndexHierarchical)
        Dim myGUID As Guid
        Dim myDataSet As Data.DataSet
  
        If e.Item.OwnerTableView.Name = "MasterTable" Then
            myGUID = New Guid(gdi("ItemID").Text)
  
            myDataSet = GetDetailTableData(myGUID)
  
            e.Item.OwnerTableView.DetailTables(0).DataSource = myDataSet
        End If

That then replaces the makeshift detail table I created on PageLoad with the actual data I want in it. Part of the problem is that the Dataset I get back from the database can contain different columns, so I can't hardcode them, I NEED to set them to the datasource. The problem is that every time I click the button twice it does the myRadGrid.MasterTableView.DetailTables.Add(myDetailTable) command again, so I get many, many detail tables. I've gotten around it by doing this in the PageLoad, but it's not ideal.

Try
    If Not rgdTable.MasterTableView.DetailTables(0).Name = "DetailTable" Then
    End If
Catch ex As ArgumentOutOfRangeException
    rgdTable.MasterTableView.DetailTables.Add(myDetailTable)
End Try

What i wanted to do was say something like below.

If rgdTable.MasterTableView.DetailTables.Count = 0 Then
    rgdTable.MasterTableView.DetailTables.Add(myDetailTable)
End If

but that won't work due to the fact that there is no .Count function on the detail tables. If you could help with that I would appreciate it.




However, my Try/Catch statement DOES prevent that from being a problem. My bigger problem is that when I click to expand in to some of the DetailTables it randomly resizes the column widths in the MasterTable, which is just really ugly looking as it knocks the page completely out of sync. Could you tell me what to do to stop that from happening? Is there another event I can handle that I can put the code in to resize the columns to the widths I want to prevent this? I currently handle the ColumnCreated event that you suggested and it works a treat on creation, and I've tried PreRender but that seems to have no effect.

Thanks.
0
Pete
Top achievements
Rank 1
answered on 17 Nov 2011, 05:25 PM
Spent some of today trying all kinds of different options, seeing if I could size columns on pre-render, and then decided I would open the same form in Firefox as opposed to Internet Explorer and use Firebug to see if there was some fundamental change occurring... only to find that in Firefox it doesn't have the same rendering problem in resizing the columns.

The company I work for still uses Internet Explorer 8 by default and in Internet Explorer 8 whenever I expand in to a detail table it randomly resizes the column widths of the parent table.

Any help would be appreciated.
0
Veli
Telerik team
answered on 21 Nov 2011, 12:05 PM
Hello Pete,

For programmatically binding detail tables, the advisable approach would be to use RadGrid's DetailTableDataBind event. Here are a few help topics to get you started:

Hierarchical Databinding Using the DetailTableDataBind Event
RadGrid Programmatic Creation
Traversing Detail Tables / Items in RadGrid

To be able to advise you further on the resizing issue, we need to be able to reproduce it. If you can open a regular support ticket and send us a test page or a small project we can run locally, that would help us a lot trying to identify what the problem is.

Here is a general advise without knowing any particular details on the issue. Try to specify static width in pixels for all the columns but one. Use the HeaderStyle-Width property. The only column that does not get any width will be used as a buffer zone, taking any difference in column widths when you expand/collapse your items.

Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Pete
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Pete
Top achievements
Rank 1
Veli
Telerik team
Share this question
or