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

extracting selected rows from 1st grid to 2d grid

1 Answer 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Taeho Yoo
Top achievements
Rank 1
Taeho Yoo asked on 15 Jul 2008, 05:04 AM
Hi,

I have two grids. The first grid has a checkbox column and when the checkbox is checked, the corresponding row should be added to the second grid.
I have tried http://www.telerik.com/community/forums/thread/b311D-ekbdh.aspx

But in the example, button triggers the method where selected items get moved but in my case, I need to use the embedded checkbox column to move a selected row but the second grid doesn't get populated after the method where a selected row gets moved. but a strange thing is that when I click a column heading as if sorting then all of sudden the records show up in the second radgrid.

here is my code,

My first grid
<radG:RadGrid   
                            ID="RadGrid_QueryComments" 
                            EnableAJAX="true"   
                            runat="server"   
                            OnNeedDataSource="RadGrid_QueryComments_NeedDataSource"   
                            Title="Double Click on the comment template to Select it.">  
                            <PagerStyle Mode="NumericPages"></PagerStyle> 
                            <MasterTableView Width="100%" DataKeyNames="commentNo" GroupLoadMode="Client">  
                                                                               
                                <Columns> 
                                    <radG:GridTemplateColumn HeaderText="Select">  
                                        <ItemTemplate> 
                                            <asp:CheckBox runat="server" Text='<%# Eval("commentNo") %>' AutoPostBack="true" OnCheckedChanged="ckbSelectedCommentTemplate_CheckedChanged" ID="ckbSelectedCommentTemplate" /> 
                                        </ItemTemplate> 
                                    </radG:GridTemplateColumn> 

The second Grid
<radG:RadGrid ID="RadGrid_SelectedCommentNos"   
                                    EnableAJAX="true" 
                                    AllowSorting="true"   
                                    AllowPaging="true"   
                                    AllowMultiRowSelection="true"   
                                    runat="server" 
                                    OnNeedDataSource="RadGrid_SelectedCommentNos_NeedDataSource">  
                                    <ClientSettings ApplyStylesOnClient="true">  
                                        <Selecting AllowRowSelect="true" /> 
                                    </ClientSettings> 
                                </radG:RadGrid> 

NeedDataSource Methods
protected void RadGrid_QueryComments_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)  
    {  
        ((RadGrid)source).DataSource = this.commentTemplates;  
    }  
 
    protected void RadGrid_SelectedCommentNos_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)  
    {  
        RadGrid_SelectedCommentNos.DataSource = this.selectedCommentTemplates;  
    } 

commentTemplates and selectedCommentTemplates properties
    public DataTable commentTemplates  
    {  
        get  
        {  
            object res = this.ViewState["commentTemplates"];  
            if (res != null)  
            {  
                return (DataTable)res;  
            }  
 
            // I am just showing pseudocode here  
            // get default records  
            // save into ViewState["commentTemplates"]  
            // finally   
            return theResult;  
        }  
    }  
      
 
    public DataTable selectedCommentTemplates  
    {  
        get  
        {  
            object res = this.ViewState["selectedCommentTemplates"];  
            if (res != null)  
            {  
                return (DataTable)res;  
            }  
            else  
            {  
                // get default value  
                // insert into ViewState["selectedCommentTemplates"] 
                // return theResults  
            }  
        }  
        set   
        {  
            ViewState["selectedCommentTemplates"] = value;  
        }  
    }  
 
 

Lastly checkbox serverside event
    protected void ckbSelectedCommentTemplate_CheckedChanged(object sender, EventArgs e)  
    {  
        DataSet selectedCommentTemplateData = new DataSet();  
        DataTable existingData = selectedCommentTemplates;  
 
        if (((CheckBox)sender).Checked)  
        {  
            // fill selectedCommentTemplateData with this selected record  
            existingData.Merge(selectedCommentTemplateData.Tables[0]);  
            selectedCommentTemplates = existingData;  
        }  
        else  
        {  
            existingData.Rows.Remove(commentTemplates.Select("commentNo=" + ((CheckBox)sender).Text)[0]);  
            selectedCommentTemplates = existingData;  
        }  
 
        RadGrid_SelectedCommentNos.Rebind();  
    } 

Thank you in advance,

Toby

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 16 Jul 2008, 11:20 AM
Hello Taeho,

Please find attached a reworked version of the sample project that you examined in the posted forum article. Two RadGrids are defined on the page with a GridTemplateColumn and a CheckBox. Clicking the checkboxes moves the grid items back and forth between the two grids.

Please see how this relates to your case.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Taeho Yoo
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or