How to bind different table at runime using Telerik GridView?

On the Form, I have ComboBox and GridView.

I have a combobox with 4 Table names.  Once the tablename is selected, I would like to open the table using.....

string TableID = radComboBox1.SelectedItem.ToString();
string queryString = string.Format("SELECT * FROM {0})", TableID);

By the way, I could change "SELECT *" to "SELECT FirstName, LastName", etc.
Just note that these same fields are not in the other table so I will have another SELECT with different field names.  Number of fields in each table could be different

All these 4 tables have different fields.

I have radGridView1 defined.  Could someone help me write the code to bind to the GridView?  I would like to bind (to same GridView) again with different table if user changes TableName.

LVL 13
Who is Participating?
Vikram Singh SainiConnect With a Mentor Software Engineer cum AD DeveloperCommented:

Go this way. (Below is the code and is meant for explanation purpose)

(1) Declare combobox with names of table as:

<telerik:RadComboBox ID="rcbTables" runat="server" AutoPostBack="true" Width="100px"
        Skin="Office2010Silver" OnSelectedIndexChanged="rcbTables_SelectedIndexChanged">
            <telerik:RadComboBoxItem Text="Expenses" Value="Expenses" Selected="true" />
            <telerik:RadComboBoxItem Text="Earning" Value="Earning" />

Open in new window

(2) Declare RadGrid as:

<telerik:RadGrid ID="RadGridData" runat="server" Width="100%" PageSize="50" AllowPaging="True"
        AutoGenerateColumns="True" DataSourceID="sdsData" Skin="Office2010Silver" AllowSorting="true"

Open in new window

(3) Declare SqlDataSource as:

<asp:SqlDataSource ID="sdsData" runat="server" ConnectionString="<%$ ConnectionStrings:accdbConString %>"
        ProviderName="<%$ ConnectionStrings:accdbConString.ProviderName %>" />

Open in new window

(4) Connection string in web.config as: (Modify it as per your requirement)

        <add name="accdbConString" connectionString="Connection string here" providerName="Provider name here"/>

Open in new window

(5) Code behind (.aspx.cs) as:

public partial class Projects : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

        protected void rcbTables_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
            switch (rcbTables.SelectedValue)
                case "Expenses":

                case "Earning":

        public void FillGrid(string tableName)
            // Set SqlDataSource data
            sdsData.SelectCommand = "SELECT * FROM " + tableName;

            // Bind the SqlDataSource data to RadGrid
            RadGridData.DataSourceID = "sdsData";

Open in new window

** I had tested entire code first. Let me know if you are unable to get it successfully.
AshokAuthor Commented:

My project is not web based (ASP.NET).

It is WPF project.
AshokAuthor Commented:
Answer not in expected environment which is WPF.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.