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

find checkbox in header template of Rad grid

3 Answers 880 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ankur
Top achievements
Rank 1
ankur asked on 05 Oct 2010, 07:13 AM
I am new to Rad controls there is a simple problem i am facing.

My scenario

I have to generate columns dynamically on the basis of the data coming from db.
in that in every column header there has to have a check box.


I have done that , but the thing is that there is a button outside rad grdi in which i have to find the check box
and depending upon its checked status i have to perform some business logic.

how to find the check box(located  in header) on the button click some code snippets will be helpful.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Oct 2010, 08:09 AM
Hello,


Try the following code to access the checkbox control in header of GridclientSelectColumn.

Code:
foreach (GridHeaderItem headerItem in RadGrid1.MasterTableView.GetItems(GridItemType.Header))
{
    CheckBox chk = (CheckBox)headerItem["ColumnUniqueName"].Controls[0]; // Get the header checkbox
}


-Shinu.
0
ankur
Top achievements
Rank 1
answered on 05 Oct 2010, 09:53 AM
hi shinu

thanks for your help

but it doent worked out for me i am posting my sample code so that you can get an
better understanding of the scenario

html markup
 <form id="form1" runat="server">
   <div>
        <asp:HiddenField ID="HdnCheck" runat="server" />
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ToolkitScriptManager>
       
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" />
       
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Transfer the text file to database" />
        <br />
        <br />
       <telerik:RadGrid ID="RdDynamicGrid" runat="server" AutoGenerateColumns="false">
       </telerik:RadGrid>
        <asp:Button ID="btnCheck" runat="server" Text="Fire button" OnClick="btnCheck_Click" />
    </div>
    </form>

In this case i am uploading an txt file from this file i am generating the rad grid

The class used to generate the grid
public class GridDynamic:ITemplate
{
    public GridDynamic()
    {
         
    }
    string _ColName;
    DataControlRowType _rowType;
    int _Count;
     
 
      
 
    public GridDynamic(string ColName, DataControlRowType RowType)
    {
        _ColName = ColName;
        _rowType = RowType;
    }
 
    public GridDynamic(DataControlRowType RowType, int ArticleCount)
    {
        _rowType = RowType;
        _Count = ArticleCount;
 
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (_rowType)
        {
            case DataControlRowType.Header:
                CheckBox lc = new CheckBox();
                lc.Text = "<b>"+_ColName +"</b>";
                lc.ID = _ColName;
                 
            

                container.Controls.Add(lc);
                break;
 
            case DataControlRowType.DataRow:              
                 Label lbl = new Label();
                 lbl.DataBinding += new EventHandler(this.lbl_DataBind);
                 container.Controls.Add(lbl);              
                break;
            default:
                break;
 
        }
 
    }
 
 
   

 
    private void lbl_DataBind(Object sender, EventArgs e)
    {
        Label lbl  = (Label)sender;
        GridDataItem row = (GridDataItem)lbl.NamingContainer;
        lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString();
    }
 
}


My page from where it is getting called this is the function that is generating the grid
public void GenerateDynamicData()
   {
       DataTable GentrateData = (DataTable)ViewState["GridData"];
       for (int i = 0; i < GentrateData.Columns.Count; i++)
       {
           if (GentrateData.Columns.Count > 0)
           {
 
               GridTemplateColumn tf = null;
               tf = new GridTemplateColumn();
               tf.HeaderTemplate = new GridDynamic(i.ToString(), DataControlRowType.Header);
               tf.ItemTemplate = new GridDynamic(i.ToString(), DataControlRowType.DataRow);
               RdDynamicGrid.Columns.Add(tf);
               RdDynamicGrid.DataSource = GentrateData;
               RdDynamicGrid.DataBind();
           }
       }
   }

This is the button click event where i want to access my check box
   protected void btnCheck_Click(object sender, EventArgs e)
   {
       GetCheckBoxState();
   }
 
   public void GetCheckBoxState()
   {
       CheckBox SelectAllCheckBox = new CheckBox();
       List<string> abc = new List<string>();
        //the loop is used to iterate through the loop to check box
       for (int i = 0; i < RdDynamicGrid.Columns.Count; i++)
       {
           foreach (GridHeaderItem headerItem in RdDynamicGrid.MasterTableView.GetItems(GridItemType.Header))
           {
               SelectAllCheckBox = (CheckBox)headerItem[i.ToString()].Controls[0]; // Get the header checkbox
           }
 
           //GridHeaderItem header = RdDynamicGrid.MasterTableView.GetItems(GridItemType.Header)(i) as GridHeaderItem;
         //  SelectAllCheckBox = RdDynamicGrid.MasterTableView.ge[i].FindControl(i.ToString()) as CheckBox;
           if (SelectAllCheckBox.Checked)
               abc.Add(SelectAllCheckBox.ID.ToString());
           else
           {
               if (abc.Contains(SelectAllCheckBox.ID.ToString()))
                   abc.Remove(SelectAllCheckBox.ID.ToString());
           }
           SelectAllCheckBox = null;
       }
       sendData(abc);
   }
i think now you can understand the scenario and suggest the way out of the situation.
0
Pavlina
Telerik team
answered on 08 Oct 2010, 09:30 AM
Hello Ankur,

After you are using GridTemplateColumn I suggest that you try adding the checkbox in its ItemTemplate. Here is a link which explains how to add GridTemplateColumn dynamically:
Programmatic creation(Creating template columns programmatically section)

To generate the columns for the grid programmatically (including the template column with the checkboxes), intercept their OnInit event and build the columns inside that handler.

Regards,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
ankur
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ankur
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or