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

Cell Click Event for RadGrid

6 Answers 1754 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Medac
Top achievements
Rank 1
Medac asked on 13 Jul 2009, 04:42 AM
HI,

I want to implement Cell click event in radgrid. i.e when user selects cell in a radgrid then the particular cell should be highlighted and should be able to retrieve the value of cell in javascript.

Can anybody help in this?

Regards,
Medac

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Jul 2009, 08:11 AM
Hello Medac,

You can try out the following code to implement cell click in grid:
c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
       if (e.Item is GridDataItem) 
        { 
            foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
                dataItem[col.UniqueName].Attributes.Add("onclick""CellClick('" + dataItem.ItemIndex + "','"+ col.UniqueName +"');"); 
            } 
        } 
    } 

js:
 function CellClick(index, uniqueName) 
   {          
      var grid = $find("<%=RadGrid1.ClientID %>"); 
      var MasterTable = grid.get_masterTableView(); 
      var row = MasterTable.get_dataItems()[index]; 
      var cell = MasterTable.getCellByColumnUniqueName(row, uniqueName); 
      var celltext = cell.innerHTML
      cell.style.backgroundColor = "Red"
      alert(celltext);     
       
   }     

Hope this helps..
-Princy.
0
Medac
Top achievements
Rank 1
answered on 13 Jul 2009, 09:29 AM
Hey princy,

It worked, but it is retaining the previous section. How to make only one cell selection at a time. I mean to say the coloring part code is painting the cells with red background. It is not clearing the previous background color.

Can you help me in this?

Regards,
Medac
0
Princy
Top achievements
Rank 2
answered on 14 Jul 2009, 07:10 AM
Hi Medac,

To allow only single cell selection you would have to modify the above code as shown below:
aspx:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item;                 
                dataItem[col.UniqueName].Attributes.Add("onclick""CellClick('" + dataItem.ItemIndex + "','"+ col.OrderIndex +"');"); 
            } 
 
        } 
    } 

c#:
 function CellClick(index, colIndex) 
   {          
      var grid = $find("<%=RadGrid1.ClientID %>"); 
      var MasterTable = grid.get_masterTableView(); 
      var rows = MasterTable.get_dataItems();       
      for (var i = 0; i < rows.length; i++) 
      {          
          var row = rows[i]; 
          for (var j = 0; j < row._element.cells.length; j++) 
          {             
            var cell = row._element.cells[j]; 
            if(row._itemIndexHierarchical == index && cell.cellIndex+2 == colIndex)   
            {              
                 var celltext = cell.innerHTML
                  alert(celltext); 
                 cell.style.backgroundColor = "Red";              
            }          
           else 
            {                           
                 cell.style.backgroundColor = "";                    
            }            
          } 
      }       
   }     

Regards
Princy.
0
Medac
Top achievements
Rank 1
answered on 15 Jul 2009, 09:00 AM
Hey Princy,

Is this a only way to acieve cell click event in Radgrid?

Regards,
Medac
0
Princy
Top achievements
Rank 2
answered on 15 Jul 2009, 09:35 AM
Hello Medac,

Unfortunately, it seems RadGrid does not support the selection and highlighting of individual data cells out-of-the-box. An alternative to achieve cell selection is illustrated in the following help document, you can go through it:
Selecting cells

Thanks
Princy.
0
Bruno
Top achievements
Rank 1
answered on 08 Aug 2011, 02:33 PM
Hi,

If RadGrid does not support the selection and highlighting of individual data cells out-of-the-box, what's the use for it?

Tks

Tags
Grid
Asked by
Medac
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Medac
Top achievements
Rank 1
Bruno
Top achievements
Rank 1
Share this question
or