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

[Solved] How to get the number of rows, column and selected items

1 Answer 844 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Erwin
Top achievements
Rank 1
Erwin asked on 05 Jun 2009, 07:40 AM
I have a radgrid and i want to know the following

1. number of rows
2. number of columns
3. row of the item selected in grid
4. column of the item selected in grid

I have tried grid.column.count but it does not have that property?

Additional question:

I have a checkbox in the grid and i want the user to select only one checkbox...

i need to deselect the previous selected checkbox if the user will check another checkbox.

Is it possible to just find the column of the checkbox then uncheck all in the checkbox in the event "onmousedown"

since if its not possible im thinking to make a "for loop" and locate each cell for the checkbox.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Jun 2009, 11:16 AM
Hello Erwin,

You can check out the following code to access rows ,columns or cells in your grid:
c#:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        int colcount = RadGrid1.MasterTableView.Columns.Count; // to get the column count 
        int rowcount = RadGrid1.MasterTableView.Items.Count; //to get the row count 
        foreach(GridDataItem dataItem in RadGrid1.SelectedItems) 
         { 
            GridColumn col = (GridColumn)RadGrid1.MasterTableView.GetColumn("Name"); //to access the selected item's column 
            string strtxt = dataItem["Name"].Text; // to access the cell value 
         } 
    } 
Also refer to the following document which explains on how to access cells and rows in a grid:
Accessing cells and rows

If you want to select rows in the grid using checkboxes on each row, you can use the GridClientSelectColumn and set the ClientSettings>Selecting>AllowRowSelect to true which enables client-side selection.

If you are using a checkbox with a TemplateColumn, then you can try out the following code:
aspx:
 <telerik:GridTemplateColumn UniqueName="TemplateColumn"
        <ItemTemplate> 
            <asp:CheckBox ID="CheckBox" runat="server" /> 
        </ItemTemplate> 
 </telerik:GridTemplateColumn> 

c#:
 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
       foreach (GridDataItem dataItem in RadGrid1.Items) 
        { 
            CheckBox chkbx = (CheckBox)dataItem.FindControl("CheckBox"); 
            chkbx.Attributes.Add("onclick""SelectMeOnly('"+ chkbx.ClientID + "','"+dataItem.ItemIndex+"')"); 
        } 
    } 

js:
function SelectMeOnly(checkbox,rowindex)  
   {  
     var i, obj, pageElements; 
    if(navigator.userAgent.indexOf("MSIE")!= -1)   
     {   
        //IE browser   
        pageElements = document.all;   
     }   
     else if(navigator.userAgent.indexOf("Mozilla") != -1 || navigator.userAgent.indexOf("Opera")!= -1)   
     {   
        //FireFox/Opera browser   
        pageElements = document.documentElement.getElementsByTagName("input");   
     }   
     for (i=0; i < pageElements.length; i++)   
     {   
       obj = pageElements[i];  
       if (obj.tagName == "INPUT")  
       {  
         if (checkbox == obj.id)  
          obj.checked = true;  
            
         else  
          obj.checked = false
       }  
    }  
  }     

Thanks
Princy.
Tags
Grid
Asked by
Erwin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or