How can use dropdownlist inside gridview for display value in label

9 posts, 1 answers
  1. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 22 Oct 2010 Link to this post

    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.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Oct 2010 Link to this post

    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.
  3. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 27 Oct 2010 Link to this post

    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
  4. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Oct 2010 Link to this post

    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.
  5. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 27 Oct 2010 Link to this post

    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 ....
  6. shivesh
    shivesh avatar
    38 posts
    Member since:
    Sep 2010

    Posted 27 Oct 2010 Link to this post

    i got my solution thanks for your support
  7. komal
    komal avatar
    1 posts
    Member since:
    Feb 2017

    Posted 20 Feb 2017 in reply to Shinu Link to this post

    Thanks Sir above code is very helpful for project.

  8. Uma
    Uma avatar
    2 posts
    Member since:
    Jul 2017

    Posted 05 Jul 2017 in reply to Shinu Link to this post

    GridDataItem item = (GridDataItem)list.NamingContainer;

    i m getting error for namingcontainer

  9. Uma
    Uma avatar
    2 posts
    Member since:
    Jul 2017

    Posted 05 Jul 2017 in reply to Uma Link to this post

    Thank u It helped me lot i got 
Back to Top