ComboBox-DataSource and Grid

11 posts, 2 answers
  1. Marcus
    Marcus avatar
    13 posts
    Member since:
    Jan 2011

    Posted 26 May 2011 Link to this post

    Hello. I have a question. I have a Grid. There's a ComboBox with DataSource with 3 fields. I want a grid TemplateColumn TeamName (Label.Text) binding with a ComboBox DataSource-Fields( not ComboBox.Value, not ComboBox.Text). I too would like a ComboBox SelectedIndexChanged TemplateColumn TeamName also changed. Is it possible?

    <body>
        <form id="form1" runat="server">
        <div>
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0"
                DataSourceID="SqlDataSource1" GridLines="None">
    <MasterTableView AutoGenerateColumns="False" DataKeyNames="Number"
                    DataSourceID="SqlDataSource1">
    <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
     
    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </RowIndicatorColumn>
     
    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </ExpandCollapseColumn>
     
        <Columns>
            <telerik:GridBoundColumn DataField="Number" DataType="System.Int32"
                FilterControlAltText="Filter Number column" HeaderText="Number" ReadOnly="True"
                SortExpression="Number" UniqueName="Number">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column"
                HeaderText="Year" UniqueName="TemplateYear" DataField="Year">
                <ItemTemplate>
                    <telerik:RadComboBox ID="RadComboBox1" Runat="server"
                        SelectedValue='<%# Bind("Year") %>'>
                        <Items>
                            <telerik:RadComboBoxItem runat="server" Text="2009" Value="2009" />
                            <telerik:RadComboBoxItem runat="server" Text="2010" Value="2010" />
                            <telerik:RadComboBoxItem runat="server" Text="2011" Value="2011" />
                        </Items>
                    </telerik:RadComboBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
                HeaderText="Team" UniqueName="TemplateColumn1" DataField="Team">
                <ItemTemplate>
                    <telerik:RadComboBox ID="RadComboBox2" Runat="server"
                        DataSourceID="SqlDataSource2" DataTextField="Code" DataValueField="Code"
                        SelectedValue='<%# Bind("Team") %>'
                        onselectedindexchanged="RadComboBox2_SelectedIndexChanged"
                        AutoPostBack="True" onitemdatabound="RadComboBox2_ItemDataBound">
                    </telerik:RadComboBox>
                    
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn2 column"
                HeaderText="TeamName" UniqueName="TemplateColumn2">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"
                        Text='<%# Bind("RadComboBox2.Attributes("Name")") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
     
    <EditFormSettings>
    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
    </EditFormSettings>
    </MasterTableView>
     
    <FilterMenu EnableImageSprites="False"></FilterMenu>
     
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
            </telerik:RadGrid>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:webzeus %>"
                 
                SelectCommand="SELECT top 15 [Number], [Year], [Team] FROM [Project] WHERE year >2008 and year <2012">
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                        ConnectionString="<%$ ConnectionStrings:webs %>"
                        SelectCommand="SELECT [Code], [Name], [Manager] FROM [Team] WHERE ([disabled] = @disabled)">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="false" Name="disabled" Type="Boolean" />
                        </SelectParameters>
            </asp:SqlDataSource>
         
        </div>
        </form>
    </body>
    Thanks
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 May 2011 Link to this post

    Hello Marcus,
    Try the following code snippet to achieve your scenario.
    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem item = (GridDataItem)e.Item;
               RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
               Label lbl1 = (Label)item.FindControl("Label1");
               lbl1.Text = combo.SelectedValue;
           }
       }
    protected void RadCombo1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
          GridColumn col=(GridColumn)rgd_ABIUser.MasterTableView.GetColumn("TemplateColumn2");
          col.HeaderText = (sender as RadComboBox).SelectedValue; 
       }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marcus
    Marcus avatar
    13 posts
    Member since:
    Jan 2011

    Posted 26 May 2011 Link to this post

    Thanks Shinu,
    how do I in label instead SelectedValue Attribute to assign?

    about as:
    Protected Sub RadComboBox2_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxItemEventArgs)
        Dim dataItem As DataRowView = CType(e.Item.DataItem, DataRowView)
        e.Item.Attributes("Name") = dataItem("Name")
    End Sub
     
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf e.Item Is GridDataItem) Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
            Dim combo As RadComboBox = dataItem.FindControl("RadComboBox2")
            Dim lbl As Label = dataItem.FindControl("Label1")
            lbl.Text = combo.Attributes("Name")
     
        End If
    End Sub

    lbl.Text = combo.Attributes("Name")
           but is missing reference to SelectedValue

    Thanks
  5. Marcus
    Marcus avatar
    13 posts
    Member since:
    Jan 2011

    Posted 26 May 2011 Link to this post

    done!!!!!
    THANKS!!!!!
  6. Marcus
    Marcus avatar
    13 posts
    Member since:
    Jan 2011

    Posted 26 May 2011 Link to this post

    how can I instead  header  item to change ?
    protected void RadCombo1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
          GridColumn col=(GridColumn)rgd_ABIUser.MasterTableView.GetColumn("TemplateColumn2");
          col.HeaderText = (sender as RadComboBox).SelectedValue;
       }
    so:
  7. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 May 2011 Link to this post

    Hello Marcus,

    I suppose you want to change the corresponding cell text under the column TeamName. If that the requirement, access the GridDataItem by using the NamingContatiner property of the RadComboBox. Here is a sample cpde.
    C#:
    Protected Sub RadCombo1_SelectedIndexChanged(sender As Object, e As RadComboBoxSelectedIndexChangedEventArgs)
        Dim item As GridDataItem = DirectCast(TryCast(sender, RadComboBox).NamingContainer, GridDataItem)
        item("TeamName").Text = TryCast(sender, RadComboBox).SelectedValue
    End Sub

    Thanks,
    Shinu.
  8. Marcus
    Marcus avatar
    13 posts
    Member since:
    Jan 2011

    Posted 27 May 2011 Link to this post

    Shinu, Thank you !!!!!!!!!!
  9. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 16 Jul 2011 Link to this post

    Hi Shinu and Telerik users,

    I need to manipulate the value of template column (RadNumericTextBox), this action fires when I change the value of column "ativ_status".

    The problem is that I need to execute the command "GridDataItem dataitem = (GridDataItem)e.Item;", but, I don´t have the argument "GridItemEventArgs" in "t0302_ativ_status_SelectedIndexChanged"

    Please, what can I do to solve this issue?

    Thanks, best 

    Daniel
    -------
    ASP.NET code:

    <telerik:GridDropDownColumn DataField="ativ_status" DataSourceID="SqlDataSource_Crono_AS"
                                              HeaderText="STATUS" ListTextField="nome" ListValueField="valor"
                                              UniqueName="ativ_status" 
                                              ItemStyle-Width="70px" HeaderStyle-HorizontalAlign="Center">
                  </telerik:GridDropDownColumn>

    <telerik:GridTemplateColumn HeaderText="%" UniqueName="Percent_concluido"
                                              ItemStyle-Width="30px" HeaderStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                      <asp:Label runat="server" ID="percent_conncluido" Text='<%# Eval("percent_concluido") %>'></asp:Label>&nbsp;%
                    </ItemTemplate>
                    <EditItemTemplate>
                      <telerik:RadNumericTextBox ID="t0302_percent_concluido" runat="server" DataType="System.Decimal"
                                                 Font-Bold="True" IncrementSettings-InterceptArrowKeys="true"
                                                 IncrementSettings-InterceptMouseWheel="true" 
                                                 MaxValue="100" MinValue="0" ShowSpinButtons="True"
                                                 Skin="Sunset" Style="text-align: center;" 
                                                 Width="75px" Culture="pt-BR" Type="Percent" 
                                                 AutoPostBack="True" DbValue='<%# Bind("percent_concluido") %>'
                                                 >
                        <FocusedStyle BorderStyle="Double" />
                        <EnabledStyle BorderStyle="Double" />
                        <NumberFormat DecimalDigits="0" GroupSizes="4" />
                      </telerik:RadNumericTextBox>
                    </EditItemTemplate>
                  </telerik:GridTemplateColumn>

    C# code:

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.IsInEditMode)
            {
                GridDataItem item = (GridDataItem)e.Item;
                GridDropDownListColumnEditor editor = (GridDropDownListColumnEditor)item.EditManager.GetColumnEditor("ativ_status");


                editor.ComboBoxControl.AutoPostBack = true;
                editor.ComboBoxControl.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(t0302_ativ_status_SelectedIndexChanged);


            }
        }


    protected void t0302_ativ_status_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

            GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
            var vstatus = (editedItem["ativ_status"].Controls[0] as RadComboBox).Text;

    ///// ERROR OCCURS IN THE NEXT LINE - DON´T EXISTS "Item" (this argument provided by GridItemEventArgs)
            GridDataItem dataitem = (GridDataItem)e.Item;
            RadNumericTextBox new_value = (RadNumericTextBox)dataitem["Percent_concluido"].FindControl("t0302_percent_concluido");

            if (vstatus == "Finalizada")
                new_value.Text = "100";
            else
                if (vstatus == "Não Iniciada")
                    new_value.Text = "0";
        }
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2011 Link to this post

    Hello Daniel,


    You can access the ComboBox using NamingContainer property.

    C#:
    protected void t0302_ativ_status_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
         GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
          //your code here
    }

    Thanks,
    Princy.
  11. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 18 Jul 2011 Link to this post

    Hi Princy,

    Thanks for your response.

    Your suggestion don´t access the combobox value inside of template column. If I don´t use template column I can access the value, the problem is when I have a control inside of template column.

    For example:

    <telerik:GridTemplateColumn HeaderText="%" UniqueName="Percent_concluido"
                                              ItemStyle-Width="30px" HeaderStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                      <asp:Label runat="server" ID="percent_conncluido" Text='<%# Eval("percent_concluido") %>'></asp:Label>&nbsp;%
                    </ItemTemplate>
                    <EditItemTemplate>
                      <telerik:RadNumericTextBox ID="t0302_percent_concluido" runat="server" DataType="System.Decimal"
                                                 Font-Bold="True" IncrementSettings-InterceptArrowKeys="true"
                                                 IncrementSettings-InterceptMouseWheel="true" 
                                                 MaxValue="100" MinValue="0" ShowSpinButtons="True"
                                                 Skin="Sunset" Style="text-align: center;" 
                                                 Width="75px" Culture="pt-BR" Type="Percent" 
                                                 AutoPostBack="True" DbValue='<%# Bind("percent_concluido") %>'
                                                 >
                        <FocusedStyle BorderStyle="Double" />
                        <EnabledStyle BorderStyle="Double" />
                        <NumberFormat DecimalDigits="0" GroupSizes="4" />
                      </telerik:RadNumericTextBox>
                    </EditItemTemplate>
                  </telerik:GridTemplateColumn>


    I need to access the value of "t0302_percent_concluido" control, but, this need to happen inside of this code:
    protected void t0302_ativ_status_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
    }

    I don´t have argument "GridItemEventArgs".

    Please, tips?

    Thanks, best

    Daniel
  12. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Jul 2011 Link to this post

    Hello Daniel,

    Try the following code snippet to access a label control in SelectedIndexChanged event of DropDownColumn.
    C#:
    void ComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
           GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
           var vstatus = (editedItem["DropDownListColumn"].Controls[0] as RadComboBox).Text;
           Label lbl = (Label)editedItem.FindControl("lbl");
           lbl.Text = "hello";
    }

    Thanks,
    Princy
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017