0

In my project i have a UserControl named SubControl. In that UserControl which contains TreeList and two Button. the button used to focus the next and previous row of the treelist.

In another project I have another UserControl named MainControl various controls and Here i'm using the SubControl.

What my problem is, From MainControl i can't able to bind the data into the treelist from the database.

SqlConnection SqlCon = new SqlConnection("Data Source=source; Initial Catalog=dbname; Integrated Security=True");
        SqlCon.Open();
        SqlCommand ad = new SqlCommand("Select* from mytablename", SqlCon);
        SqlDataAdapter da = new SqlDataAdapter(ad);
        DataSet ds = new DataSet();

        da.Fill(ds);

        SubControl.DataSource = ds; 
        SubControl.DataBindings();

How can i achieve this. Helps appreciated

William Xifaras
  • 5,212
  • 2
  • 19
  • 21
User6667769
  • 745
  • 1
  • 7
  • 25
  • DataSource property of SubControl is of type string. So you can not assign dataset to string. Can you share the code of SubControl class? How are you using DataSource property of it to populate the Tree? – Chetan May 24 '17 at 11:44
  • @ChetanRanpariya I created Object type property for DataSource – User6667769 May 24 '17 at 11:50
  • How are you creating treeview using DataSource property? – Chetan May 24 '17 at 11:53
  • I dont have any code ...I simply add the TreeList in Usercontrol. then i add this usercontrol into another user control. in that 2nd usercontrol i need to bind the data to the treelist – User6667769 May 24 '17 at 11:58
  • Well in that case the answer below should help you. If you know which column to use as ValueMember and which column to use as DisplayMember I then it's pretty much simple. Did you try the approach explained in the answer? – Chetan May 24 '17 at 12:01
  • I have only one table – User6667769 May 24 '17 at 12:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145019/discussion-between-user6667769-and-chetan-ranpariya). – User6667769 May 24 '17 at 12:05
  • i have a usercontrol which is having Treelist. and im using the usercontrol from another usercontrol. is it possible to bind data from here> @ChetanRanpariya – User6667769 May 24 '17 at 12:08
  • Yes you can bind it. Use the answer provided below to understand how to do it. – Chetan May 24 '17 at 12:10

1 Answers1

2

You have to bind to the DataTable, not the DataSet.

SubControl.DataSource = ds.Tables[0];

From DevExpress:

https://www.devexpress.com/Support/Center/Question/Details/Q520794

In order to create the tree list hierarchical structure, it's necessary to specify two additional fields in the source DataTable. The first field must store the nodes' unique IDs (in most cases, there is already a primary key in the DataTable). The other field must contain the parent node's ID for each node. To specify these fields for the TreeList control, use the TreeList.KeyFieldName and TreeList.ParentFieldName properties. Please refer to the following help article for more information in this regard: Tree Generation Algorithm in the XtraTreeList.

William Xifaras
  • 5,212
  • 2
  • 19
  • 21