How to add a drop down list to a grid with a runtime table

4 posts, 0 answers
  1. Al
    Al avatar
    19 posts
    Member since:
    Oct 2012

    Posted 01 Feb 2010 Link to this post

    I may be just overlooking something in the examples but how do you programatically make a column in the grid be a drop down list when the datasource is a runtime only table.

    What I want is a 2 column unbound grid with 20 rows and the first column is a drop down list with all of the field names from a sql table.
    What I'm doing is creating a in memory datatable with 2 columns and 20 blank rows and making this the datasource for a RadGrid.
    Then I want to make the first column of the grid be a drop down list with the fieldnames from an sql table.
    The grid should start out in edit mode and then save the users entries for both columns in the underlying datatable so that I can loop through the datatable and use the fieldnames and value pairs for an update string.

    This seems straight forward but I haven't been able to make it work yet.

    Any help would be greatly appreciated.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Feb 2010 Link to this post

    Hi,

    A suggestion would be to use a GridTemplateColumn to achieve the desired scenario. Check out the code snippet and the design view and let me know if this helps.

    ASPX:
     <telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid2" AllowMultiRowEdit="true"  runat="server" 
                GridLines="None"  OnPreRender="RadGrid2_PreRender"
           <MasterTableView EditMode="InPlace" > 
                    <Columns> 
                        <telerik:GridTemplateColumn> 
                            <EditItemTemplate> 
                                <asp:DropDownList ID="DropDownList1" runat="server"
                                    <asp:ListItem>Name</asp:ListItem> 
                                    <asp:ListItem>Place</asp:ListItem> 
                                    <asp:ListItem>Age</asp:ListItem> 
                                </asp:DropDownList> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn> 
                            <EditItemTemplate> 
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
          </MasterTableView> 


    C#
        protected void RadGrid1_PreRender(object sender, EventArgs e) 
            { 
                foreach (GridDataItem dataItem in RadGrid1.Items) 
                { 
                    dataItem.Edit = true
     
                } 
                RadGrid1.Rebind(); 
            } 
     
            protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
            { 
                DataTable dt = new DataTable(); 
                dt.Columns.Add("Col1"); 
                dt.Columns.Add("Col2"); 
                DataRow row = dt.NewRow(); 
                row[0] = " "
                row[1] = " "
                dt.Rows.Add(row); 
                DataRow row1 = dt.NewRow(); 
                row[0] = " "
                row[1] = " "
                dt.Rows.Add(row1); 
                DataRow row2 = dt.NewRow(); 
                row[0] = " "
                row[1] = " "
                dt.Rows.Add(row2); 
                DataRow row3 = dt.NewRow(); 
                row[0] = " "
                row[1] = " "
                dt.Rows.Add(row3); 
                RadGrid1.DataSource = dt; 
            } 
     

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Al
    Al avatar
    19 posts
    Member since:
    Oct 2012

    Posted 02 Feb 2010 Link to this post

    Thanks for the help,

     

        Using what you gave me and some code that I already had found I can now get the grid to display the 2 columns for a dynamic table and have one of the columns be a drop down list.

     

       The problem I'm still having is that I don't know how to save the user changes in the grid to the in memory datatable that is connected as the datasource for the grid.  The table has blank rows until the user makes a change in the grid the grid is always in edit mode, but then I want the user to click a button that sends the changes back to the in memory datatable so that I can then loop through the datatable to get the user entered data.

  5. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 05 Feb 2010 Link to this post

    Hi Allen,

    Please, refer to the following online resources, which elaborate on this subject for more information about how to achieve the required functionality:
    http://www.telerik.com/help/aspnet-ajax/grdinsertupdatedeleteatdatabaselevel.html
    http://www.telerik.com/community/code-library/aspnet-ajax/grid/update-insert-delete-through-queries-at-database-level.aspx

    Regards,
    Pavlina
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
Back to Top