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

Help with itemcommand

5 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Venkatesh
Top achievements
Rank 1
Venkatesh asked on 19 Dec 2012, 11:29 AM
Hi,
I have the below code in Itemcommand. I want to restrict users to enter only 2 rows. And also, if an existing row is deleted and number of rows is within 2, user should be able to add new item. This code is not working

int count = radgrid1.MasterTableView.Items.Count;
                  if (count > 2)
                  {
                      e.Canceled = true;
                      radgrid1r.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
                                      
                  }
                  else
                  {
                      radgrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
                  }

Also, i am  clicking on "ADD NEW ITEM" twice to make it disapper.
If the items inserted are 2 then automatically the "add new item" should not be displayed.
How to fix this?

5 Answers, 1 is accepted

Sort by
0
Florence
Top achievements
Rank 1
answered on 19 Dec 2012, 04:58 PM
Hi Venkatesh,

Have you tried calling Rebind() for the grid after setting these properties? If you do not want to rebind the control, you can try directly hiding the command item by setting Visible=false.
To access the command items directly, use RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem). This will return a collection of the items.
0
Shinu
Top achievements
Rank 2
answered on 20 Dec 2012, 04:13 AM
Hi,

You can take the items count in prerender and hide the commanditem.
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
        int count = RadGrid1.MasterTableView.Items.Count;
        if (count >=10)
        {
            GridCommandItem cmd = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            cmd.Visible = false;
        }
}

Thanks,
Shinu.
0
Venkatesh
Top achievements
Rank 1
answered on 20 Dec 2012, 11:55 AM
HI,
Thanks for the reply.
The prerender code is almost fine. I have the "Add new item" command in "Top and Bottom". With the code you provided, it hid only the "Top" one.
How to hide the bottom "Add new item" command also.

Thanks
0
Accepted
Florence
Top achievements
Rank 1
answered on 20 Dec 2012, 02:26 PM
Hi Venkatesh,

To hide the second command item, you also need to write:
(GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[1]

In my opinion, it would be a bit better if you first check the length of the collection returned by GetItems and if it is larger than 0, do a foreach loop where you hide each item. This way you do not hard-code the index and are save in scenarios where the command item isn't always there.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 Dec 2012, 03:49 AM
Hi,

Try looping through the items as shown below.
C#:
protected void RadGrid2_PreRender(object sender, EventArgs e)
{
    int count = RadGrid2.MasterTableView.Items.Count;
    if (count >= 10)
    {
        foreach (GridCommandItem item in RadGrid2.MasterTableView.GetItems(GridItemType.CommandItem))
        {
            item.Display = false;
        }
     }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Venkatesh
Top achievements
Rank 1
Answers by
Florence
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Venkatesh
Top achievements
Rank 1
Share this question
or