BlueMoon
BlueMoon

Reputation: 51

How to bind List<string> to gridview inside another gridview in asp.net

I just wanted to bind the below List object to Grid view. That list contains another List. So when I bind the datasource, that column is coming as EMPTY.

Details below:

List<Person> Persons = new List<Person>();
public class Person
{
    public string Name1;
    public string Name2;
    public List<string> Address;
    public DateTime DateOfBirth;
    public string TeamName;
}

I assigned my gridview as below:

var data = new List<Persons>();
data = GetData();
GrdPerson.DataSource = data;
GrdPerson.DataBind();

Since we have address as List<string> in datasource, it is coming as empty in page.

I know that Address does not contain any public property hence it is coming as empty. So I tried to use Gridview in Address column of Master Grid.

But do not know how to assign the datasource at a time.

Upvotes: 1

Views: 1290

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460288

You can handle the gridview's RowDataBound event to DataBind the inner grid:

protected void GrdPerson_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Person person = (Person)e.Row.DataItem;
        GridView innerGrid = (GridView)e.Row.FindControl("GrdPersonAddresses");
        innerGrid.DataSource = person.Address;
        innerGrid.DataBind();
    }
}

Upvotes: 1

Related Questions