DOTNET Rocks!

Vincent Maverick S. Durano

  Home  |   Contact  |   Syndication    |   Login
  9 Posts | 2 Stories | 14 Comments | 0 Trackbacks

News

This blog are Still work in progress

Archives

Image Galleries

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="GridView3" 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.

 

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