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

Adding bound objects in child view in Hierarchy GridView

1 Answer 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 2
Steve asked on 30 Oct 2012, 06:17 PM
I have the following RowSourceNeeded handler :

void gvEmployes_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    if (e.Template == gvtEmployeLogiciels)
    {
        using ( var repo = new EmployesRepository())
        {
            foreach (LogicielDTO l in repo.GetLogicielsOfEmploye((EmployeDTO)e.ParentRow.DataBoundItem))
            {
                GridViewRowInfo gridRow = e.Template.Rows.NewRow();
                gridRow.Cells["nom"].Value = l.nom;
                gridRow.Cells["nbLicense"].Value = l.nbLicense;
                e.SourceCollection.Add(gridRow);
            }
        }
    }
}

However, I want the objects to be bound instead of just filling in cells so that something like this would work :

void gvEmployes_UserDeletingRow(object sender, GridViewRowCancelEventArgs e)
{
    foreach (var row in e.Rows)
    {
        switch (row.DataBoundItem.GetType().Name) // This will error if you are deleting a LogicielDTO row.
        {
            case "EmployeDTO":
                MessageBox.Show("Deleteing " + ((EmployeDTO)row.DataBoundItem).nom);
                break;
            case "LogicielDTO":
                MessageBox.Show("Deleteing " + ((LogicielDTO)row.DataBoundItem).nom);
                break;
        }
    }
}

How can I acheive this?

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 02 Nov 2012, 07:53 AM
Hello Caroline,

Thank you for writing.

When the RadGridView is used in this hierarchy mode the DataBoundItem for the inner level row will be always initialized to null and can not be used. This behavior is by design. In this case you can use the Tag property to hold and use some related information. Here is a code snippet using this approach:
void gvEmployes_RowSourceNeeded(object sender, GridViewRowSourceNeededEventArgs e)
{
    if (e.Template == gvtEmployeLogiciels)
    {
        using ( var repo = new EmployesRepository())
        {
            foreach (LogicielDTO l in repo.GetLogicielsOfEmploye((EmployeDTO)e.ParentRow.DataBoundItem))
            {
                GridViewRowInfo gridRow = e.Template.Rows.NewRow();
                gridRow.Cells["nom"].Value = l.nom;
                gridRow.Cells["nbLicense"].Value = l.nbLicense;
                gridRow.Tag = l;
                e.SourceCollection.Add(gridRow);
            }
        }
    }
}
 
void gvEmployes_UserDeletingRow(object sender, GridViewRowCancelEventArgs e)
{
    foreach (var row in e.Rows)
    {
        if(row.DataBoundItem == null)
        {
            LogicielDTO l = row.Tag as LogicielDTO;
            if(l != null)
            {
                //your logic here...
            }
            return;
        }
         
        switch (row.DataBoundItem.GetType().Name) // This will error if you are deleting a LogicielDTO row.
        {
            case "EmployeDTO":
                MessageBox.Show("Deleteing " + ((EmployeDTO)row.DataBoundItem).nom);
                break;
            case "LogicielDTO":
                MessageBox.Show("Deleteing " + ((LogicielDTO)row.DataBoundItem).nom);
                break;
        }
    }
}

I hope this helps. 

All the best,
Julian Benkov
the Telerik team
Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Steve
Top achievements
Rank 2
Answers by
Julian Benkov
Telerik team
Share this question
or