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

setting the number of rows to be inserted as 10

5 Answers 141 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Venkatesh
Top achievements
Rank 1
Venkatesh asked on 17 Dec 2012, 06:16 AM
Hi,
I have used the rad grid. In the ItemCommand i have checked for the rows as below. Also, if any of the existing rows in the available 10 are deleted, the user should be able to add one more item. I  have used this code:
protected void radgrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
             
                int i = int.Parse(hdnRowsCount.Value);
  
                if (e.CommandName == RadGrid.InitInsertCommandName)
                {                                  
                   if (i < 10)
                    {
                       i++;
                       hdnRowsCount.Value = Convert.ToString(i);
                    }
                    else
                    {
                        e.Canceled = true;
                        radgrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;
                    }
                }
                if (e.CommandName == RadGrid.DeleteCommandName)
                {
                    i--;
                }
              
        }

In the aspx, i have the "hdnRowsCount" set to 0. Once the item is deleted, the user should be able to add new rows again. Also, when the number of rows has reached, "Add new item" option should be hidden. Now It is not gettiong hidden.
How to fix this?
Thanks

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Dec 2012, 09:23 AM
Hi,

One suggestion is that you can take the total row count and hide command item as shown below.
C#:
int count = RadGrid2.MasterTableView.Items.Count;
   if (count == 10)
   {
       GridCommandItem item = (GridCommandItem)RadGrid2.MasterTableView.GetItems(GridItemType.CommandItem)[0];
       item.Display = false;
   }

Thanks,
Princy.
0
Venkatesh
Top achievements
Rank 1
answered on 18 Dec 2012, 06:06 AM
HI,
Should the code you provided be added inside "InitInsertCommandName"?
Thanks
0
Princy
Top achievements
Rank 2
answered on 18 Dec 2012, 06:22 AM
Hi,

You can take the total row count in ItemCommand event as shown below.
C#:
protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        int count = RadGrid2.MasterTableView.Items.Count;
        if (count == 10)
        {
            GridCommandItem item = (GridCommandItem)RadGrid2.MasterTableView.GetItems(GridItemType.CommandItem)[0];
            item.Display = false;
        }
    }
}

Thanks,
Princy.
0
Venkatesh
Top achievements
Rank 1
answered on 18 Dec 2012, 06:36 AM
Hi,
Thanks for the reply.

So if an existing item is deleted in the grid, would the "Add item" buttons be visible again? I need the buttons to be visible if the row is less than 10.

In the aspx, i have as below:
<MasterTableView DataKeyNames="ProductNumber" AutoGenerateColumns="false" EditMode="InPlace"
                   CommandItemDisplay="TopAndBottom" CommandItemSettings-AddNewRecordText="Add New Order">

Thanks
0
Accepted
Tsvetoslav
Telerik team
answered on 20 Dec 2012, 01:10 PM
Hello Venkatesh,

You can achieve your requirement as follows:
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
 
    if (RadGrid1.Items.Count > 0)
    {
        RadGrid1.MasterTableView.CommandItemDisplay =  GridCommandItemDisplay.None;
        RadGrid1.Rebind();
    }
    else
    {
        RadGrid1.MasterTableView.CommandItemDisplay =  GridCommandItemDisplay.Top;
        RadGrid1.Rebind();
    }
}




Regards, Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Venkatesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Venkatesh
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or