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

ItemDataBound - Edit Command, rowcount is always zero

3 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sravanthi
Top achievements
Rank 1
Sravanthi asked on 07 Aug 2012, 04:26 PM
I have a radgrid with template column as follows :

 <telerik:GridTemplateColumn UniqueName="Priority" DataField="Priority" HeaderText="Priority"
                        ItemStyle-Width="125px" HeaderStyle-Width="125px">
                         <ItemTemplate>
                            <asp:Literal ID="ltlPriority" Text='<%# string.IsNullOrEmpty(Eval("Priority").ToString()) ? 0 : (int)Eval("Priority") %>'
                                runat="server"></asp:Literal>
                        </ItemTemplate>
                        <EditItemTemplate>  
                            <telerik:RadComboBox ID="cmbPriority" runat="server">                          
                            </telerik:RadComboBox>     
                        </EditItemTemplate>
 </telerik:GridTemplateColumn>

I have to populate the cmbPriority Combobox with numbers equal to total row count.
(if row count is 5, the combobox should show 1,2,3,4,5)

OnItemDatabound this is what I have ::

 if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem item = (GridEditableItem)e.Item;
                object tmpobj = item.FindControl("cmbPriority");
                if (tmpobj != null)
                {
                    RadComboBox ddc = tmpobj as RadComboBox;
                    int nbrofrows = rgProducts.MasterTableView.Items.Count;
               
                    List<ProductPriority> priorities = new List<ProductPriority>();
                    //List<int> priorities = new List<int>();
                    for (int i = 1; i <= nbrofrows; i++)
                    {
                        ProductPriority pp = new ProductPriority();
                        pp.Priority = i;
                        priorities.Add(pp);
                    }
                    ddc.DataSource = priorities;
                    ddc.DataTextField = "Priority";
                    ddc.DataValueField = "Priority";
                    ddc.DataBind();
                }
            }

On "Add" command, nbrofrows has the right count.
 But, On 'Edit' command, the count is always zero.

Thanks !!!!
Sri

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 08 Aug 2012, 07:42 AM
Hi,

You can get the total row count while editing in ItemCommand event which fires before ItemDataBound event.
C#:
protected void grdOrders_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.EditCommandName)
    {
       int nbrofrows = grdOrders.MasterTableView.Items.Count;
    }
}

Thanks,
Shinu.
0
Sravanthi
Top achievements
Rank 1
answered on 08 Aug 2012, 02:32 PM
Its working. Thanks!!!

One more question, How can i distinguish between add or edit in ItemDatabound event??

Thanks,
Sri
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Aug 2012, 07:51 PM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode && e.Item is GridEditableItem )
        {
            if(e.Item is GridDataInsertItem) // for editmode="form" use GridEditFormInsertItem
{
 // insert mode
}
else
{
// edit mode
}
  
        }
    }


Thanks,
Jayesh Goyani

Tags
Grid
Asked by
Sravanthi
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sravanthi
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or