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

RadCombo in RadGrid duplicate value

1 Answer 184 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
ZAFFAR
Top achievements
Rank 1
ZAFFAR asked on 27 Feb 2012, 01:17 PM
In my project i have data entry grid (Rad GRID) with Rad Combo box . problem I am facing is, when I press "Add Row" button,
all rad combo box value changing to first raw of the database table. please help me to sort this issue
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Height="75px" Width="75px" Transparency="50">
            <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' style="border:0;" />
        </telerik:RadAjaxLoadingPanel>
        <telerik:RadGrid ID="RadGrid1" AllowMultiRowSelection="true" AllowMultiRowEdit="true"
            Skin="WebBlue" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
            Width="97%" PageSize="14" AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False"
            OnNeedDataSource="RadGrid1_NeedDataSource" ShowStatusBar="true" >
            <PagerStyle Mode="Slider" />
            <ClientSettings>
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
            <MasterTableView CommandItemDisplay="Top"  AutoGenerateColumns="False" Width="100%">
                <CommandItemTemplate>
                <div style="padding:10px 0px;">
                                                                              
                    <asp:Button runat="server" ID="btnAdd" Text="Add row" OnClick="btnAdd_Click" />
                      
                    <asp:LinkButton ID="LinkButton1" OnClientClick="javascript:return confirm('Delete all selected Products?')"
                    runat="server" CommandName="DeleteSelected" CausesValidation="false" ><img style="border:0px;vertical-align:middle;" alt="" src="images/Delete.gif" /> Delete</asp:LinkButton>
                        
                    
                    </div>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridClientSelectColumn HeaderStyle-Width="40px" />
                 
                            
                           <telerik:GridTemplateColumn UniqueName="pk_productid" HeaderText="Product Code" >
                            <FooterTemplate>
                              Template footer</FooterTemplate>
                            <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                            <ItemTemplate>
                             
                             
                             
                              <telerik:RadComboBox runat="server" ID="RadComboBox1"   AutoPostBack="true"  DataSourceID="sqlproduct"
                              MarkFirstMatch="true" EnableLoadOnDemand="True" DataTextField="pk_productid"
                              OnItemsRequested="RadComboBox1_ItemsRequested" OnClientItemsRequested="UpdateItemCountField"
                              OnDataBound="RadComboBox1_DataBound" OnItemDataBound="RadComboBox1_ItemDataBound"
                              HighlightTemplatedItems="true" Height="140px" Width="220px" DropDownWidth="420px"
                              OnSelectedIndexChanged="OnSelectedIndexChangedHandler" >
                                <HeaderTemplate>
                                  <ul>
                                    <li class="col1">Product Code</li>
                                    <li class="col2">Description</li>
                                  </ul>
                                </HeaderTemplate>
                                <ItemTemplate>
                                  <ul>
                                    <li class="col1">
                                      <%#DataBinder.Eval(Container.DataItem, "pk_productid")%>
                                    </li>
                                    <li class="col2">
                                      <%#DataBinder.Eval(Container.DataItem, "descript")%></li>
                                  </ul>
                                </ItemTemplate>
                                <FooterTemplate>
                                A total of
                                <asp:Literal runat="server" ID="RadComboItemsCount" />
                                items
                            </FooterTemplate>
                              </telerik:RadComboBox>
                           </ItemTemplate>
                          </telerik:GridTemplateColumn>
                            
                                      
                                          
                   
                    <telerik:GridTemplateColumn HeaderText="Quantity" SortExpression="prQty" UniqueName="prqty"
                        EditFormColumnIndex="1">
                        <HeaderStyle Width="80px" />
                        <ItemTemplate>
                             
                            <asp:TextBox runat="server" ID="txtprqty" Text='<%# Bind("prQty", "{0:#.##}") %>'></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
                  
            </MasterTableView>
        </telerik:RadGrid><br />
         
                        </div>
                         
                         
                    </div>                      
        
    
        <asp:SqlDataSource ID="sqlds2" runat="server" ConnectionString="<%$ ConnectionStrings:conDB %>"
        SelectCommand="SELECT fk_productID,prqty from tblPurchaserequest where docnum=0">
        </asp:SqlDataSource>
         
        <asp:SqlDataSource ID="sqlProduct" runat="server" ConnectionString="<%$ ConnectionStrings:conDB %>"
        SelectCommand="SELECT TOP 5 pk_productID,descript from tblProduct">
        </asp:SqlDataSource>
         
        <script type="text/javascript">
        function UpdateItemCountField(sender, args) {
            //set the footer text
            sender.get_dropDownElement().lastChild.innerHTML = "A total of " + sender.get_items().get_count() + " items";
        }
        </script>
Protected Sub RadComboBox1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        'set the initial footer label
        CType(cmbsite.Footer.FindControl("RadComboItemsCount"), Literal).Text = Convert.ToString(cmbsite.Items.Count)
    End Sub
   
  Protected Sub RadComboBox1_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
 
        Dim sql As String = "SELECT * from tblProduct WHERE pk_productID LIKE '%" + e.Text + "%' or descript LIKE '%" + e.Text + "%'"
 
        sqlProduct.SelectCommand = sql
        Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox)
 
        comboBox.DataBind()
    End Sub
 
    Protected Sub RadComboBox1_ItemDataBound(ByVal sender As Object, ByVal e As RadComboBoxItemEventArgs)
        e.Item.Text = (DirectCast(e.Item.DataItem, DataRowView))("pk_productID").ToString()
        e.Item.Value = (DirectCast(e.Item.DataItem, DataRowView))("descript").ToString()
    End Sub
 
        
    Protected Sub OnSelectedIndexChangedHandler(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        Session("pk_productid") = e.Value
    End Sub
 
    Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
        'Bind the grid to the source
        RadGrid1.DataSource = GetData
        If IsPostBack Then
            ViewState("dt") = dt
            dt = CType(ViewState("dt"), DataTable)
            RadGrid1.DataSource = dt
        End If
 
    End Sub
    Protected Property GetData() As DataTable
        Get
            If ViewState("dt") Is Nothing Then
                dt.Columns.Add("pk_productid", GetType(String))
                dt.Columns.Add("prqty", GetType(Int32))
                dt.Rows.Add("-", 0)
                ViewState("dt") = dt
            Else
                dt = DirectCast(ViewState("dt"), DataTable)
            End If
            Return dt
        End Get
        Set(ByVal value As DataTable)
            'Add the source to viewstate
            ViewState("dt") = value
        End Set
    End Property
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
       
        dt = CType(ViewState("dt"), DataTable)
         
        For Each dataItem As GridDataItem In RadGrid1.Items
            dt.Rows(dataItem.ItemIndex)("pk_productid") = CType(dataItem("pk_productid").FindControl("RadComboBox1"), RadComboBox).SelectedItem
            dt.Rows(dataItem.ItemIndex)("prqty") = CType(dataItem("prqty").FindControl("txtprqty"), TextBox).Text
Next
    ViewState("dt") = AddRow(dt)
        RadGrid1.Rebind()
    End Sub
   
    Private Function AddRow(ByVal dt As DataTable) As DataTable
        Dim dr As DataRow = dt.NewRow
        dr(0) = "-"
        dr(1) = 0
        dt.Rows.Add(dr)
        
        Return dt
    End Function

1 Answer, 1 is accepted

Sort by
0
ZAFFAR
Top achievements
Rank 1
answered on 27 Feb 2012, 03:44 PM
It solved with the with following sub, this procedure called after the radgrid1.rebind function in btnAdd_click
Private Sub RebindRadComboBoxes()
        Dim dt As DataTable = CType(ViewState("dt"), DataTable)
        Dim x As Integer = 0
        For Each row As GridDataItem In RadGrid1.Items
            Dim rcb As RadComboBox = CType(row.FindControl("RadComboBox1"), RadComboBox)
            rcb.Text = dt.Rows(x).Item("pk_productid")
            x = x + 1
        Next
 
    End Sub
Tags
ComboBox
Asked by
ZAFFAR
Top achievements
Rank 1
Answers by
ZAFFAR
Top achievements
Rank 1
Share this question
or