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

match the text of radgrid field to dropwdownlist

5 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 31 May 2012, 03:59 PM
What I am trying to do is match the text of my grid field to my dropdownlsit field text, but it always gives me the follwoing error even though the text is in the item is in the dropdown list in the grid.  I am trying to match computer make and models that come up from and existing table to a new database.  for example.

Optiplex 755
Optiplex 760
Precision 2000
latitude 2400

In my dropdown inside my radgird these are all in there but when the match tries to happen it tells me they are not, it should nto be a case issue.

 <telerik:GridBoundColumn DataField="strSN" HeaderText="SN" />
                                <telerik:GridBoundColumn DataField="strComputername" HeaderText="NETWORK NAME" />
                                <telerik:GridBoundColumn DataField="strBrand" HeaderText="EXIST_MODEL" />
                                <telerik:GridTemplateColumn HeaderText="DB_Model">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddlModel" runat="server" CausesValidation="false"></asp:DropDownList>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>

Code to fill grid dropdown
Protected Sub myRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridDataItem) Then
           Dim item As GridDataItem = CType(e.Item, GridDataItem)
           Dim options As DropDownList = DirectCast(item.FindControl("ddlModel"), DropDownList)
           Dim field As String = e.Item.Cells(5).Text.ToString.ToLower
           sql = "Select dm.intModelId, m.strmake + ' ' + dm.strModel Model from Drat_Model dm INNER JOIN Drat_Make m on m.intMakeId = dm.intMakeId Order by strModel"
           buildDD(sql, options)
           'options.SelectedItem.Text = DirectCast(DataBinder.Eval(e.Item.DataItem, field).ToString(), String)
       End If
   End Sub



Error
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'optiplex 760'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'optiplex 760'.
  
Source Error: 
  
  
Line 95: 
Line 96:             buildDD(sql, options)
Line 97:             options.SelectedItem.Text = DirectCast(DataBinder.Eval(e.Item.DataItem, field).ToString(), String)
Line 98:         End If
Line 99:     End Sub
  


but if I comment out and then populate it works and the Optiplex 760 is in the dropdownlist and is also the same as the field Exist_Model.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Jun 2012, 07:54 AM
Hi Kevin,

I guess you need to set the SelectedItem of the DropDownList with a cell text.Please take a look into the following code snippet i tried.

VB:
Private Sub RadGRid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim ditem As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim ddl As DropDownList = DirectCast(ditem.FindControl("ddlModel"), DropDownList)
        Dim field As String = e.Item.Cells(4).Text.ToString()
        ' bind the DropDownList here from database
        ddl.DataBind()
        ddl.SelectedItem.Text = field
    End If
End Sub

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 01 Jun 2012, 02:35 PM
Hi Shinu,

Ok that works to set the text alike, but I thought it would set the value along with it as it selected the liek text, but it always returns a 0 which is my default tot pick and item.  The problem i have it I do not have a value in DB to set that's why i am trying to do it with the text match, which agian I was hoping to set value, is there a way to force the value pick on this.

Protected Sub myRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridDataItem) Then
           Dim item As GridDataItem = CType(e.Item, GridDataItem)
           Dim options As DropDownList = DirectCast(item.FindControl("ddlModel"), DropDownList)
           Dim field As String = e.Item.Cells(5).Text.ToString
           sql = "Select dm.intModelId, m.strmake + ' ' + dm.strModel Model from Drat_Model dm INNER JOIN Drat_Make m on m.intMakeId = dm.intMakeId INNER JOIN Drat_Manufacturer mm on mm.intManufacturerId = " _
               & "m.intManufacturerID INNER JOIN Drat_Category c on c.intCategoryID = mm.intCategoryId where c.intCategoryID in (1,2) Order by strModel"
           options.Items.Add(New ListItem("Pick Model", "0"))
           buildDD(sql, options)
           options.SelectedItem.Text = field
       End If
   End Sub
Radgrid save method

If ddlOption.SelectedValue = 1 Then
          For Each Item As GridDataItem In myRadGrid.Items
              Dim Model As DropDownList = DirectCast(Item.FindControl("ddlModel"), DropDownList)
              Dim Id As String = Item.GetDataKeyValue("intExistId")
              sql = "Execute sp_importComputers " & Id & ", " & HFPersId.Value & ", " & Model.SelectedValue
              Response.Write(sql)
              Response.End()
              insertUpdateDelete(sql)
          Next


0
Shinu
Top achievements
Rank 2
answered on 04 Jun 2012, 05:10 AM
Hi Kevin,

Try setting the 'SelectedItem.Value' also so that you can get the value while saving also.

VB:
Private Sub myRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridDataItem) The
               . . .
               . . .
                                           .    .   .
           options.SelectedItem.Text = field
           options.SelectedItem.Value = field   'set the SelectedItem.Value
       End If
End Sub

Thanks,
Shinu.
0
Kevin
Top achievements
Rank 1
answered on 04 Jun 2012, 01:45 PM
Hi Shinu,
I orignally  thoght of theat but the value is really and ID, not the text  I load the dropdown with an ID and text, and then I load a field that has the text that I match to the dropwdown but I need the ID value and not the text value.  I think at this poitn I am asking the impossable and they will jsut have to pick the dropdown, unless you have any other ideas.
0
Shinu
Top achievements
Rank 2
answered on 05 Jun 2012, 10:30 AM
Hi,

I could replicate your issue of  getting '0' as the SelectedValue always, since you are setting only the SelectedItem.Text, which is the text of 5th cell(BoundColumn, i guess).If you want to compare using the Selected text, please try the following code snippet.

VB:
If Model.SelectedItem.Text = "1" Then
 
' your code
 
End If

I understood like you are binding the SelectedItem.Value with an ID(from db) and SelectedItem.Text with the text from 5th cell, then you should get the SelectedValue as the ID itself.If you want to compare with the text value, please try the following code snippet.

VB:
Private Sub myRadGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridDataItem) The
               . . .
               . . .
                                           .    .   .
           options.SelectedItem.Text = field
           options.SelectedItem.Value = DirectCast(DataBinder.Eval(e.Item.DataItem, "EmployeeID").ToString(), String' binding fron database
       End If
End Sub
 
Protected Sub save_Click(sender As Object, e As EventArgs)
    For Each ditem As GridDataItem In RadGRid1.Items
        Dim Model As DropDownList = DirectCast(ditem.FindControl("ddlModel"), DropDownList)
        Model.SelectedItem.Text = "1" Then
 
                ' your code
 
        End If
    Next
End Sub

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kevin
Top achievements
Rank 1
Share this question
or