bind usercontrol in radgrid

14 posts, 3 answers
  1. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 04 Jan 2011 Link to this post

    hi
    i used usercontrol as editformsetting
    like this:
        <EditFormSettings UserControlName="~/UserControls/Permission.ascx" EditFormType="WebUserControl">
                        <EditColumn UniqueName="EditCommandColumn1">
                        </EditColumn>
                    </EditFormSettings>
    i have textbox and treeview in usercontrol
    i want when i click "Edit" in row fill textbox and treeview with datakeyname in grid
    how can i do these?
    thanks
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Jan 2011 Link to this post

    Hello Niloofar,

    Following is a sample code to achieve your requirement.

    Usercontrol(ASCX):
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <br />
       <telerik:RadTreeView ID="RadTreeView1" runat="server" AllowNodeEditing="True">
           <Nodes>
               <telerik:RadTreeNode Expanded="true">
                   <Nodes>
                   </Nodes>
               </telerik:RadTreeNode>
           </Nodes>
       </telerik:RadTreeView>

    ASPX:
    <telerik:RadGrid ID="RadGridAddress" runat="server" >
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID">
            <Columns>
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
             </Columns>
            <EditFormSettings UserControlName="~/RadGrid/UserControl_EDIT.ascx" EditFormType="WebUserControl">
                <EditColumn UniqueName="EditCommandColumn1">
                </EditColumn>
            </EditFormSettings>
       </MasterTableView>
    </telerik:RadGrid>

    C#:
    int rowindex = -1;
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.EditCommandName)
        {
            GridDataItem item = (GridDataItem)e.Item;
            rowindex = item.ItemIndex;
        }
    }
    protected void RadGridAddress_PreRender(object sender, EventArgs e)
    {
        if (rowindex > -1)
        {
            GridEditFormItem editItem = (GridEditFormItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[rowindex]; // accessing edit form
            GridDataItem item = (GridDataItem)editItem.ParentItem;
            UserControl userControl = (UserControl)editItem.FindControl(GridEditFormItem.EditFormUserControlID); //accessing user control
            TextBox txtbox = (TextBox)userControl.FindControl("TextBox2"); //accessing textbox in user control
            RadTreeView treeview = (RadTreeView)userControl.FindControl("RadTreeView1");//accessing treeview in user control
            RadTreeNode node = (RadTreeNode)treeview.Nodes[0];
            txtbox.Text = item.GetDataKeyValue("EmployeeID").ToString(); // setting DataKeyValue to TextBox
            node.Text = item.GetDataKeyValue("EmployeeID").ToString();// setting DataKeyValue to TreeView node
            rowindex = -1;
        }
    }

    Thanks,
    Princy.
  3. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 04 Jan 2011 Link to this post

    thank you very much
  4. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 06 Jan 2011 Link to this post

    command "if" did not fire for insert ("add new record")
    how can i change?
    because i need bind treeview
    thanks
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Jan 2011 Link to this post

    Hello Niloofar,

    You can try the following conditon to check whether the grid is in insert mode.

    C#:
    protected void RadGridAddress_PreRender(object sender, EventArgs e)
       {
          if (RadGridAddress.MasterTableView.IsItemInserted)
           {
               GridEditFormInsertItem editItem = (GridEditFormInsertItem)RadGridAddress.MasterTableView.GetInsertItem();
               UserControl userControl = (UserControl)editItem.FindControl(GridEditFormItem.EditFormUserControlID);
               TextBox txtbox = (TextBox)userControl.FindControl("TextBox2");
               RadTreeView treeview = (RadTreeView)userControl.FindControl("RadTreeView1");
               .   .   .    .   .    .   .   .
            }
       }

    Thanks,
    Princy,
  6. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 08 Jan 2011 Link to this post

    Hi
    i have this error:
    CS0103: The name 'DataItem' does not exist in the current context
     <tr>
    Line 85:         <td align="right" colspan="2">
    Line 86:             <asp:Button ID="btnUpdate" Text="??????" runat="server" CommandName="Update"
    Line 87:                 Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'>
    Line 88:             </asp:Button>

    i write these code:
        <asp:Button ID="btnUpdate" Text="??????" runat="server" CommandName="Update"
                    Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'>
                </asp:Button>
    <asp:Button ID="btnInsert" Text="???" runat="server" CommandName="PerformInsert"
    Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:Button>
                &nbsp;
    <asp:Button ID="btnCancel" Text="??????" runat="server" CausesValidation="False"
    CommandName="Cancel"></asp:Button>
  7. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 10 Jan 2011 Link to this post

    when i click "add new record" in radgrid then click update i have error  before i clicked cancel
    in update : int.Parse(item.Cells[3].Text --> error: Object reference not set to an instance of an object.
    thanks
  8. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Jan 2011 Link to this post

    Hello Niloofar,

    Make the following modification in the above code and see if it works now.

    ASPX:
    <telerik:RadGrid ID="RadGridAddress" runat="server" OnItemDataBound="RadGridAddress_ItemDataBound" DataSourceID="SqlDataSource1" >
        <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID">
            <Columns>
                .  .   .   .   .  .
            </Columns>
            <EditFormSettings UserControlName="~/RadGrid/UserControl_EDIT.ascx" EditFormType="WebUserControl">
                <EditColumn UniqueName="EditCommandColumn1">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

    ASCX(UserControl):

    <asp:TextBox ID="TextBox1" runat="server" Text="xx"></asp:TextBox>
    <br />
    <telerik:RadTreeView ID="RadTreeView1" runat="server" AllowNodeEditing="True">
        <Nodes>
            <telerik:RadTreeNode Expanded="true">
                <Nodes>
                </Nodes>
            </telerik:RadTreeNode>
        </Nodes>
    </telerik:RadTreeView>
    <asp:Button ID="Button1"
     Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
            runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
        CommandName="Cancel"></asp:Button>

    C#:
    protected void RadGridAddress_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormItem editItem = (GridEditFormItem)e.Item;
                GridDataItem item = (GridDataItem)editItem.ParentItem;
                UserControl userControl = (UserControl)editItem.FindControl(GridEditFormItem.EditFormUserControlID);
                TextBox txtbox = (TextBox)userControl.FindControl("TextBox1");
                RadTreeView treeview = (RadTreeView)userControl.FindControl("RadTreeView1");
                RadTreeNode node = (RadTreeNode)treeview.Nodes[0];
                txtbox.Text = item.GetDataKeyValue("EmployeeID").ToString();
                node.Text = item.GetDataKeyValue("EmployeeID").ToString();
            }
     
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem editItem = (GridEditFormInsertItem)e.Item;
                UserControl userControl = (UserControl)editItem.FindControl(GridEditFormItem.EditFormUserControlID);
                TextBox txtbox = (TextBox)userControl.FindControl("TextBox1");
                RadTreeView treeview = (RadTreeView)userControl.FindControl("RadTreeView1");
                RadTreeNode node = (RadTreeNode)treeview.Nodes[0];
                txtbox.Text = "some text";
                node.Text = "some text";
            }
        }

    Thanks,
    Princy.
  9. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 11 Jan 2011 Link to this post

    hi
    excuse me
    i ask a lot of questions
    when i clicked "add new record" i have this error:
    CS0246: The type or namespace name 'GridEditFormInsertItem' could not be found (are you missing a using directive or an assembly reference?)
    Source Error:

    Line 51:     <tr>
    Line 52:         <td colspan="4">
    Line 53: <asp:Button ID="Button1"Line 54:  Text='<%# (Container is GridEditFormInsertItem) ? "???" : "??????" %>'
    Line 55:         runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
    thanks a lot
  10. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 13 Jan 2011 Link to this post

    help please
  11. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 15 Jan 2011 Link to this post

    hi
    when i insert record panel dont close automatically
    after insert i must click cancel for close panel
    plz help
  12. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jan 2011 Link to this post

    Hello Niloofar,

    If you are performing insert operation from code behind(manually), you need to set AllowAutomaticInserts as 'false'. Check whether you have set it as 'true' which in turn cause this issue.

    Hope this helps,
    Princy.
  13. niloofar
    niloofar avatar
    58 posts
    Member since:
    Aug 2010

    Posted 19 Jan 2011 Link to this post

    hi
    thanks for your answer
    AllowAutomaticInserts  is 'false' but when i updated and inserted not close automatically
  14. Kiranmayee
    Kiranmayee avatar
    22 posts
    Member since:
    Jul 2018

    Posted 11 Sep 2018 Link to this post

    Hi,

    Can anyone please help me with adding existing records of a radgrid. I have a radgrid with parts and quantities. I put in a column with add option that comes up on all records as shown below. I want the syntax for a situation where up on the add button click on each record; I want to fill the Part Num and Quanitity field of the web user control to be auto filled by grabbing the values from the Part Num and Quantity fields of that record. Please see the picture below for understanding. 

    Thank you,

    Kiran.

Back to Top