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

bind usercontrol in radgrid

13 Answers 353 Views
Grid
This is a migrated thread and some comments may be shown as answers.
niloofar
Top achievements
Rank 1
niloofar asked on 04 Jan 2011, 10:22 AM
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

13 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Jan 2011, 10:57 AM
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.
0
niloofar
Top achievements
Rank 1
answered on 04 Jan 2011, 11:45 AM
thank you very much
0
niloofar
Top achievements
Rank 1
answered on 06 Jan 2011, 08:51 AM
command "if" did not fire for insert ("add new record")
how can i change?
because i need bind treeview
thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 06 Jan 2011, 09:54 AM
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,
0
niloofar
Top achievements
Rank 1
answered on 08 Jan 2011, 10:02 AM
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>
0
niloofar
Top achievements
Rank 1
answered on 10 Jan 2011, 12:03 PM
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
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Jan 2011, 12:49 PM
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.
0
niloofar
Top achievements
Rank 1
answered on 11 Jan 2011, 11:37 AM
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
0
niloofar
Top achievements
Rank 1
answered on 13 Jan 2011, 07:20 AM
help please
0
niloofar
Top achievements
Rank 1
answered on 15 Jan 2011, 08:07 AM
hi
when i insert record panel dont close automatically
after insert i must click cancel for close panel
plz help
0
Princy
Top achievements
Rank 2
answered on 18 Jan 2011, 06:33 AM
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.
0
niloofar
Top achievements
Rank 1
answered on 19 Jan 2011, 01:23 PM
hi
thanks for your answer
AllowAutomaticInserts  is 'false' but when i updated and inserted not close automatically
0
Kiranmayee
Top achievements
Rank 1
answered on 11 Sep 2018, 01:06 PM

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.

Tags
Grid
Asked by
niloofar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
niloofar
Top achievements
Rank 1
Kiranmayee
Top achievements
Rank 1
Share this question
or