Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Biding one GridView with Different tables from the database based on user selection

By: Vincent Maverick Durano

This article demonstrates on how are we going to bind a GridView with different data from different tables based from the selection in the RadioButonList..

In-order to achieve this functionality then we need to dynamically generates a boundfield columns because the GridView will have different datafields to display. Also note that Im using the Northwind database here just for demo.

STEP 1: Suppose that you have this markup below in your ASPX file.

<asp:RadioButtonList ID="RadioButtonList1" runat="server"AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">

        <asp:ListItem>Customers Info</asp:ListItem>

        <asp:ListItem>Product Info</asp:ListItem>

        </asp:RadioButtonList>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

</asp:GridView>

STEP 2: The Relevant codes would be something like below

private void BindCustomers()

{

        SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING"); // calling up your connection string that was configured  in you Web Config File

 

        DataTable dt = new DataTable(); // I use DataTable here because I only want to grab data in ONE Table.. If using mutilple tables then use DataSet instead

 

        conn.Open();

        String sql = "SELECT * FROM TableUsers"; //But i would suggest you to use parameterize queries or SP for security reasons

        SqlCommand cmd = new SqlCommand(sql, conn);

        SqlDataAdapter ad = new SqlDataAdapter(cmd);

        ad.Fill(dt);

 

        if (dt.Rows.Count > 0)

        {

            BoundField Field = new BoundField();

            Field.DataField = "UserID";

            Field.HeaderText = "User Name";

            DataControlField Col = Field;

            GridView1.Columns.Add(Col);

 

            Field = new BoundField();

            Field.DataField = "Address";

            Field.HeaderText = "User Address";

            Col = Field;

            GridView1.Columns.Add(Col);

 

            Field = new BoundField();

            Field.DataField = "PhoneNumbers";

            Field.HeaderText = "User Phone Number";

            Col = Field;

 

            GridView1.Columns.Add(Col);

            GridView1.DataSource = dt;

            GridView1.DataBind();

 

        }

        conn.Close();

 

    }

 

    private void BindOrders()

    {

        SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING"); // calling up your connection string that was configured  in you Web Config File

 

        DataTable dt = new DataTable(); // I use DataTable here because I only want to grab data in ONE Table.. If using mutilple tables then use DataSet instead

 

        conn.Open();

        String sql = "SELECT * FROM TableUserOrders"; //But i would suggest you to use parameterize queries or SP for security reasons

        SqlCommand cmd = new SqlCommand(sql, conn);

        cmdCommandType = CommandType.Text;

        cmd.ExecuteNonQuery();

 

        SqlDataAdapter ad = new SqlDataAdapter(cmd);

        ad.Fill(dt);

 

        if (dt.Rows.Count > 0)

        {

            BoundField Field = new BoundField();

            Field.DataField = "UserID";

            Field.HeaderText = "User Name";

            DataControlField Col = Field;

            GridView1.Columns.Add(Col);

 

            Field = new BoundField();

            Field.DataField = "ProductName";

            Field.HeaderText = "Product Name";

            Col = Field;

            GridView1.Columns.Add(Col);

 

            Field = new BoundField();

            Field.DataField = "DatePurchased";

            Field.HeaderText = "Date";

            Col = Field;

            Field = new BoundField();

            Field.DataField = "Amount";

            Field.HeaderText = "Total Amount";

            Col = Field;

 

            GridView1.Columns.Add(Col);

            GridView1.DataSource = dt;

            GridView1.DataBind();

 

        }

        conn.Close();

 

    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        int index = RadioButtonList1.SelectedIndex;

        switch (index)

        {

            case 0:

                {

                    BindCustomers(); // If users selects CustomerIfo in the radio button then GridView will populate the Customers

                    break;

                }

            case 1:

                {

                    BindOrders(); // Else GridView will populate the Orders

                    break;

 

                }

            default:

                break;

 

        }

    }

 

STEP 3: compile and run the application..

 

So that’s it! Hope you will find this post useful.

 

Technorati Tags: ,
Posted on Wednesday, July 23, 2008 5:02 PM ASP.NET , GridView | Back to top


Comments on this post: Binding one GridView with Different tables from the database based on user selection

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
Thanks Buddy.............

Your code has helped me
Left by Abhishek on Nov 25, 2008 6:30 PM

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
oh nice, nice nice. that is exactly what I need.. I spent days on googling how to do something similar and finally I found this one.!!!!!
Left by Eddie on Feb 20, 2009 10:33 AM

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
Error::A field or property with the name 'booking_Date' was not found on the selected data source.

error comes while binding second table
And booking_Date is the coloumn of first table
Left by prat on Mar 15, 2011 9:52 PM

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
I have two tables,but both tables have no common table & i need to bind both tables specific coloumns in one Gridview,can anyone help
Left by prat on Mar 16, 2011 1:08 PM

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
@prat,

A gridview can only handle one DataSource at a time. so if you are trying to display columns from different tables then would need to create a custom DataTable for that by merging the fields to make one column.
Left by Vinz on Mar 16, 2011 1:19 PM

# re: Binding one GridView with Different tables from the database based on user selection
Requesting Gravatar...
Nice article. Can u tell me how to update,insert,delete data in case we are using a single gridview as u showed in ur article... Plzz help
Left by nethra on Jul 21, 2011 12:02 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net