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

FIll Dropdownlist on editForm

4 Answers 160 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 05 Apr 2012, 03:42 PM
I am trying to fill a drop down list witht he value of the textbox from the radgrid.  I tire a previous example sent to me but keep getting Object not set to reference.

Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If TypeOf e.Item Is GridDataItem Then
           Dim gr As DropDownList = CType(e.Item.FindControl("ddlRank"), DropDownList)
           Dim rank As String = e.Item.Cells(6).Text
           sql = "Select * from OPENQUERY(SIDPERS, 'Select Distinct PAY_GR, PAY_GR GR from PERS_GRABBR_CODE_TBL ORDER BY PAY_GR')"
           myDataTable = New DataTable
           myDataTable = getData(sql)
           gr.DataSource = myDataTable
           gr.DataTextField = "GR"
           gr.DataValueField = "PAY_GR"
           gr.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, rank).ToString(), String)
           gr.DataBind()
       End If
   End Sub

<EditFormSettings EditFormType="Template">
                                       <FormTemplate>
                                             <table width="100%">
                                               <tr>
                                                   <td style="width:50%" align="right">Prior Uic: </td>
                                                   <td style="width:50%" align="left"><asp:TextBox ID="txtPriorUic" runat="server" Width="100px" Text='<%# Bind("strPrevUic") %>'></asp:TextBox></td>
                                               </tr>
                                               <tr>
                                                   <td style="height:5px"></td>
                                               </tr>
                                               <tr>
                                                   <td style="width:50%" align="right">Para: </td>
                                                   <td style="width:50%" align="left""><asp:TextBox ID="txtpara" runat="server" Width="100px" Text='<%# Bind("strPara") %>'></asp:TextBox></td>
                                               </tr>
                                               <tr>
                                                   <td style="height:5px"></td>
                                               </tr>
                                               <tr>
                                                   <td style="width:50%" align="right"> Line: </td>
                                                   <td style="width:50%" align="left"><asp:TextBox ID="txtLine" runat="server" Width="100px" Text='<%# Bind("strLine") %>'></asp:TextBox></td>
                                               </tr>
                                               <tr>
                                                   <td style="height:5px"></td>
                                               </tr>
                                               <tr>
                                                   <td style="width:50%" align="right">Return Grade: </td>
                                                   <td style="width:50%" align="left"><asp:DropDownList ID="ddlRank" runat="server" Width="105px"> </asp:DropDownList>
                                                   </td>
                                               </tr>
                                           </table>
                                           <table width="100%">
                                                <tr>
                                                    <td style="width:50%" align="right"></td>
                                                    <td style="width:50%" align="left">
                                                       <asp:LinkButton ID="lnkSubmit" runat="server" text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
                                                       CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'></asp:LinkButton>
                                                             
                                                       <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                                   </td>
                                               </tr>
                                           </table>
                                       </FormTemplate>
                                   </EditFormSettings>



4 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 05 Apr 2012, 03:54 PM

Hi,
Ok put int his and now I do not get and error anymore but my DropDownList also does not even populate.

Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If TypeOf e.Item Is GridEditFormItem And e.Item.IsInEditMode Then
           If TypeOf e.Item Is GridDataItem Then
               Dim gr As DropDownList = CType(e.Item.FindControl("ddlRank"), DropDownList)
               Dim rank As String = e.Item.Cells(6).Text
               sql = "Select PAY_GR, GR from OPENQUERY(SIDPERS, 'Select Distinct PAY_GR, PAY_GR GR from PERS_GRABBR_CODE_TBL ORDER BY PAY_GR')"
               myDataTable = New DataTable
               myDataTable = getData(sql)
               gr.DataSource = myDataTable
               gr.DataValueField = "PAY_GR"
               gr.DataTextField = "GR"
               gr.SelectedValue = rank
               gr.DataBind()
           End If
       End If
   End Sub
0
Princy
Top achievements
Rank 2
answered on 09 Apr 2012, 06:06 AM
Hello Kevin,

Try accessing the dropdownlist in edit mode and bind the selected value as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then
Dim ddl As DropDownList = DirectCast(e.Item.FindControl("ddlRank"), DropDownList)
        Dim updateQuery As String = "SELECT FirstName FROM [Employees]"
        SqlCommand.CommandText = updateQuery
        SqlCommand.Connection = conn
        Dim da As New SqlDataAdapter(SqlCommand)
        Dim ds As New DataSet()
        da.Fill(ds)
        ddl.DataSource = ds
        ddl.DataTextField = "FirstName"
        ddl.DataValueField = "FirstName"
        ddl.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "FirstName").ToString(), String)
        ddl.DataBind()
    End If
End Sub

Thanks,
Princy.
0
Kevin
Top achievements
Rank 1
answered on 10 Apr 2012, 02:20 PM
HI,

I have tried this before to no avial , i does not seem to work for me, i always end up with no errors but a blank dropdownlist.
Protected Sub myRadGrid_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles myRadGrid.ItemDataBound
       If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then
           If TypeOf e.Item Is GridDataItem Then
               Dim gr As DropDownList = DirectCast(e.Item.FindControl("ddlRank"), DropDownList)
               Dim rank As String = e.Item.Cells(6).Text
               sql = "Select PAY_GR, GR from OPENQUERY(SIDPERS, 'Select Distinct PAY_GR, PAY_GR GR from PERS_GRABBR_CODE_TBL ORDER BY PAY_GR')"
               myDataTable = New DataTable
               myDataTable = getData(sql)
               gr.DataSource = myDataTable
               gr.DataValueField = "PAY_GR"
               gr.DataTextField = "GR"
               gr.SelectedValue = DirectCast(DataBinder.Eval(e.Item.DataItem, "GR").ToString(), String)
               gr.DataBind()
           End If
       End If
   End Sub

<telerik:RadGrid ID="myRadGrid" runat="server" Width="75%" BorderWidth="1px" CellPadding="6" GridLines="None" BorderColor="#404040" Skin="Web20">
                      <ExportSettings ExportOnlyData="true" FileName="DonorSlots" IgnorePaging="true" OpenInNewWindow="true"></ExportSettings>
                      <MasterTableView AutoGenerateColumns="false" DataKeyNames="intDeployId" Name="MasterGrid" BorderColor="#404040" Font-Size="9" Font-Names="Veranda,arial,sans-serif" AllowPaging="True" 
                      PageSize="20" PagerStyle-Mode="NumericPages" HeaderStyle-HorizontalAlign="Center" GridLines="Both" BorderWidth="1px" CommandItemDisplay="Top" EditMode="EditForms">
                      <CommandItemSettings ShowAddNewRecordButton="false" ShowRefreshButton="false" ShowExportToExcelButton="false" /><AlternatingItemStyle BackColor="#B0C4DE"  HorizontalAlign="Center" />
                      <ItemStyle HorizontalAlign="Center" /><HeaderStyle ForeColor="White" Font-Bold="true" BorderColor="#404040" BorderWidth="1px" />
                          <CommandItemTemplate>
                              <table width="100%">
                                  <tr>
                                      <td align="right"><asp:Button ID="btnExport" runat="server" CommandName="ExportToExcel" Text="Export Excel" /></td>
                                  </tr>
                              </table>
                          </CommandItemTemplate>
                          <Columns>
                              <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" EditText="Update"></telerik:GridEditCommandColumn>
                              <telerik:GridTemplateColumn HeaderText="HIST">
                                  <ItemTemplate>
                                          <asp:Image ID="imgLooker" runat="server" ImageUrl="~/Images/ViewResources.gif" ImageAlign="Middle" BorderStyle="None" />
                                          <asp:PopupControlExtender ID="popInfo" runat="server" PopupControlID="pnlPop" TargetControlID="imgLooker" DynamicContextKey='<% #Eval("strSSN") %>' 
                                          DynamicControlID="pnlpop" DynamicServiceMethod="HelloWorld" Position="bottom"></asp:PopupControlExtender>
                                  </ItemTemplate>
                              </telerik:GridTemplateColumn>
                              <telerik:GridBoundColumn DataField="strFullname" HeaderText="NAME" />
                              <telerik:GridBoundColumn DataField="strPrevUic" HeaderText="PRIOR UIC" />
                              <telerik:GridBoundColumn DataField="strPara" HeaderText="PARA" />
                              <telerik:GridBoundColumn DataField="strLine" HeaderText="LINE" />
                              <telerik:GridBoundColumn DataField="strGrade" HeaderText="GRADE" />
                          </Columns>
                          <EditFormSettings EditFormType="Template">
                              <FormTemplate>
                                      <table width="100%">
                                      <tr>
                                          <td style="width:50%" align="right">Prior Uic: </td>
                                          <td style="width:50%" align="left"><asp:TextBox ID="txtPriorUic" runat="server" Width="100px" Text='<%# Bind("strPrevUic") %>'></asp:TextBox></td>
                                      </tr>
                                      <tr>
                                          <td style="height:5px"></td>
                                      </tr>
                                      <tr>
                                          <td style="width:50%" align="right">Para: </td>
                                          <td style="width:50%" align="left""><asp:TextBox ID="txtpara" runat="server" Width="100px" Text='<%# Bind("strPara") %>'></asp:TextBox></td>
                                      </tr>
                                      <tr>
                                          <td style="height:5px"></td>
                                      </tr>
                                      <tr>
                                          <td style="width:50%" align="right"> Line: </td>
                                          <td style="width:50%" align="left"><asp:TextBox ID="txtLine" runat="server" Width="100px" Text='<%# Bind("strLine") %>'></asp:TextBox></td>
                                      </tr>
                                      <tr>
                                          <td style="height:5px"></td>
                                      </tr>
                                      <tr>
                                          <td style="width:50%" align="right">Return Grade: </td>
                                          <td style="width:50%" align="left"><asp:DropDownList ID="ddlRank" runat="server" CausesValidation="false" Width="105px"></asp:DropDownList>
                                          </td>
                                      </tr>
                                  </table>
                                  <table width="100%">
                                          <tr>
                                              <td style="width:50%" align="right"></td>
                                              <td style="width:50%" align="left">
                                              <asp:LinkButton ID="lnkSubmit" runat="server" text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
                                              CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'></asp:LinkButton>
                                                    
                                              <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                          </td>
                                      </tr>
                                  </table>
                              </FormTemplate>
                          </EditFormSettings>
                      </MasterTableView>
                  </telerik:RadGrid>


Here is our getdata Fucntion as well.
Public Shared Function getData(ByVal sqlString As String) As DataTable
       Dim conn As New SqlConnection(CreateConnectString("??", "??"))
       Dim cmd As New SqlCommand(sqlString, conn)
       Dim ds As New DataSet
       Dim da As New SqlDataAdapter
       da.SelectCommand = cmd
       da.Fill(ds)
       conn.Close()
       If ds.Tables.Count > 0 Then
           getData = ds.Tables(0)
       Else
           getData = Nothing
       End If
   End Function


0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Apr 2012, 10:08 AM
Hello Kevin,

The above code is working as expected in my end. Since the dropdownlist is in formtemplate, access it using GridEditableItem instead of GridDataItem.
C#:
If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then

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