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

Getting started with MasterTable/DetailsTable .. I don't understand

17 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 05 Oct 2010, 03:23 PM
What I want to do is pretty simple.. Id like to use Linq2Ent (worse case I can use SP) to create a simple grid..

When you load the page you have a list of catagories... when you click a category it expands and shows the quarter (2009q1, 2009q2, 2009q3,2009q4,2010q1 etc..) on each row with the amount spent for that quarter... I want to be able to edit those rows in the Details table.. so its simple right?

So I imagine I will need to queries... One query for the MasterTable (list of categories) and then when a row from the mastertable is clicked I will need another Linq2Ent query to take the ID for that category and get the list of quarters and show them underneath the categories..

Do I have the concept right? I know you don't have many examples using linq2Ent so I suppose the exact concept applies with store procedures... Do you have any very basic samples? The closest thing I could find is this.. and its more complex than I need.

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx

17 Answers, 1 is accepted

Sort by
0
matt
Top achievements
Rank 1
answered on 05 Oct 2010, 03:24 PM
Also I would like an example which does not use declaritive data source since I always seem to run into restrictions and end up having to do things in code behind anyways
0
Matthew
Top achievements
Rank 1
answered on 05 Oct 2010, 07:07 PM
You can leave the datasource section blank, in which case you would set it to an object in the "NeedDataSource" event. 

You should, however, specify the datakey name of the column.
The master will execute a query to databind, then the details will execute queries for each row to databind. 
When this happens, the needdatasource event is raised. 
This is why it's often easier to use declarative datasources.
0
matt
Top achievements
Rank 1
answered on 05 Oct 2010, 07:49 PM
Ok. I got that now.. it wasnt as hard as I thought once I figured out all the stuff that was not needed.. So now I simply load data on the NeedsDataSource and load data for

RadGrid1_DetailTableDataBind



Possibly I could do this all with datasources but they scare me.. I am using someones Stored Procedures so I am more comfortable programmatically doing things..

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
            GridLines="None" OnDetailTableDataBind="RadGrid1_DetailTableDataBind" AutoGenerateColumns="False">
            <MasterTableView Name="Categories" DataKeyNames="FTaskID">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="ftaskid" Name="GridDetails" Width="100%">
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="ftaskid" DataField="ftaskid">
                            </telerik:GridBoundColumn>
                        <%--    <telerik:GridBoundColumn HeaderText="ftaskid" DataField="ftaskid">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="BLI" DataField="BLI">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="fundtype" DataField="fundtype">
                            </telerik:GridBoundColumn>--%>
                            <telerik:GridBoundColumn HeaderText="Qtr" DataField="Qtr">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Year" DataField="Year">
                            </telerik:GridBoundColumn>
                           <%-- <telerik:GridBoundColumn HeaderText="Expenditure Amount" DataField="Expenditures">
                            </telerik:GridBoundColumn>--%>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="FTaskID" DataField="FTaskID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Level2" DataField="Level2">
                    </telerik:GridBoundColumn>                
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
0
matt
Top achievements
Rank 1
answered on 05 Oct 2010, 08:07 PM
How do I specify I only want the ability to EDIT/INSERT in the details view and not the master table? I see this sample

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx

but it allows editing at all levels...

It seems editing is specified at the RadGrid level... my guess is I have to somehow disable editing for the master table in code bhind when the grid loads somehow ?

0
Sebastian
Telerik team
answered on 06 Oct 2010, 10:48 AM
Hello matt,

If you do not add edit/delete columns and hide the command item in master table definition, the end users will not be able to perform data editing operations on the main grid level. I hope this information is helpful.

Regards,
Sebastian
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
matt
Top achievements
Rank 1
answered on 06 Oct 2010, 05:06 PM
hmmm so I can hide and show the edit/insert etc... in the ASPX? (I need the inert command panel for ONLY the details grid as well)? Or in the grid load event programmtically ?

0
Sebastian
Telerik team
answered on 06 Oct 2010, 05:08 PM
Hello matt,

It is up to you to decide - you either need to remove the GridEditCommandColumn and GridButtonColumn with CommandName = Delete and set CommandItemDisplay = None for your master table declaratively (in the aspx) or dynamically from the code-behind.

Regards,
Sebastian
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
matt
Top achievements
Rank 1
answered on 06 Oct 2010, 05:11 PM
Gotcha..

And for the Details grid I want the Qtr andYear column to be read only..

but when inserting I want the user to be able to edit these columns...

Can I do that in .ASPX or would I need to control the property of making these read only in code behind somewhere ?
0
matt
Top achievements
Rank 1
answered on 06 Oct 2010, 08:43 PM
I want to make some of the edit template textboxes read only depending on a value in that row of data...

I am tihnking I must use code behind..

So do I do this in the GridTableView (since that is were my details are) like this ?

 

 

<telerik:GridTableView DataKeyNames="ftaskid" Name="GridDetails" Width="100%" CommandItemDisplay="Top" OnDataBinding="GridDetails_OnDataBinding">

 

 




any code that does something like this and modifies the UI of certain editable textboxes ? 
0
Princy
Top achievements
Rank 2
answered on 07 Oct 2010, 07:54 AM
Hello Matt,

If you want to change the ReadOnly property of TextBox in some row based on the cell value, try the following code snippet in ItemDataBound event.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
      if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "GridDetails")
      {
          GridEditFormItem editItem = (GridEditFormItem)e.Item;
          if (//your condition)
          {
              TextBox txt = (TextBox)editItem.FindControl("TextBox1");
              txt.ReadOnly = true;
          }
      }

Thanks,
Princy.
0
matt
Top achievements
Rank 1
answered on 07 Oct 2010, 06:29 PM
Ok got it... Now how do I change the 'INSERT' template (details table)? I want to add 2 columns that were hidden in the EDIT template...

Here is all I can figure.. How do I access those INSERT editboxes? So I can tell it.. ADD TWO MORE!

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
      {
          if (e.CommandName == RadGrid.InitInsertCommandName) 
          {
            //  GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
             // GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("H6_Exp");
            //  GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("H6_Exp");
            //  editColumn.Visible = false;
          }
      }

here is the aspx

<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
           AutoGenerateColumns="False" AutoGenerateEditColumn="False" OnUpdateCommand="RadGrid1_UpdateCommand"
           Width="800px" OnItemDataBound="RadGrid1_ItemDataBound" 
           OnNeedDataSource="RadGrid1_NeedDataSource" 
           oninsertcommand="RadGrid1_InsertCommand" onitemcommand="RadGrid1_ItemCommand">
           <MasterTableView Name="Categories" DataKeyNames="FTaskID">
               <DetailTables>
                   <telerik:GridTableView DataKeyNames="ftaskid" Name="GridDetails" Width="100%" CommandItemDisplay="Top">
                       <Columns>
                           <telerik:GridEditCommandColumn UniqueName="geccDetails">
                           </telerik:GridEditCommandColumn>
                           <telerik:GridBoundColumn HeaderText="Qtr" DataField="Qtr" ReadOnly="true">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="Year" DataField="Year" ReadOnly="true">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="H6_Exp" DataField="H6_Exp">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="H6_Use" DataField="H6_Use" UniqueName="H6_Use" ReadOnly="true">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="PH_Exp" DataField="PH_Exp">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="PH_Use" DataField="PH_Use" UniqueName="PH_Use"
                               ReadOnly="true">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="Oth_Exp" DataField="Oth_Exp">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="Oth_Use" DataField="Oth_Use" ReadOnly="true">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="FF_Exp" DataField="FF_Exp">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn HeaderText="FF_Use" DataField="FF_Use" ReadOnly="true">
                           </telerik:GridBoundColumn>
                       </Columns>
                   </telerik:GridTableView>
               </DetailTables>
               <Columns>
                   <telerik:GridBoundColumn HeaderText="FTaskID" DataField="FTaskID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn HeaderText="Level2" DataField="Level2">
                   </telerik:GridBoundColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
0
matt
Top achievements
Rank 1
answered on 07 Oct 2010, 06:52 PM
I did a test to see if I can atleast give a value to one of the fields during the INSERT...  nothing is showing in the box.. am I reference the right grid? I want to reference the detail grid.. not the main grid..

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
     {
         if (e.CommandName == RadGrid.InitInsertCommandName) 
         {
             e.Item.OwnerTableView.InsertItem();
             GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
             GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
             TextBox box = editFormItem["H6_Exp"].Controls[0] as TextBox;
             box.Text = "xxx";
         //    TextBox box = editFormItem.FindControl("H6_Exp") as TextBox;
             //GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("H6_Exp");
           //  GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("H6_Exp");
           //  editColumn.Visible = false;
         }
     }
0
Princy
Top achievements
Rank 2
answered on 08 Oct 2010, 10:14 AM
Hello Matt,

Try the following code snippet in ItemDataBound event to set some value to the TextBox in insert form of DetailTable.

C#:
protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted && e.Item.OwnerTableView.Name == "GridDetails") //access insert form of DetailTable
       {
           GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
           TextBox box = insertItem["H6_Exp"].Controls[0] as TextBox;
           box.Text = "xxx";
       }
   }

Thanks,
Princy.
0
matt
Top achievements
Rank 1
answered on 08 Oct 2010, 09:14 PM
Works!

So now I need to make 2 columns that are read only able to inserted...

How would I do this?

Since it normally would not show up for the insert template... I need to someone unhide it.


0
Princy
Top achievements
Rank 2
answered on 11 Oct 2010, 07:46 AM
Hello Matt,

Try the following code snippet in ItemDataBound event to set the ReadOnly property of GridBoundColumn based on insert/edit mode.

ASPX:
<telerik:GridTableView Name="GridDetails">
  <columns>
    <telerik:GridBoundColumn HeaderText="Qtr" DataField="Qtr" UniqueName="Qtr">
    </telerik:GridBoundColumn>
  </columns>
</telerik:GridTableView>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
    if (e.Item.OwnerTableView.Name == "GridDetails")
      {     
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
         {
          GridEditFormItem editItem = (GridEditFormItem)e.Item;
          TextBox txtqtr = (TextBox)editItem["Qtr"].Controls[0];
          txtqtr.ReadOnly = true;
         }
       if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
        {
          GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
          TextBox txtqtr = (TextBox)insertItem["Qtr"].Controls[0];
          txtqtr.ReadOnly = false;
        }
   }

Thanks,
Princy.
0
matt
Top achievements
Rank 1
answered on 11 Oct 2010, 04:33 PM
Ok got it.. Im 99% there..

Last thing that is not working is getting th eprimary key for the item being inserted... This line works fine when I am updating values

 

 

int iCategoryID = Int32.Parse(dataItem.GetDataKeyValue("FTaskID").ToString());

 




But when I run that in the

RadGrid1_InsertCommand


I get

+  editedItem.GetDataKeyValue("ftaskid") 'editedItem.GetDataKeyValue("ftaskid")' threw an exception of type 'System.ArgumentOutOfRangeException' object {System.ArgumentOutOfRangeException}


I have also tried using a GridEditFormInsertItem tihnking that might make a difference.

 

 

// GridEditFormInsertItem gefit = (GridEditFormInsertItem)e.Item;

 

 

 

// int iFTaskIDx = Int32.Parse(gefit.GetDataKeyValue("ftaskid").ToString());

 


but the same problem..

How do I get that primary key  ?

 

 

<DetailTables>

 

 

 

<telerik:GridTableView DataKeyNames="ftaskid" Name="GridDetails" Width="100%" CommandItemDisplay="Top">

 

 

 

<Columns>

 







0
Princy
Top achievements
Rank 2
answered on 12 Oct 2010, 10:56 AM
Hello Matt,

I am not quite sure about your requirement.  If you wants to access the DataKeyValue of parent item , then try the following code snippet in InsertCommand event.

C#:
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
  {
      if (e.Item.OwnerTableView.Name == "GridDetails")
      {
          GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
          GridDataItem parentItem = (GridDataItem)insertItem.OwnerTableView.ParentItem; // accessing parentItem of  insertItem
          int parentID =(Convert.ToInt32(parentItem.GetDataKeyValue("FTaskID")));
       }
  }

Thanks,
Princy.
Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
matt
Top achievements
Rank 1
Matthew
Top achievements
Rank 1
Sebastian
Telerik team
Princy
Top achievements
Rank 2
Share this question
or