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
AshokSr. Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikram Singh SainiSoftware 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokSr. Software EngineerAuthor Commented:

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

It is WPF project.
AshokSr. Software EngineerAuthor Commented:
Answer not in expected environment which is WPF.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.