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

Hierarchical GridView - winform bind in code

4 Answers 216 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Thurgood
Top achievements
Rank 1
Richard Thurgood asked on 12 Jul 2008, 02:59 AM

Okay, I know I'm missing something here. I'm using the latest version of Rad for WinForms (Q1 2008) and I'm trying to populate a RadGridView using code only (not the BindingSource control). Can you tell me why this code isn't working?

private void PopulateGrid()
{
 DataSet ds = GetMasterDetail();
 radGridView1.DataSource = ds;
}

private DataSet GetMasterDetail()
{
  DataTable master = Clients.GetMasterList();
  master.TableName = "Clients";
  DataTable child = Orders.GetChildList();
  child.TableName = "Orders";

  DataSet ds = new DataSet();
  ds.Tables.Add(master.Copy());
  ds.Tables.Add(child.Copy());

  DataRelation relationship = new DataRelation("MasterDetail", ds.Tables["Clients"].Columns["ClientID"], ds.Tables["Orders"].Columns["ClientID"]);
  ds.Relations.Add(relationship);

  return ds;
}

4 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 14 Jul 2008, 12:38 PM
Hello Richard,

All your code looks right, except for the DataMember property of RadGridView, which is not set in this scenario. Your PopulateGrid method should be changed to following:

private void PopulateGrid()  
{  
    DataSet ds = GetMasterDetail();  
    radGridView1.DataMember = "Clients";  
    radGridView1.DataSource = ds;  
}  

I hope this helps. If you have further questions, feel free to write back.

Kind regards,
Julian Benkov
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Richard Thurgood
Top achievements
Rank 1
answered on 14 Jul 2008, 01:18 PM
I tried that but I was setting the DataMember after binding the datasource and it bombed. Duh!  My fault. Asside from that, I also needed to set the AutoGenerateHierarchyFromDataSet property to true.  So, here's what worked:

private void PopulateGrid()
{
 DataSet ds = GetMasterDetail();
 
 radGridView1.AutoGenerateHierarchyFromDataSet  = true;
 radGridView1.DataMember = "Clients";
 radGridView1.DataSource = ds;
}

private DataSet GetMasterDetail()
{
  DataTable master = Clients.GetMasterList();
  master.TableName = "Clients";
  DataTable child = Orders.GetChildList();
  child.TableName = "Orders";

  DataSet ds = new DataSet();
  ds.Tables.Add(master.Copy());
  ds.Tables.Add(child.Copy());

  DataRelation relationship = new DataRelation("MasterDetail", ds.Tables["Clients"].Columns["ClientID"], ds.Tables["Orders"].Columns["ClientID"]);
  ds.Relations.Add(relationship);

  return ds;
}

0
Narendra
Top achievements
Rank 2
answered on 27 Aug 2008, 05:16 AM
Hello,

    i was also trying to bind Hierarchical grid in code behind. But cant make it.
    i am trying to read from xml file and loaded to dataset.

    i have set  propery AutoGenerateHierarchyFromDataSet =true.
    but still i am unable to bind data in Hierarchical  format. 
    code goes here, Please check once let me know what was my mistake.

public frmBOM()
{
InitializeComponent();
PopulateGrid();
}

private void PopulateGrid()
{
DataSet ds = GetMasterDetail();
radGridView1.AutoGenerateHierarchyFromDataSet = true;
radGridView1.DataMember = "MasterDataHeader";
radGridView1.DataSource = ds;
}

private DataSet GetMasterDetail()
{
DataSet dsMaster = new DataSet();
dsMaster.ReadXml("c:\\My Documents\\Visual Studio 2008\\Projects\\MyTestFopl\\MyTestFopl\\dtMaster.xml");
DataSet dsChild = new DataSet();
dsChild.ReadXml("c:\\My Documents\\Visual Studio 2008\\Projects\\MyTestFopl\\MyTestFopl\\dtChild.xml");
DataTable master = dsMaster.Tables["MasterDataHeader"];
DataTable child = dsChild.Tables["MasterDataDetails"];
master.TableName = "MasterDataHeader";
child.TableName = "MasterDataDetails";
DataSet ds = new DataSet();
ds.Tables.Add(master.Copy());
ds.Tables.Add(child.Copy());
DataRelation relationship = new DataRelation("MasterDetail", ds.Tables["MasterDataHeader"].Columns["MDHCode"], ds.Tables["MasterDataDetails"].Columns["MDHCode"]);
ds.Relations.Add(relationship);
return ds;
}

 

Please suggest me what i am doing wrong.

Thanks in advance.
Regards
Narendra P 
    
0
Julian Benkov
Telerik team
answered on 29 Aug 2008, 11:46 AM
Hi Narendra,

Your code looks ok - we did not find any errors or omissions in it. Please, open a support ticket and send us a simple project to reproduce this issue locally. Thank you in advance.

 
Sincerely yours,
Julian Benkov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Richard Thurgood
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Richard Thurgood
Top achievements
Rank 1
Narendra
Top achievements
Rank 2
Share this question
or