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

Better Approach than Hidden Columns?

2 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nathan
Top achievements
Rank 2
Nathan asked on 18 Jan 2012, 04:32 PM
Hello,

I'm currently using the RadGrid control with a delete button I've added to the "CommandItemTemplate" section. When the user selects certain rows and clicks delete, a client side event is called "delete_click". This function iterates over each selected row to build up an array of parameters which should include the primary key of the row. I'm currently using hidden columns to hold certain values needed in this function. Certain values are IsUserAdmin, userRole, and userId - but none of these should be displayed as a column in the grid.The primary key (data key) is userId.... "<MasterTableView DataKeyNames='userId'>

Question #1: Is there a better approach for associating these values with a row instead of hidden columns? You can see in the example below, to access the row's column "IsUserAdmin", I access the column by it's unique name. This is a column in the grid that is hidden. Can I associate this without using a hidden column as a better approach? 

Below is an example of how I access the cells for each selected row. 

         var grid = $find("<%=grdSummary.ClientID %>");
         var MasterTable = grid.get_masterTableView();
         var selectedRows = MasterTable.get_selectedItems();
         var parms = new Array();
         for (var i = 0; i < selectedRows.length; i++) {
         {  
          var row = selectedRows[i];
           parm = new ScheduleSummaryParms();
           // Retrieve Cell by column
           parm.IsAdmin = MasterTable.getCellByColumnUniqueName(row, "userIsAdmin").innerHTML; 
           parm.userRole = MasterTable.getCellByColumnUniqueName(row, "userIsAdmin").innerHTML;        
          parms[i] = parm;
         }

Question #2: Is there a way to access the row's primary key. I've been thinking about adding a hidden column for the primary key, but I thought there had to be a better way. I've seen examples using "getDataKeyValue", but this doesnt seem to work. 

Thanks!!

2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Jan 2012, 07:19 PM
Hello,

<MasterTableView DataKeyNames="ID,Name" ClientDataKeyNames="ID,Name">

Please try with ClientDataKeyNames.

Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 18 Jan 2012, 07:47 PM
Hello,

Please check below code snippet.

<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
  <script type="text/javascript">
      function GetKeyValues() {
 
          var grid = $find("<%=RadGrid1.ClientID %>");
          var masterTableView = grid.MasterTableView;
          var selectedRows = masterTableView.get_selectedItems();
          
          for (var i = 0; i < selectedRows.length; i++) {
              var row = selectedRows[i];
              alert(row.getDataKeyValue("ID"));
          }         
      }
  </script>
  </telerik:RadScriptBlock>
<asp:Button ID="Button1" runat="server"  OnClientClick="GetKeyValues(); return false;"/>
       <br />
       <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound"
           OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender" OnItemCommand="RadGrid1_ItemCommand"
           AllowMultiRowSelection="true">
           <MasterTableView DataKeyNames="ID" ClientDataKeyNames="ID">
               <Columns>
                   <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                   </telerik:GridBoundColumn>
                  

               </Columns>
           </MasterTableView>
           <ClientSettings>
               <Selecting AllowRowSelect="true" />
           </ClientSettings>
       </telerik:RadGrid>


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Nathan
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or