Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to make a order with the help of gridview where a dropdown select a item and label show rate of selected item.

and other two text box quantity inserted by user and amount calculated on item quantity and rate.

but when i am trying to bind these then all item and all rates of each item is polpulating in grid view when i am selecting item from dropdown no rate is change when ever i want if user selct other item then rate should be change.

following is my aspx code..


XML
<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnRowDataBound="gridView1_RowDataBound" PageSize="5">
            <Columns>
                <asp:TemplateField HeaderText="Product_Name">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="product_id" DataTextField="product_name" Width="100px" OnSelectedIndexChanged="DropDownList1_SelectedindexChanged"> </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Product_Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Product_Rate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="txtQuantity" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Amount">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAmount" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
             </Columns>
          </asp:GridView>

asp.net c# code.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

namespace RadControlsWebApp1
{
    public partial class testtttttttttttttttt : System.Web.UI.Page
    {
        DataTable dt;
        DataRow dr; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            fillgrid();
            createdatatable();
            }
        }
        protected void fillgrid()
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd = new SqlCommand("select product_name,product_rate from product_detail", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "product_detail");
            gridView1.DataSource = ds;
            gridView1.DataBind();
        }

        protected void createdatatable()
        {
       dt = new DataTable();

       dt.Columns.Add("product_name");
       dt.Columns.Add("product_rate");
       dt.Columns.Add("product_Quantity");
       dt.Columns.Add("product_Amount");

        }


        protected void btnHome_Click(object sender, EventArgs e)
        {
            //Response.Redirect("Home.aspx");
        }
      

        protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                SqlCommand cmd = new SqlCommand("select product_id,product_name,product_rate from product_detail", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
                ddl.DataSource = ds;
                ddl.DataTextField = "product_name";
                ddl.DataValueField = "product_id";
                ddl.DataBind();
            }
          
        }
        protected void DropDownList1_SelectedindexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd = new SqlCommand("select product_id,product_name,product_rate from product_detail", con);
            SqlDataReader reader;
            reader = cmd.ExecuteReader();
            if (reader.Read())
            { 
            }
          }
    }
}

i am unable to do how can i do that??

Thanks
Posted
Updated 14-Oct-10 0:26am
v2
Comments
Arun Jacob 14-Oct-10 5:58am    
Format the code
raju melveetilpurayil 14-Oct-10 6:26am    
formated ;P
khalid mehmood 14-Oct-10 8:16am    
have you set AutopostBack property to true of DropDownList1 ?

1 solution

Well,
1. Not very clear on what you are trying to do
2. Your code has hell lot of problems! Why are you opening a SQL connection and using on every Row Databound? :doh:

Whatever I got, you need to show rate of an item based on dropdown in grid. Try using XMLHttpRequest for this feature - it would be quick and good from usability prospective. Just pass on the dropdown value and based on it get the rate.
Further via Javascript you can also calculate the price based on quantity filled.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900