Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 123, comments - 362, trackbacks - 0

My Links

News

Archives

Image Galleries

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

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 like this article.

 

Print | posted on Wednesday, July 23, 2008 5:02 PM |

Feedback

Gravatar

# re: Binding one GridView with Different tables from the database based on user selection

Thanks Buddy.............

Your code has helped me
11/25/2008 6:30 PM | Abhishek
Gravatar

# re: Binding one GridView with Different tables from the database based on user selection

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.!!!!!
2/20/2009 10:33 AM | Eddie
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: