How to access the Column Index of rad grid at Server Side

2 posts, 1 answers
  1. Sairam
    Sairam avatar
    56 posts
    Member since:
    May 2013

    Posted 21 Sep 2013 Link to this post

    Hello folks,
     I Have problem like these ..In rad-grid  ..i  Have 20 Column above ....I am Put The allow scroll bar is true ..rad grid  increased ..too wide
      I want to give option to user when ever The User click on "Column  " Link Button I want to hide That Column ..showing remaining column as
    it is..But here I got some problem ..I  am not aware of how to use column index based on "Click on Column ". These code I am used to get Column
    Index but  unfortunately ..I  am not getting the Value ..
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
           if (e.CommandName == "hideCol")
               // Add the column index to hide to the hiddenColumnIndexes list

    e.Item.ItemIndex ,I  am Getting "-1" value Can help here to Getting The correct Column Index ..Form user  click on  Column ....
    The Following Picture Shows .."-" Symbol in Column Text Prefix When user Click On "-" Button ..We Need to Hide that Column ..but here  "I am not Know How get Column Index" raised By Event Column .

    protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
          // For the header row add a link button to each header
          // cell which can execute a row command
          if (e.Item.ItemType==GridItemType.Header)
              // Loop through each cell of the header row
              for (int columnIndex = 0; columnIndex < e.Item.Cells.Count; columnIndex++)
                  LinkButton hideLink = new LinkButton();
                  hideLink.CommandName = "hideCol";
                  hideLink.CommandArgument = columnIndex.ToString();
                  hideLink.Text = "-";
                  hideLink.CssClass = "gvHideColLink";
                  hideLink.Attributes.Add("title", "Hide Column");
                  // Add the "Hide Column" LinkButton to the header cell
                  e.Item.Cells[columnIndex].Controls.AddAt(0, hideLink);
                  // If there is column header text then
                  // add it back to the header cell as a label
                  if (e.Item.Cells[columnIndex].Text.Length > 0)
                      Label columnTextLabel = new Label();
                      columnTextLabel.Text = e.Item.Cells[columnIndex].Text;
          // Hide the column indexes which have been stored in hiddenColumnIndexes
          foreach (int columnIndex in hiddenColumnIndexes)
              if (columnIndex < e.Item.Cells.Count)
                  e.Item.Cells[columnIndex].Visible = false;

    The Below to Code to Bind the Data And Hide That Column Code .

    Reply me Any One.


  2. Answer
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Sep 2013 Link to this post

    Hi Sairam,

    Keep in mind that it Column index starts from 2,special columns such as Expand/Collapse, RowIndicatorColumn etc are also counted.
    So you may modify the loop as :
     for (int columnIndex = 2; columnIndex < e.Item.Cells.Count; columnIndex++)

    In order to access the column index,please try the following code snippet.Please note that using  "hiddenColumnIndexes.Add(e.Item.ItemIndex)" , e.Item.ItemIndex gives you the row index and not the column.

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
     if (e.CommandName == "hideCol")
      int columnIndex = Convert.ToInt16(e.CommandArgument); // You can obtain "e.CommandArgument" from  hideLink.CommandArgument = columnIndex.ToString();           

Back to Top