Search code examples
c#devexpressdevexpress-windows-ui

Add a data source in my application at run-time


I am working on windows form application, and creating a designer application. I have used devexpress tools for the designing. I can add various kind of shapes and text labels on it, further I want to link the labels with database, so I want to connect it to a database. For that I want to add connection to database at run-time, and later using that data source I need to map the label to particular table's column so that labels value can be picked from that column. When I open the properties of that label, I need to select the data source, table, column. On pressing OK the label will take the value from that.

In later phase I need to read the data from any joined table, or views or query. In devexpress it is providing the Query Builder for that and it is having Data Source Connection wizard, but that is working at design time ony and I need to do that whole thing at run-time.


Solution

  • For connecting the database with the application, devexpress is providing a Dashboard Designer in which it is easy to connect to any database using a wizard, but using its object and calling its ShowDataSourceWizard() will invoke the wizard and show you query editor as well but when its finished, it cant fill your datagrid because the DashboardSqlDataSource component created by the Dashboard Designer control can be used only in dashboards and cannot be used in other controls

    To introduce the required functionality without dashboards, use the SqlDataSource component and the SqlDataSourceUIHelper class. SqlDataSourceUIHelper provides an API to invoke various tools available to end users for configuring the data connection settings in an application.

    SqlDataSource objSqlDataSource = new SqlDataSource();
    if(SqlDataSourceUIHelper.ConfigureConnection(objSqlDataSource))
    {
        if(SqlDataSourceUIHelper.ManageQueries(objSqlDataSource))
        {
            dataGridView1.DataSource = objSqlDataSource;
            dataGridView1.DataMember = objSqlDataSource.Queries[0].Name;
            objSqlDataSource.Fill();
            dataGridView1.Refresh();
        }  
    }
    

    ConfigureConnection() will open the wizard for connection to any database and ManageQueries() will let you create your queries using its query builder.