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

Can't create child rows in grid when using object-relational hierarchy

1 Answer 53 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 15 Apr 2014, 04:16 PM
My RadGridView is bound to object defined as:

public class DataElement 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Datatype { get; set; }
        public string Size { get; set; }
        public bool PrimaryKey { get; set; }
        public bool ForeignKey { get; set; }
        public List<TagModel> Tags { get; set; }

        
    }
public class TagModel
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }
Which binds fine and the child row (of type TagModel) is displayed as expected with  the Add New Row option available


When I click on the "Click here to add a new row" a new row is added in the grid but after entering in data the row is not actually created.  It goes away if I collapse the parent row and nothing is added to the bound Tags list.  This appears to work fine with Datasets but not with objects.  I have tried handling different events but none seem to fire as I would have expected.   I am currently evaluating this product and I really need this to work in order to move forward with purchasing this product.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Apr 2014, 03:29 PM
Hello Mike,

Thank you for writing.

Please find the sample code snippet below, demonstrating how to achieve the desired scenario:
public Form1()
{
    InitializeComponent();
 
    BindingList<DataElement> items = new BindingList<DataElement>();
 
    for (int i = 0; i < 10; i++)
    {
        BindingList<TagModel> subItems = new BindingList<TagModel>();
        for (int j = 0; j < 5; j++)
        {
            subItems.Add(new TagModel(j,"Name" + i + "." + j));
        }
        items.Add(new DataElement(i,"Name" + i,"Description" + i,"DataType" + i, "Size" + i,i % 2 == 0,i % 5 == 0,subItems));
    }
 
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoGenerateHierarchy = true;
 
    this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.MasterTemplate.Templates[0].AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
 
public class DataElement
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public string Description { get; set; }
 
    public string Datatype { get; set; }
 
    public string Size { get; set; }
 
    public bool PrimaryKey { get; set; }
 
    public bool ForeignKey { get; set; }
 
    public BindingList<TagModel> Tags { get; set; }
 
    public DataElement()
    {
    }
 
    public DataElement(int id, string name, string description, string datatype,
        string size, bool primaryKey, bool foreignKey, BindingList<TagModel> tags)
    {
        this.Id = id;
        this.Name = name;
        this.Description = description;
        this.Datatype = datatype;
        this.Size = size;
        this.PrimaryKey = primaryKey;
        this.ForeignKey = foreignKey;
        this.Tags = tags;
    }
}
 
public class TagModel
{
    public int Id { get; set; }
 
    public string Name { get; set; }
 
    public TagModel()
    {
    }
 
    public TagModel(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
    GridViewRowInfo row = e.Rows[0];
    GridViewRowInfo parentRow = row.Parent as GridViewRowInfo;
    if (row is GridViewNewRowInfo && parentRow != null)
    {
        int id = 0 ;
        string name = string.Empty;
 
        if (row.Cells["Id"].Value != null)
        {
            int.TryParse(row.Cells["Id"].Value.ToString(), out id);
        }
 
        if (row.Cells["Name"].Value != null)
        {
            name = row.Cells["Name"].Value.ToString();
        }
        
        TagModel tagModel = new TagModel(id, name);
        
        ((DataElement)parentRow.DataBoundItem).Tags.Add(tagModel);
        row.ViewTemplate.Refresh();
    }
}

I have attached a sample video (drag and drop over the browser to play), illustrating better the implemented behavior.

Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Mike
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or