hi,
I am trying to make a order by gridview.
I am using dropdown for item name selection and the next column is label display price of that item.
The next column is textbox quantity inserted by user and last column is label amount display calculated amount on the basis of price and quantity.
Find following my code to provide me a solution.
<telerik:RadGrid ID="radGrid1" runat="server" OnInsertCommand="radGrid1_InsertCommand" AutoGenerateColumns="False" GridLines="None"
AllowMultiRowEdit="True" AllowAutomaticInserts="true" OnItemDataBound="radGrid1_ItemDataBound" önNeedDataSource="radGrid1_NeedDataSource">
<mastertableview autogeneratecolumns="false" editmode="InPlace" showfooter="false" commanditemdisplay="Top" önprerender="radGrid1_PreRender">
<columns>
<telerik:gridtemplatecolumn headertext="Product Name" uniquename="Product_Name" xmlns:telerik="#unknown">
<itemtemplate>
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataTextField="product_Name"
DataValueField="product_Name" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
</itemtemplate>
</telerik:gridtemplatecolumn>
<telerik:gridtemplatecolumn headertext="Product_rate" xmlns:telerik="#unknown">
<itemtemplate>
<asp:Label ID="lblrate" runat="server">
</itemtemplate>
</telerik:gridtemplatecolumn>
<telerik:gridtemplatecolumn headertext="Product Quantity" xmlns:telerik="#unknown">
<edititemtemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("Product_Quantity")%>' />
</edititemtemplate>
</telerik:gridtemplatecolumn>
<telerik:gridtemplatecolumn headertext="Product Amount" xmlns:telerik="#unknown">
<itemtemplate>
<asp:Label ID="lblAmount" runat="server" />
</itemtemplate>
</telerik:gridtemplatecolumn>
</columns></mastertableview>
javascript code is...
<script type="text/javascript" language="javascript">
function show(labelrate, labelamount, txtqty) {
var txt_Quantity = $find(txtqty);
var lbl_Rate = $find(labelrate)
var lbl_Amount = $find(labelamount);
var total = txt_Quantity.Getvalue() * lbl_Rate.Getvalue();
labelamount.Setvalue(total);
}
</script>
protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem || e.Item.IsInEditMode)
{
GridDataItem item = (GridDataItem)e.Item;
DropDownList list = (DropDownList)item.FindControl("ddl1");
list.Items.Insert(0, new ListItem("select", "-1"));
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select product_name from product_detail", con);
DataSet ds = new DataSet();
da.Fill(ds);
list.DataSource = ds;
list.DataBind();
Label lbl_Rate = (Label)item.FindControl("lblRate");
TextBox txt_Quantity = (TextBox)item.FindControl("txtQuantity");
Label lbl_Amount = (Label)item.FindControl("lblAmount");
txt_Quantity.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
lbl_Rate.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
lbl_Amount.Attributes.Add("onFocusout", "return show('" + txt_Quantity.ClientID + "','" + lbl_Rate.ClientID + "','" + lbl_Amount.ClientID + "')");
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList)sender;
GridDataItem item = (GridDataItem)list.NamingContainer;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select product_rate from product_detail where product_name='" + list.SelectedItem.Text + "'", con);
SqlDataReader reader; ;
reader = cmd.ExecuteReader();
if (reader.Read())
{
(item.FindControl("lblRate") as Label).Text = reader["product_rate"].ToString();
}
}