Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,


how to add values to the dropdown inside gridview and how to write the code for selectedindexchanged event for dropdown inside gridview in asp.net


my form having three columns in gridview one is quantity (dropdown list) and price and amount i want to calculate amount inside gridview. if i selected quantity is 2 then it calculate quatity* price how can i get the selectedindexchanged property for dropdown inside gridview or any other options. please help me.

Regards,
P.Maruthi
Posted

 
Share this answer
 
C#
       //inside rowdatabound of gridview find the dropdown and bind it like below

  DropDownList ddlQuantity = e.Row.FindControl("ddlQuantity");
     // now you have a ddl
     //{ write a method here to bind the ddl }



       // write this into the selectedindex_change of ddlQuantity

 Control ddlQuantity = (DropDownList)sender as Control;
 GridViewRow gvRow = ddlQuantity.NamingContainer as GridViewRow;

       //now you have the current row of the grid where the dropdown is
       // lets the dropdown selected value is 2 then

Label price =   (Label)gvReplyAdded.Rows[gvRow.RowIndex].FindControl("lblPrice");
Label Amount = (Label)gvReplyAdded.Rows[gvRow.RowIndex].FindControl("lblAmount");
Amount.Text = (Convert.ToInt32(price.Text) * Convert.ToInt32(((DropDownList)sender).SelectedValue)).ToString();

     //here you can get the control of gridview of the same row
 
Share this answer
 
v2
in gridview add dropdownlist and give its datasource sqldatasource. and a hidden field which bind to your query by which you bind.

by sqldatasource bind list. and on datarowbound set the selected value of dropdownlist by hidden field value. like that:-

XML
<itemtemplate>
<asp:dropdownlist runat="server" id="ddl" width="100%" datatextfield="FieldName" datavaluefield="FieldValue" appenddatabounditems="true" datasourceid="SqlDataSource1" xmlns:asp="#unknown">

<asp:listitem text="" value="0" selected="True" />
                                        </asp:dropdownlist>
                                        <asp:hiddenfield runat="server" value="<%# Bind("FieldId") %>" id="DDL1" xmlns:asp="#unknown" /> 
</itemtemplate>


XML
<asp:SqlDataSource ID="SqlDataSource1" SelectCommandType="StoredProcedure" SelectCommand="usp_Name"
                    runat="server">
                    <SelectParameters>
                        <asp:Parameter Name="ParamName" DefaultValue="0" />
                    </SelectParameters>
                </asp:SqlDataSource>





C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           DropDownList ddl = ((DropDownList)e.Row.FindControl("ddl"));
            HiddenField hdn = ((HiddenField)e.Row.FindControl("DDL1"));
  	    ddl.SelectedValue =	hdn.value
        }
}      
 
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