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

Hide Radgrid rows on checkbox event

5 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
TelDev
Top achievements
Rank 1
TelDev asked on 25 Aug 2010, 09:58 AM
Hi,

I have a sample grid

col1     col2     col3
1     name1     company1
2     name2     company2
3     name3     company3
4     name4     company4
5     name5     company5
6     name6     company6

I have checkboxes
 Hide even rows
 Hide odd rows
 Hide prime number rows

For hiding the rows, I looped through the grid rows(radgrid.MasterTableView.Items) and if col1 is same as selected checkbox then used item.display to false, but it does not work. Is it right way to do it?

Thanks
Teldev

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Aug 2010, 12:17 PM
Hello Teldev,


I hope the following sample code will help you to achieve the required.

C#:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if ((item.ItemIndex % 2) == 0)
        {
            item.Visible = !CheckBox1.Checked;
        }
    }
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if ((item.ItemIndex % 2) == 1)
        {
            item.Visible = !CheckBox2.Checked;
        }
    }
}

Where the aspx is:
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Text="Hide Even"
    OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" Text="Hide Odd" OnCheckedChanged="CheckBox2_CheckedChanged" />
<telerik:RadGrid ID="RadGrid1" AutoGenerateDeleteColumn="true"  . . .



-Shinu.
0
TelDev
Top achievements
Rank 1
answered on 25 Aug 2010, 12:25 PM
Thanks Shinu for the reply.
I followed the same way, but i think my problem is with re-binding the grid, it does not work and in turn the rows are still visible.
0
Mira
Telerik team
answered on 30 Aug 2010, 08:47 AM
Hello TelDev,

I recommend that you use the following code on grid pre-render to implement the desired functionality:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if ((item.ItemIndex % 2) == 0)
        {
            item.Visible = !CheckBox1.Checked;
        }
    }
 
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if ((item.ItemIndex % 2) == 1)
        {
            item.Visible = !CheckBox2.Checked;
        }
    }
}

I hope it helps.

Regards,
Mira
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
0
TelDev
Top achievements
Rank 1
answered on 30 Aug 2010, 08:58 AM
Hi Mira,

Thanks for reply, What in case if i have linkbuttons instead of check boxes.

Thanks
Teldev
0
Shinu
Top achievements
Rank 2
answered on 30 Aug 2010, 02:31 PM
Hello Teldev,

You can try the following code snippet to achieve this. Use one HiddenField to identify which LinkButton is clicked. Then in PreRender event based on this hiddenField value hide corresponding row.

C#:
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        HiddenField1.Value = "LinkButton1";
    }
protected void LinkButton2_Click(object sender, EventArgs e)
    {
       HiddenField1.Value = "LinkButton2";
    }
 
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (HiddenField1.Value == "LinkButton1")
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            if ((item.ItemIndex % 2) == 0)
               item.Visible = false;
           else
                item.Visible = true;
        }
    }
    if (HiddenField1.Value == "LinkButton2")
    {
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            if ((item.ItemIndex % 2) == 1)
               item.Visible =  false;
            else
               item.Visible = true;
        }
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
TelDev
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
TelDev
Top achievements
Rank 1
Mira
Telerik team
Share this question
or