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

Create detailTable programmatically

8 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amanda
Top achievements
Rank 2
Amanda asked on 30 Jul 2008, 03:36 PM
I'm using VS 2008 , .NET 3.5.  I have a grid that is pulling data from one of my SQL database tables.  I create a datatable, populate it, then use the grid's datasource for this datatable.  This works just fine. 
I have another SQL table that holds an image (along with other info) that relates/links to my first table.  I want to have this second table information in the grid's detail table.  So for each entry, the table will expand and show the 2 entries from the second table that relates to the one entry in the first table.  Here's my code so far:

  Dim dt As Data.DataTable  
        dt = dbobj.CreateDT(strsql, JobMastModel.DBClasses.DBTools.DB.DW)  
        RadGrid1.DataSource = dt  
        RadGrid1.DataBind()  
        dbobj.DBConnMain.JMConnection.Close()  
 
        'Detail table (has files)  
        Dim dtFiles As Data.DataTable  
        Dim files As Telerik.Web.UI.GridTableView = New Telerik.Web.UI.GridTableView  
        Dim strFile As String 
        strFile = "SELECT * from tblDGdrawings WHERE dg_id = '" & dt.Rows(0).Item("id") & "'" 
        dtFiles = dbobj.CreateDT(strFile, JobMastModel.DBClasses.DBTools.DB.DW)  
        
        files.DataSource = dtFiles  
        RadGrid1.MasterTableView.DetailTables.Add(files) 


The grid shows the expand button, but nothing happens when I try clicking on it.  What else do I need to add?  The grid shows the expand button, but nothing happens when I try clicking on it.  What else do I need to add?
Another problem is that on line 11 I'm grabbing the files from the second table WHERE the id equals only the one number.  How do I write that SQL statement so it would relate to each individual entry in the first grid/table? Also, in the detail tables, I want the user to be able to click the row and it'll give me the option to open the file that's in the database.
 Please let me know if you need any other informaiton.

Oh, here's a simplified version of my table structures
Table 1(tblDGinfo) - id, drawing_number, school_name, dealer, lab_type, etc.
Table2(tblDGdrawings) - id, dg_id, attachment(image), type, length, etc.
tblDGinfo.id relates to tblDGdrawings.dg_id

8 Answers, 1 is accepted

Sort by
0
Vladimir
Top achievements
Rank 1
answered on 30 Jul 2008, 07:05 PM
0
Daniel
Telerik team
answered on 30 Jul 2008, 10:12 PM
Hello Amanda,

If you create your RadGrid on Load event you should keep in mind that you need to first add the columns/table and then set their properties.

Please check the following demos illustrating the approach:
Create hierarchical RadGrid on Load
Create hierarchical RadGrid on Init

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amanda
Top achievements
Rank 2
answered on 31 Jul 2008, 01:51 PM
Vlad, that helped a lot!!!!  Thanks!!!  I am having a problem though opening up my file.  I have this code going when I click the image button in the detail table:

 Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand  
        'Try  
        Dim selected As Integer = e.Item.Cells(2).Text  
        Dim commandname As String = e.CommandName  
 
        If commandname = "ExpandCollapse" Then 
 
        Else 
            Dim attachments As Data.DataTable = New Data.DataTable  
            attachments = dbobj.CreateDT("Select * from tblDGdrawings where id = '" & selected & "'", JobMastModel.DBClasses.DBTools.DB.DW)  
 
            Dim data() As Byte 
            Dim type, DownloadFileName, description As String 
            Dim length As Integer 
 
            data = attachments.Rows(0).Item("attachment")  
 
            type = attachments.Rows(0).Item("type")  
            DownloadFileName = attachments.Rows(0).Item("downloadName")  
            length = attachments.Rows(0).Item("length")  
            description = attachments.Rows(0).Item("description")  
            Try 
 
             
                With Page.Response  
                    .Clear()  
                    .ContentType = type  
                    '.ContentType = "application/octet-stream"  
                    .BinaryWrite(data)  
                    If DownloadFileName <> "" Then 
                        Page.Response.AddHeader("content-disposition", _  
                          "attachment; filename=" & DownloadFileName)  
                        Dim s As String = "" 
                    End If 
                    .OutputStream.Write(data, 0, length)  
                    .End()  
                End With 
            Catch ex As Exception  
                Dim flag4 As String = ex.ToString  
                Dim flag3 As String = "" 
            End Try 
            
        End If 
    End Sub 

At line 36, it errors out. Actually, vb just closes right up.  Here's the ex.String:

System.Threading.ThreadAbortException: Thread was being aborted.

   at System.Threading.Thread.AbortInternal()

   at System.Threading.Thread.Abort(Object stateInfo)

   at System.Web.HttpResponse.End()

   at Default2.RadGrid1_ItemCommand(Object source, GridCommandEventArgs e) in C:\Inetpub\Programs\DesignGuide\Search.aspx.vb:line 263

Would anyone have any idea why this is happening?  I have this exact code in another program and it works just fine.  A popup box comes up asking if the user wants to save or open the file.  Why not here?
Here's my aspx code just in case for the grid.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"   
            GridLines="None">  
            <MasterTableView DataKeyNames="id">  
                          
             <DetailTables> 
                    <telerik:GridTableView DataKeyNames="id" Width="100%" GridLines="Horizontal">  
                        <Columns> 
                            <telerik:GridBoundColumn SortExpression="id" HeaderText="ID" HeaderButtonType="TextButton" 
                                DataField="id" Visible ="false">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn SortExpression="dg_id" HeaderText="dg_id" HeaderButtonType="TextButton" 
                                DataField="dg_id" UniqueName="dg_id" Visible="false">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn SortExpression="description" HeaderText="Description" HeaderButtonType="TextButton" 
                                DataField="description" UniqueName="description">  
                                </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn SortExpression="downloadName" HeaderText="File" HeaderButtonType="TextButton" 
                                DataField="downloadName" UniqueName="downloadName">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn SortExpression="date_added" HeaderText="Date Added" HeaderButtonType="TextButton" 
                                DataField="date_added" UniqueName="date_added">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridButtonColumn SortExpression="attachment" ButtonType="ImageButton" HeaderText="" HeaderButtonType="TextButton" 
                                UniqueName="attachment" ImageUrl="~/images/file1.jpg">  
                             </telerik:GridButtonColumn> 
                        </Columns> 
                    </telerik:GridTableView> 
                </DetailTables> 
 
                <RowIndicatorColumn Visible="False">  
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Resizable="False" Visible="False">  
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="id" SortExpression="id" UniqueName="column"   
                        Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="dg_id" SortExpression="dg_id"   
                        UniqueName="column1" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="school_name" HeaderText="School"   
                        SortExpression="school_name" UniqueName="column2">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="lab_type" HeaderText="Lab Type"   
                        SortExpression="lab_type" UniqueName="column3">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="student_capacity"   
                        HeaderText="Student Capacity" SortExpression="student_capacity"   
                        UniqueName="column4">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="square_feet" HeaderText="Square Footage"   
                        SortExpression="square_feet" UniqueName="column5">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="width" HeaderText="Width"   
                        SortExpression="width" UniqueName="column6">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="length" HeaderText="Length"   
                        SortExpression="length" UniqueName="column7">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="perimeter" SortExpression="perimeter"   
                        UniqueName="column8" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="peninsula" SortExpression="peninsula"   
                        UniqueName="column9" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="octagon_island"   
                        SortExpression="octagon_island" UniqueName="column10" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="forward_facing_island"   
                        SortExpression="forward_facing_island" UniqueName="column11" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="rectangular_island"   
                        SortExpression="rectangular_island" UniqueName="column12" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="tables" SortExpression="tables"   
                        UniqueName="column13" Visible="False">  
                    </telerik:GridBoundColumn> 
                </Columns> 
                <EditFormSettings> 
                    <PopUpSettings ScrollBars="None" /> 
                </EditFormSettings> 
            </MasterTableView> 
        </telerik:RadGrid> 

And the detail table data bind event:
Protected Sub RadGrid1_DetailTableDataBind(ByVal source As ObjectByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind  
        Dim dataItem As Telerik.Web.UI.GridDataItem = CType(e.DetailTableView.ParentItem, Telerik.Web.UI.GridDataItem)  
        Dim DGID As String = dataItem.GetDataKeyValue("id").ToString()  
        e.DetailTableView.DataSource = dbobj.CreateDT("SELECT * FROM tblDGdrawings WHERE dg_id = '" & DGID & "'", JobMastModel.DBClasses.DBTools.DB.DW)  
    End Sub 
Please let me know if anything else is needed.  Thanks!

0
Daniel
Telerik team
answered on 04 Aug 2008, 05:15 PM
Hi Amanda,

BinaryWrite and Write are not compatible with AJAX. Please read the following article in MSDN: UpdatePanel Control Overview and especially "Properties and Methods not Supported During Asynchronous Postbacks".
It is possible that you previously run the same code in a non-AJAX website.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amanda
Top achievements
Rank 2
answered on 04 Aug 2008, 06:07 PM
Ah, I see, and it actually works.  When I take the grid out of the AjaxPanel, i get the popup box giving the options to save or open.  Although, when i click open, adobe opens up, but I get an error on every file.

There was an error opening this document.  The file is damaged and could not be repaired.  Here's the database table structure:

id int 
dg_id int 
attachment image 
type nvarchar(50)
length int 
downloadName nvarchar(100) 
description nvarchar(200) 
date_added datetime 

and an example of an entry:
5 2 <Binary data> application/pdf 1523066 Gen2 RockyPoint 3D.pdf Rocky Point 3D 5/6/2008 12:00:00 AM
3 1 <Binary data> application/pdf 975062 Gen1 GlenbrookSouthHS 3D.pdf Glenbrook South 5/6/2008 2:37:44 PM

Does anyone know why i can't open the files?
0
Daniel
Telerik team
answered on 07 Aug 2008, 05:29 AM
Hi Amanda,

Could you please try to replace BinaryWrite with WriteFile and let us know whether this helps.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Amanda
Top achievements
Rank 2
answered on 07 Aug 2008, 02:06 PM
I'm not sure how that works because when I do .WriteFile( ), in the parentheses I have to put the name of a file to write to the http from.  I don't actually have that.  Although when I put the "downloadName" in, i get an error saying the file name does not start with '%pdf-'.  I tried just putting in that string, but it doesn't work.  do you know how I would actually use this command correctly withthe data that i have?  I can't put the byte variable 'data' in the argument because it doesn't accept a '-1 dimensional array'.  It's looking for a string.
0
Bruno
Top achievements
Rank 2
answered on 11 Aug 2008, 12:18 PM
Hi,

Try setting your Content Type to
Response.ContentType="application/pdf"

You can find a very good approach supporting 'download resume' managers: Handling Custom File Downloads in ASP.NET With Server Controls

also this link could be helpful:
Downloading Files - Forcing the File Download Dialog

Additionally you may want to exclude a control using the following code:
ScriptManager.GetCurrent(Page).RegisterPostBackControl(myControl) 

This line will register the control as postback trigger, instead of asynchronous.

Tags
Grid
Asked by
Amanda
Top achievements
Rank 2
Answers by
Vladimir
Top achievements
Rank 1
Daniel
Telerik team
Amanda
Top achievements
Rank 2
Bruno
Top achievements
Rank 2
Share this question
or