Thursday, 15 May 2014

To Display the data in Grid when Dropdownlist select

CREATE TABLE Product
(
Pid int IDENTITY(1,1) primary key ,
Pname varchar(50),
Price int
)

CREATE TABLE Customer
(
Cid int IDENTITY(1,1) primary key,
PId int FOREIGN KEY REFERENCES Product(Pid),
CustomerName varchar (50) NULL,
Address varchar (50) NULL,
Mobile bigint NULL
)

create proc .sp_DDL2GV // SP for displaying the data in Grid when DDL selected item 
@Pid int
as
select c.CustomerName,c.Address, c.Mobile, p.Pname, p.Price from Customer c  inner join Product p  on p.Pid= c.PId where p.Pid=@Pid

<table cellpadding="2" class="style1">
            <tr>
                <td>
                    <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                     
                    </asp:DropDownList>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                        <Columns>
                            <asp:BoundField DataField="CustomerName" HeaderText="Customer Name"
                                ReadOnly="True" />
                            <asp:BoundField DataField="Address" HeaderText="Address" ReadOnly="True" />
                            <asp:BoundField DataField="Mobile" HeaderText="Mobile No" ReadOnly="True" />
                            <asp:BoundField DataField="Pname" HeaderText="Product name" ReadOnly="True" />
                            <asp:BoundField DataField="Price" HeaderText="Price" ReadOnly="True" />
                        </Columns>
                    </asp:GridView>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddl();
        }
    }

    public void ddl() // To bind the Dropdownlist 
    {
        cmd = new SqlCommand("Select Pid, Pname from Product", con);
        con.Open();
        DropDownList1.DataSource = cmd.ExecuteReader();
        DropDownList1.DataTextField = "Pname";
        DropDownList1.DataValueField = "Pid";
        DropDownList1.DataBind();
        con.Close();

    }

//To Display the data in GridView when Dropdownlist Selected index changed
public DataSet gvdata()
    {
        
        cmd = new SqlCommand("sp_DDL2GV ", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Pid", DropDownList1.SelectedValue);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        return ds;
         
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvdata();

    }







No comments:

Post a Comment