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

[Best Practice] How set row data to UserControl controls ? (RadGrid EditForm with UserControl)

1 Answer 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
HHH
Top achievements
Rank 1
HHH asked on 24 Mar 2014, 11:56 AM
I use RadGrid EditForm with UserControl

    <telerik:RadGrid ID="personGrid" runat="server" AutoGenerateColumns="False" OnEditCommand="personGrid_EditCommand" OnUpdateCommand="personGrid_UpdateCommand">
            <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
                <EditFormSettings UserControlName="PersonItemsUC.ascx" EditFormType="WebUserControl">
                </EditFormSettings>
                <Columns>
                    <telerik:GridBoundColumn UniqueName="ID" Display="false" HeaderText="ID" DataField="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="Name" HeaderText="Name" DataField="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="Family" HeaderText="Family" DataField="Family">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="Age" HeaderText="Age" DataField="Age">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn UniqueName="MobileNo" HeaderText="MobileNo" DataField="MobileNo">
                    </telerik:GridBoundColumn>
                    <telerik:GridEditCommandColumn EditText="Update" UniqueName="EditCommandColumn">
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
    </telerik:RadGrid>


and I have UserControl like this (have Person Info data) My UC

each row in my grid is equivalent Person class (as my DTO)

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Family { get; set; }
        public string MobileNo { get; set; }
        public int Age { get; set; }
    }

so in My usercontrol I have a method like this

        public void SetDataToControls(Person entity)
        {
            txtName.Text = entity.Name;
            txtFamily.Text = entity.Family;
            txtAge.Text = entity.Age.ToString();
            txtMobileNo.Text = entity.MobileNo;
        }

get person and set data to corresponding control

for Updating I must show current data into usercontrol
so I think that I use SetDataToControls() into EditCommand method (but can not find UserControl) and pass an instance of Person to UserControl

I have 2 Questions 

1. How Get Current Row and Convert in to Person Class ?
2. What is best event for this purpose (Calling SetDataToControls()) EditCommand cannot find UserControl 

protected void personGrid_EditCommand(object sender, GridCommandEventArgs e) // What is the best event for this purpose ?
        {
            var p = new Person() { Age = 23, Name = "HHH", Family = "FFF", MobileNo = "09999" }; // How Convert Row to Person
            var uc = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as PersonItemsUC;
            uc.SetDataToControl(p);
}


















































































 















































1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 26 Mar 2014, 05:19 PM

Hello,

This help article explains how to access the user control: http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html. Search for the "Accessing Controls in Edit/Insert Mode" section that will treat custom user controls.

To get data from the edited row, examine this article: http://www.telerik.com/help/aspnet-ajax/grid-retrieve-primary-key-field-values-for-items.html.


Regards,

Marin Bratanov
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
Grid
Asked by
HHH
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or