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

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

1 Answer 230 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sairam
Top achievements
Rank 1
Sairam asked on 21 Sep 2013, 12:44 PM

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
           hiddenColumnIndexes.Add(e.Item.ItemIndex);
           SetupShowHideColumns();
       }
 
       
   }

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 ..so 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;
                  e.Item.Cells[columnIndex].Controls.Add(columnTextLabel);
              }
          }
      }
 
      // 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 .



Thanks
Reply me Any One.

 


1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 23 Sep 2013, 09:26 AM
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.

C#:
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();           
 }
}

Thanks,
Princy        
Tags
General Discussions
Asked by
Sairam
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or