0

I am using Visual Studio and c#. I have a form that contains a Developer Express data-aware treelist called treelist1, which has a datasource property. I created a method to set the datasource property of the treelist:

public partial class UserControl1 : UserControl    {
    public UserControl1()        
    {
        InitializeComponent();
    }
            public void SetTreelist1DataSource(System.Data.DataTable dataTable)
    {
        treeList1.DataSource = dataTable;
    }
}
}

I am trying to set the dataTable property from another class:

class DataAccess
{
    public static void LoadDataSource()
    {
        string constring = "Default";
        using (SqlConnection cstr = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", cstr))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        UserControl1.SetTreelist1DataSource(dt);                            
                    }
                }
            }

        }
    }

}
}

The line UserControl1.SetTreelist1DataSource(dt); gets the compiler error, "An object reference is required for the nonstatic method, data field or property usercontrol1.settreelist1datasource(datatable)".

Any constructive help would be greatly appreciated.

Paul

user3673052
  • 147
  • 1
  • 7
  • `UserControl1` is a class. You need an instance of the class, which is typically embedded in your From. – Jasper Kent Jul 03 '20 at 07:18
  • You really shouldn't introduce dependencies between DataAccess and UI layers. Whoever calls `LoadDataSource`: return the DataTable to it and have it handle where to put it. – Fildor Jul 03 '20 at 07:20

0 Answers0