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

[Solved] Checkbox in grid

4 Answers 271 Views
Grid
This is a migrated thread and some comments may be shown as answers.
paul de Blaauw
Top achievements
Rank 1
paul de Blaauw asked on 08 Apr 2009, 01:11 PM
Hello,

I have a grid, which is dynamically build, in the first index, should there be a checkbox, for a multiselect.
When i go to the next page in the grid, i have lost my 'checks'.
Now i want to write each one in the page i am working with in a session, however how can i read the value of the row?

under here is my code:

protected void gridOverzichtLijst_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
    {  
        try 
        {  
            IEntityCollection coll = GetSearchColl();  
            gridOverzichtLijst.MasterTableView.DataKeyNames = new string[] { _idFieldName };  
 
            //first clean the colums.  
            gridOverzichtLijst.Columns.Clear();  
 
            GridTemplateColumn kolom = new GridTemplateColumn();  
            kolom.UniqueName = "CheckBoxTemplateColumn";  
            kolom.HeaderStyle.Width = 30;  
            kolom.ItemTemplate = new MyTemplate("cbRapport");  
            gridOverzichtLijst.Columns.Add(kolom);  
              
            GridSortExpression expression = new GridSortExpression();  
            expression.FieldName = SortField;  
            expression.SortOrder = GridSortOrder.Ascending;  
            if (!SortAscending)  
                expression.SortOrder = GridSortOrder.Descending;  
 
            gridOverzichtLijst.DataSource = coll;  
            gridOverzichtLijst.MasterTableView.SortExpressions.AddSortExpression(expression);  
        }  
        catch (Exception ex)  
        {  
            ErrorHandler.GetErrorHandler().WriteCriticalError("wucMasterLijst""gridOverzichtLijst_NeedDataSource", ex.Message);  
        }  
    }  
 
public class MyTemplate : ITemplate  
{  
    protected CheckBox CheckBox1;  
    private string colname;  
    private System.Web.UI.Control cont;  
    public MyTemplate(string cName)  
    {  
        colname = cName;  
    }  
 
    public void InstantiateIn(System.Web.UI.Control container)  
    {  
        CheckBox1 = new CheckBox();  
        CheckBox1.ID = "CheckBox1";  
        CheckBox1.Enabled = true;  
        CheckBox1.AutoPostBack = true;  
        CheckBox1.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);  
        container.Controls.Add(CheckBox1);  
    }  
 
    void CheckBox1_CheckedChanged(object sender, EventArgs e)  
    {  
        //here should be, at least i think, the code to put the value in a list to remember.  
    }  
 
}  

When you need more, let me know.

Thanks for help in advance.

Peter

4 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 08 Apr 2009, 02:44 PM
Hello paul de Blaauw,

When creating RadGrid programmatically, you need to use either the Page_Init or the Page_Load event - therefore you need to move your column creation and grid's properties setting code from the NeedDataSource event.

With regards to your main question - you can access the GridDataItem object where the checkbox resides through the latter's NamingContainer property:

    
    void CheckBox1_CheckedChanged(object sender, EventArgs e)     
    {     
        GridDataItem dataItem = ((CheckBox)sender).NamingContainer as GridDataItem    
    }    
 

and extract the necessary values and information from there.


I hope this helps.

Sincerely yours,
Tsvetoslav
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
paul de Blaauw
Top achievements
Rank 1
answered on 14 Apr 2009, 03:40 PM
Hello,

At the moment i have worked it out on another way, with the Radgridcheckbox column
However i want to check it when i change page when the value of the row is in a session arraylist.
How can i check if they are the same, and how do i check the column then?

Thanks in advance.

<telerik:RadGrid ID="gridOverzichtLijst" runat="server" AllowPaging="True" width="100%" 
                AllowSorting="True" onneeddatasource="gridOverzichtLijst_NeedDataSource"   
                oncolumncreated="gridOverzichtLijst_ColumnCreated"   
                onsortcommand="gridOverzichtLijst_SortCommand" Skin="Office2007"   
                onitemcreated="gridOverzichtLijst_ItemCreated" 
                onselectedindexchanged="gridOverzichtLijst_SelectedIndexChanged"   
                onitemdatabound="gridOverzichtLijst_ItemDataBound"   
                ItemStyle-CssClass="RadGrid_Office2007" AllowMultiRowSelection="True">  
                <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">  
<Selecting AllowRowSelect="True"></Selecting> 
                </ClientSettings> 
<ItemStyle CssClass="RadGrid_Office2007"></ItemStyle> 
 
                <MasterTableView CellSpacing="-1">  
                    <Columns> 
                        <telerik:GridClientSelectColumn ></telerik:GridClientSelectColumn> 
                    </Columns> 
                <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
 
                <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <PagerTemplate> 
                    <table style="outline-style:none; height:30px;" cellpadding="0" cellspacing="0" width="100%">  
                        <tr> 
                            <td style="width:25px;">  
                                <asp:ImageButton ID="cmdFirst" runat="server" OnClientClick="changePageMaster('first'); return false;" ImageUrl="~/Pics/first.png" /> 
                            </td> 
                            <td style="width:25px">  
                                <asp:ImageButton ID="cmdPrevious" runat="server" OnClientClick="changePageMaster('prev'); return false;" ImageUrl="~/Pics/previous.png" /> 
                            </td> 
                            <td style="width:25px">  
                                <asp:ImageButton ID="cmdNext" runat="server"  OnClientClick="changePageMaster('next'); return false;" ImageUrl="~/Pics/next.png" /> 
                            </td> 
                            <td style="width:25px">  
                                <asp:ImageButton ID="cmdLast" runat="server" OnClientClick="changePageMaster('last'); return false;" ImageUrl="~/Pics/last.png" /> 
                            </td> 
                            <td> 
                                <asp:Label ID="lblPagina" runat="server" Text="Pagina"></asp:Label> 
                                <asp:Label ID="lblCurrentPage" runat="server"></asp:Label> 
                                <asp:Label ID="lblVan" runat="server" Text="van"></asp:Label> 
                                <asp:Label ID="lblPageCount" runat="server"></asp:Label> 
                            </td> 
                        </tr> 
                    </table> 
                </PagerTemplate> 
                </MasterTableView> 
            </telerik:RadGrid> 


protected void gridOverzichtLijst_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)   
        {  
            foreach (TableCell cell in e.Item.Cells)  
            {  
                DateTime dtCell = new DateTime();  
                if (DateTime.TryParse(cell.Text, out dtCell))  
                {  
                    if (dtCell.Year < 1901)  
                        cell.Text = "-";  
                }  
 
                if (Session["LIST_SELECTIE"] != null)  
                {  
                    ArrayList selectie = (ArrayList)Session["LIST_SELECTIE"];  
 
                    //here i think it needs to check the row of the grid for the checkbox if it is in the list, it should be checked, anotherelse it should be not checked  
                }  
            }  
 
            //mouseover uit atm.  
            //e.Item.Attributes.Add("onmouseover", "this.style.cursor='hand'; this.style.background='#ffcb60'");  
            //e.Item.Attributes.Add("onmouseout", "this.style.background='white'");  
 
            //border:1px solid #688caf;  
            //background:#fff;  
            //color:#333;  
        }  
    } 
0
Shinu
Top achievements
Rank 2
answered on 15 Apr 2009, 08:11 AM
Hello Paul,

I guess you are trying to persist the selected rows on paging. If so, checkout the documentation for Persisting the selected rows server-side on sorting/paging/filtering/grouping. I hope this will help you.

You can also refer the following link for performing this on client side.
Persisting the selected rows client-side on sorting/paging/filtering/grouping

Thanks,
Shinu.
0
paul de Blaauw
Top achievements
Rank 1
answered on 15 Apr 2009, 10:35 AM
Not only on those events you said, however also when gone to another page, and came back, they also need to be checked.
However i will look into your links first.    

Thanks.
Tags
Grid
Asked by
paul de Blaauw
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
paul de Blaauw
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or