This is a migrated thread and some comments may be shown as answers.
Dynamic Grid DataKeyName
1 Answer 42 Views
This is a migrated thread and some comments may be shown as answers.
Suresh K
Top achievements
Rank 1
Suresh K asked on 30 Jan 2010, 02:55 PM
hi.
i am designing grid Dynamically like this with Detail Tables.

    RadGridAudioFileReport = New RadGrid 
        Dim boundColumnClientID, boundColumnClient, boundColumnTotal As New GridBoundColumn 
        Dim templatecolumnActive, templatecolumnSupport, templatecolumnFinal As New GridTemplateColumn 
        RadGridAudioFileReport.DataSource = Nothing 
        RadGridAudioFileReport.DataSource = Fill_Allocation_Report_Grid() 
        RadGridAudioFileReport.Width = Unit.Pixel(2600) 
        RadGridAudioFileReport.Skin = "Office2007" 
        RadGridAudioFileReport.AutoGenerateColumns = False 
        RadGridAudioFileReport.MasterTableView.DataKeyNames = New String() {"Client_ID"} 
        boundColumnClientID.HeaderText = "Client_ID" 
        boundColumnClientID.DataField = "Client_ID" 
        boundColumnClientID.UniqueName = "Client_ID" 
        boundColumnClientID.Visible = False 
        RadGridAudioFileReport.MasterTableView.Columns.Add(boundColumnClientID) 
        boundColumnClient.HeaderText = "Client" 
        boundColumnClient.DataField = "Client" 
        boundColumnClient.UniqueName = "Client" 
        RadGridAudioFileReport.MasterTableView.Columns.Add(boundColumnClient) 
        boundColumnTotal.HeaderText = "Total" 
        boundColumnTotal.DataField = "Total" 
        boundColumnTotal.UniqueName = "Total" 
    templatecolumnActive.HeaderTemplate = New MyTemplateActiveHeader() 
        templatecolumnActive.ItemTemplate = New MyTemplateActiveItems() 
        RadGridAudioFileReport.MasterTableView.Columns.Add(templatecolumnActive) 
     
 
    Dim tableViewAccounts As New GridTableView(RadGridAudioFileReport) 
        Dim boundColumnAccountID, boundColumnAccount As New GridBoundColumn 
        tableViewAccounts.DataSource = Accounts_FileAllocation_Report_Data() 
        tableViewAccounts.Width = Unit.Percentage(100) 
        tableViewAccounts.AutoGenerateColumns = False 
        tableViewAccounts.DataKeyNames = New String() {"accountid"} 
        Dim relationFields As GridRelationFields = New GridRelationFields() 
        tableViewAccounts.ParentTableRelation.Add(relationFields) 
        RadGridAudioFileReport.MasterTableView.DetailTables.Add(tableViewAccounts) 
 
        boundColumnAccountID.DataField = "accountid" 
        boundColumnAccountID.HeaderText = "accountid" 
        boundColumnAccountID.UniqueName = "accountid" 
        'boundColumnAccountID.Visible = False 
        tableViewAccounts.Columns.Add(boundColumnAccountID) 
        boundColumnAccount.DataField = "account" 
        boundColumnAccount.HeaderText = "Account" 
        boundColumnAccount.UniqueName = "Account" 
        tableViewAccounts.Columns.Add(boundColumnAccount) 
        RadGridAudioFileReport.DataBind() 
        Me.PlaceHolder1.Controls.Add(RadGridAudioFileReport) 
 
 
 Public Function Accounts_FileAllocation_Report_Data(ByVal client_id As String) 
 
        MsgBox(client_id.ToString()) 
        cmd = New MySqlCommand("SELECT id as accountid,accname as account FROM account_master where ccode='" & client_id & "' and delflag=0;", con) 
        myda = New MySqlDataAdapter(cmd) 
        AccountsActiveDT = New DataTable 
        myda.Fill(AccountsActiveDT) 
        Return AccountsActiveDT 
    End Function 
 
here i want the DatakeyNames to get the data for Detail Tables.
how can i get the DataKeyName("Client_ID") and how can i pass the DataKeyName to this function Accounts_FileAllocation_Report_Data().
can any one help me please.

Thank You.
Suresh K.






1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 01 Feb 2010, 08:02 AM
Hi

You can use the AdvancedDataBinding Techqniue to bind the RadGrid. Attach the NeedDataSource  and DetailTableDataBind   events  to the RadGrid 

   RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
   RadGrid1.DetailTableDataBind += new GridDetailTableDataBindEventHandler(RadGrid1_DetailTableDataBind);

and call your binding functions in them accordingly  as  shown below:

C#
 protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            if (!e.IsFromDetailTable) 
            { 
                RadGrid RadGrid1 = (RadGrid)source; 
                RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers"); 
            } 
        } 
 protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e) 
        { 
            GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem; 
            switch (e.DetailTableView.Name) 
            { 
                case "Orders"
                    { 
                        string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString(); 
                        e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'"); 
                        break
                    } 
            } 
        } 


Thanks,
Princy

Tags
Grid
Asked by
Suresh K
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or