This is a migrated thread and some comments may be shown as answers.

How can use dropdownlist inside gridview for display value in label

8 Answers 3887 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shivesh
Top achievements
Rank 1
shivesh asked on 22 Oct 2010, 01:32 PM
hi,
can anyboby give me a sample code for display respected value in lable on selection of dropdownlist inside gridview.

my prob is that i want to select a item inside radgrid by dropdownlist and want to display corresponding value in a label.

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Oct 2010, 06:41 AM
Hello Shivesh,
 You can achieve this by attaching 'onselectedindexchanged' event to DropDownList. In that event handler access the GridDataItem using NamingContainer property of DropDownList.  Then access the Label using FindControl method and set the selected value of DropDownList to the Label Text.  

Mark-Up:
<Columns>
        <telerik:GridTemplateColumn HeaderText="LastName" DataField="LastName">
             <ItemTemplate>
               <asp:DropDownList ID="DropDownList1" runat="server"
           
DataSourceID="SqlDataSource1"  DataTextField="LastName"
             onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
                </asp:DropDownList>
               </ItemTemplate>
                  ............
          </telerik:GridTemplateColumn>
          <telerik:GridTemplateColumn HeaderText="Selected" DataField="Selected">
                <ItemTemplate>
                  <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                 </ItemTemplate>
                        .............
           </telerik:GridTemplateColumn>        
</Columns>

C#:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
      DropDownList list = (DropDownList)sender;
      GridDataItem item = (GridDataItem)list.NamingContainer;
      string str = list.SelectedValue;
      (item.FindControl("Label1") as Label).Text = str;
  }

Regards,
Shinu.
0
shivesh
Top achievements
Rank 1
answered on 27 Oct 2010, 06:20 AM
Hi,
Can any body send me an complete example for dropdown populating inside radgridview and respective label change their value on the basis of dropdownlist selection.
i do't want to use sqldatasource i want to use bind grid and dropdown by code.

what ever i did is giving u for yr refrence...
<telerik:RadGrid ID="radGrid1" runat="server" AutoGenerateColumns="False" GridLines="None"
           AllowMultiRowEdit="True" OnItemDataBound="radGrid1_ItemDataBound" OnNeedDataSource="radGrid1_NeedDataSource">
           <MasterTableView AutoGenerateColumns="false">
               <Columns>
                   <telerik:GridTemplateColumn HeaderText="Product Name" UniqueName="Product_Name">
                   <ItemTemplate>
                   <asp:Label ID="lblName" runat="server"><%#DataBinder.Eval(Container.DataItem,"Product_name")%></asp:Label>
                   </ItemTemplate>
                       <ItemTemplate>
                           <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataTextField="product_Name" DataValueField="product_Name" ></asp:DropDownList>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product_rate">
                       <ItemTemplate>
                           <asp:Label ID="lblrate" runat="server" ></asp:Label>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product Quantity">
                       <EditItemTemplate>
                           <asp:TextBox ID="txtQuantity" runat="server" />
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="Product Amount">
                       <ItemTemplate>
                           <asp:Label ID="lblAmount" runat="server" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
and my code behind is..
protected void radGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
   {
       SqlConnection connection = new SqlConnection();
       connection = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
       SqlDataAdapter da = new SqlDataAdapter();
       da.SelectCommand = new SqlCommand("select product_id,product_name from product_detail", connection);
 
       DataSet ds = new DataSet();
       try
       {
           da.Fill(ds, "product_detail");
       }
       finally
       {
           connection.Close();
       }
       radGrid1.DataSource = ds;
   }
I did only that plz help me..

Thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Oct 2010, 07:43 AM
Hello Shivesh,

I hope the following sample code will help you to achieve your scenario.

ASPX:
<Columns>
   <telerik:GridTemplateColumn>
       <ItemTemplate>
         <asp:Label ID="lblName" runat="server"><%#DataBinder.Eval(Container.DataItem, "product_name")%></asp:Label>
       </ItemTemplate>
   </telerik:GridTemplateColumn>
   <telerik:GridTemplateColumn HeaderText="Product Name" UniqueName="Product_Name">
       <ItemTemplate>
         <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
          DataTextField="product_name" DataValueField="product_name"
          OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
         </asp:DropDownList>
        </ItemTemplate>
  </telerik:GridTemplateColumn>
</Columns>

C#:
protected void radGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           DropDownList list = (DropDownList)item.FindControl("ddl1");
           SqlConnection connection = new SqlConnection();
           connection = new SqlConnection(ConfigurationManager.ConnectionStrings["chalk_hillConnectionString"].ConnectionString);
           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = new SqlCommand("select product_name from product_detail", connection);
           DataSet ds = new DataSet();
           list.DataSource = ds; //populating DropDownList
           list.DataBind();
       }
   }
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DropDownList list = (DropDownList)sender;
       GridDataItem item = (GridDataItem)list.NamingContainer;
       string str = list.SelectedValue;
       (item.FindControl("lblName") as Label).Text = str; //populating Label with selected value of DropDownList.
   }


Thanks,
Princy.
0
shivesh
Top achievements
Rank 1
answered on 27 Oct 2010, 10:47 AM
Thanks princy...
but here i want to dispaly product rate in label but here you have given code that is display dropdown item text in label.
Here my prob is first column is dropdown(Product Name) second column label(Product rate).
I want to product rate on label column on selection of item from drop down.

now my code like this..
<telerik:RadGrid ID="radGrid1" runat="server" AutoGenerateColumns="False" GridLines="None"
            AllowMultiRowEdit="True" OnItemDataBound="radGrid1_ItemDataBound" OnNeedDataSource="radGrid1_NeedDataSource">
            <MasterTableView AutoGenerateColumns="false" OnPreRender="radGrid1_PreRender">
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="Product Name" UniqueName="Product_Name">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" DataTextField="product_Name"
                                DataValueField="product_Name" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn HeaderText="Product_rate">
                        <ItemTemplate>
                            <asp:Label ID="lblrate" runat="server"></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn HeaderText="Product Quantity">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtQuantity" runat="server" />
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn HeaderText="Product Amount">
                        <ItemTemplate>
                            <asp:Label ID="lblAmount" runat="server" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
       {
           DropDownList list = (DropDownList)sender;
           GridDataItem item = (GridDataItem)list.NamingContainer;
           string str = list.SelectedValue;
           (item.FindControl("lblRate") as Label).Text = str;
       }

I think i have to something change on dropdown item selected index change . may be put a query but i do't know.

Thanks in advance ....
0
shivesh
Top achievements
Rank 1
answered on 27 Oct 2010, 12:24 PM
i got my solution thanks for your support
0
komal
Top achievements
Rank 1
answered on 20 Feb 2017, 08:07 AM

Thanks Sir above code is very helpful for project.

0
Uma
Top achievements
Rank 1
answered on 05 Jul 2017, 07:41 AM

GridDataItem item = (GridDataItem)list.NamingContainer;

i m getting error for namingcontainer

0
Uma
Top achievements
Rank 1
answered on 05 Jul 2017, 07:59 AM
Thank u It helped me lot i got 
Tags
Grid
Asked by
shivesh
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
shivesh
Top achievements
Rank 1
Princy
Top achievements
Rank 2
komal
Top achievements
Rank 1
Uma
Top achievements
Rank 1
Share this question
or