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

Radgrid edit templates problem

5 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fusion Outsourcing Software Pvt. Ltd.
Top achievements
Rank 1
Fusion Outsourcing Software Pvt. Ltd. asked on 05 Jun 2009, 10:47 AM
Hello Telerik team,

I am having a grid with few columns bound to it. I have set datasource for grid in code behind. Also i want to insert/update & delete in code behind.
Please refer the .aspx & .aspx.cs code page below.
ASPX Page.
<telerik:RadGrid ID="Grid_products" AllowPaging="true" PageSize="25" Skin="Vista" runat="server"  
            OnNeedDataSource="Grid_products_NeedDataSource" Width="100%" AutoGenerateColumns="false" HorizontalAlign="NotSet"  
            OnUpdateCommand="Grid_products_UpdateCommand" OnInsertCommand="Grid_products_InsertCommand" OnDeleteCommand="Grid_products_DeleteCommand"
                
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                
                <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="ProductID" EditMode ="InPlace"
                
                <Columns> 
                
                <telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderText="Edit" UniqueName="EditCommandColumn"
                        <ItemStyle CssClass="MyImageButton" /> 
                    </telerik:GridEditCommandColumn> 
                
                    <telerik:GridBoundColumn UniqueName="ProductName" HeaderText="Product Name" DataField="ProductName" ColumnEditorID = "Grid_ProductsTextBox_ProductName"
                    </telerik:GridBoundColumn> 
                     
                     
                     
                     
                    <telerik:GridBoundColumn UniqueName="RetailPrice" HeaderText="Retail Price" DataField="RetailPrice" ColumnEditorID = "Grid_ProductsTextBox_RetailPrice"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="WholesalePrice" HeaderText="WholesalePrice Name" DataField="WholesalePrice" ColumnEditorID = "Grid_ProductsTextBox_WholesalePrice"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="CustomerPrice" HeaderText="CustomerPrice Name" DataField="CustomerPrice" ColumnEditorID = "Grid_ProductsTextBox_CustomerPrice"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" DataField="Description" ColumnEditorID = "Grid_ProductsTextBox_Description"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="UsageTime" HeaderText="Usage Time" DataField="UsageTime" ColumnEditorID = "Grid_ProductsTextBox_UsageTime"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="FollowupTime" HeaderText="Followup Time" DataField="FollowupTime" ColumnEditorID = "Grid_ProductsTextBox_FollowupTime"
                    </telerik:GridBoundColumn> 
                     
                    <telerik:GridBoundColumn UniqueName="ReminderTime" HeaderText="Reminder Time" DataField="ReminderTime" ColumnEditorID = "Grid_ProductsTextBox_ReminderTime"
                    </telerik:GridBoundColumn> 
                     
                     
                     
                
                    <telerik:GridButtonColumn HeaderText="Delete" ConfirmText="Delete this product?" ConfirmDialogType="RadWindow" 
                    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" 
                    UniqueName="DeleteColumn"
                
                    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                
                </telerik:GridButtonColumn> 
                 
                </Columns> 
                 
                <ExpandCollapseColumn Visible="False"
                    <HeaderStyle Width="10px" /> 
                </ExpandCollapseColumn> 
                 
                <RowIndicatorColumn Visible="False"
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                 
                </MasterTableView> 
                <ClientSettings> 
                    <ClientEvents OnRowDblClick="RowDblClick" OnGridCreated="GridCreated" 
                        OnCommand="GridCommand" /> 
                </ClientSettings> 
            </telerik:RadGrid> 
            <telerik:GridTextBoxColumnEditor ID="Grid_ProductsTextBox_ProductName"  
                runat="server"/> 
                <telerik:GridTextBoxColumnEditor ID="Grid_ProductsTextBox_Description"  
                runat="server"/> 
                
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_RetailPrice" runat="server"
               <NumericTextBox ID="NumericTextBox1" runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_WholesalePrice" runat="server"
               <NumericTextBox ID="NumericTextBox2" runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_CustomerPrice" runat="server"
               <NumericTextBox runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="false" AllowRounding="true" DecimalDigits="2" /> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 
                
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_UsageTime" runat="server"
               <NumericTextBox ID="NumericTextBox3" runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_FollowupTime" runat="server"
               <NumericTextBox ID="NumericTextBox4" runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 
               <telerik:GridNumericColumnEditor ID="Grid_ProductsTextBox_ReminderTime" runat="server"
               <NumericTextBox ID="NumericTextBox5" runat="server" Type="Number"
               <NumberFormat KeepNotRoundedValue="true" AllowRounding="false"/> 
               </NumericTextBox> 
               </telerik:GridNumericColumnEditor> 


ASPX.CS page

 #region Products Grid 
 
    protected void Grid_products_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        Grid_products.DataSource = GetDataTable(); 
    } 
    //string connectionString = WebConfigurationManager.ConnectionStrings["SchedulerConnectionString"].ConnectionString; 
    private DataTable GetDataTable() 
    { 
        DataTable datatable; 
        if (txt_productsearch.Text.Trim() != ""
        { 
            string selectSQL = "SELECT [ProductID], [ProductName], [RetailPrice], [WholesalePrice], [CustomerPrice], [Description],[UsageTime],[FollowupTime],[ReminderTime] FROM [Products] where ProductName LIKE '%" + txt_productsearch.Text + "%'  and AdminID =1"
            //string selectSQL = "SELECT ProductID,ProductName FROM [Products] Where ProductName LIKE '%" + txt_productsearch.Text + "%'  and AdminID =1"; 
            SqlConnection con = new SqlConnection(connectionString); 
            SqlCommand cmd = new SqlCommand(selectSQL, con); 
            //SqlDataReader reader; 
            SqlDataAdapter da = new SqlDataAdapter(selectSQL, connectionString); 
            datatable = new DataTable(); 
            //da.Fill(datatable); 
 
            //OleDbConnection connection1 = new OleDbConnection(); 
            //OleDbDataAdapter adapter1 = new OleDbDataAdapter(); 
            //adapter1.SelectCommand = new OleDbCommand(query, connection1); 
            //DataTable table1 = new DataTable(); 
            //connection1.Open(); 
            try 
            { 
                //adapter1.Fill(table1); 
                da.Fill(datatable); 
            } 
            finally 
            { 
                //connection1.Close(); 
            } 
 
        } 
        else 
        { 
            string selectSQL = "SELECT [ProductID], [ProductName], [RetailPrice], [WholesalePrice], [CustomerPrice], [Description],[UsageTime],[FollowupTime],[ReminderTime] FROM [Products] Where ProductName= ' ' and AdminID =1"
            SqlConnection con = new SqlConnection(connectionString); 
            SqlCommand cmd = new SqlCommand(selectSQL, con); 
            //SqlDataReader reader; 
            SqlDataAdapter da = new SqlDataAdapter(selectSQL, connectionString); 
            datatable = new DataTable(); 
            da.Fill(datatable); 
 
        } 
        return datatable; 
    } 
    protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e) 
    { 
        if (e.Argument.IndexOf("FilterGrid") != -1) 
        { 
            Grid_products.Rebind(); 
        } 
    } 
    protected void Grid_products_InsertCommand(object source, GridCommandEventArgs e) 
    { 
        GridEditableItem editedItem = e.Item as GridEditableItem; 
        //(editedItem.EditManager.GetColumnEditor("") as GridNumericColumnEditor). 
        //GridTextBoxColumnEditor itemupdate = e.Item.DataItem as GridTextBoxColumnEditor; 
        //Get the primary key value using the DataKeyValue. 
        //int ProductID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProductID"].ToString()); 
        string ProductName = (editedItem.EditManager.GetColumnEditor("ProductName"as GridTextBoxColumnEditor).Text; 
        string RetailPrice = (editedItem.EditManager.GetColumnEditor("RetailPrice"as GridNumericColumnEditor).Text; 
        string WholesalePrice = (editedItem.EditManager.GetColumnEditor("WholesalePrice"as GridNumericColumnEditor).Text; 
        string CustomerPrice = (editedItem.EditManager.GetColumnEditor("CustomerPrice"as GridNumericColumnEditor).Text; 
        string Description = (editedItem.EditManager.GetColumnEditor("Description"as GridTextBoxColumnEditor).Text; 
        string UsageTime = (editedItem.EditManager.GetColumnEditor("UsageTime"as GridNumericColumnEditor).Text; 
        string FollowupTime = (editedItem.EditManager.GetColumnEditor("FollowupTime"as GridNumericColumnEditor).Text; 
        string ReminderTime = (editedItem.EditManager.GetColumnEditor("ReminderTime"as GridNumericColumnEditor).Text; 
        //string RetailPrice = (editedItem.EditManager.GetColumnEditor("RetailPrice") as GridNumericColumnEditor).Text; 
         
        //string FirstName = (itemupdate.FindControl("Grid_contactsTextBox_FirstName") as GridTextBoxColumnEditor).Text; 
        string InsertSQL = "Insert into Products(ProductName,RetailPrice,WholesalePrice,CustomerPrice,Description,UsageTime,FollowupTime,ReminderTime,AdminID) Values('" + ProductName +","+ Convert.ToInt32(RetailPrice) + "," + Convert.ToInt32(WholesalePrice) + "," + Convert.ToInt32(CustomerPrice) + "," + Description + "," + Convert.ToInt32(UsageTime) + "," + Convert.ToInt32(FollowupTime) + "," + Convert.ToInt32(ReminderTime) + "',1)"
        SqlConnection con = new SqlConnection(connectionString); 
        SqlCommand cmd = new SqlCommand(InsertSQL, con); 
        int flag; 
        try 
        { 
            con.Open(); 
            flag = cmd.ExecuteNonQuery(); 
        } 
        catch (Exception Error) 
        { 
 
        } 
        finally 
        { 
            con.Close(); 
        } 
 
    } 
    protected void Grid_products_DeleteCommand(object source, GridCommandEventArgs e) 
    { 
 
    } 
    protected void Grid_products_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        GridEditableItem editedItem = e.Item as GridEditableItem; 
        //GridTextBoxColumnEditor itemupdate = e.Item.DataItem as GridTextBoxColumnEditor; 
        //Get the primary key value using the DataKeyValue. 
        int ProductID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProductID"].ToString()); 
        string ProductName = (editedItem.EditManager.GetColumnEditor("ProductName"as GridTextBoxColumnEditor).Text; 
        //string FirstName = (itemupdate.FindControl("Grid_contactsTextBox_FirstName") as GridTextBoxColumnEditor).Text; 
        string UpdateSQL = "Update Products set ProductName='" + ProductName + "' where ProductID =" + ProductID + "  where AdminID =1"
        SqlConnection con = new SqlConnection(connectionString); 
        SqlCommand cmd = new SqlCommand(UpdateSQL, con); 
        int flag; 
        try 
        { 
            con.Open(); 
            flag = cmd.ExecuteNonQuery(); 
        } 
        catch (Exception Error) 
        { 
 
        } 
        finally 
        { 
            con.Close(); 
        } 
    } 
    #endregion 



My problem is that:
1)  I am not able to insert any new record. when i click on add new record it goes to "Grid_products_InsertCommand" event. There it takes the value of "ProductName" but when it try to execute for getting "RetailPrice" it throws an exception "Object reference not set to an instance of an object.". Its just taking "ProductName" and for rest of them its showing the same exception. I dont know what the problem is with it.

2) I am not able to validate these text boxes used in grid for insert/update. There are 2 types of GridColumnEditor that i hav used. they are
a) Textbox.
b) Numeric.
It would be helpful if i could have a sample application that validates both.
Please help me as soon as possible.
Its kind of very urgent.


Thank You,
Chinmay Sharma.

5 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 10 Jun 2009, 12:49 PM
Hi Fusion Outsourcing Software Pvt. Ltd.,

Please note that if you are using only GridBoundColumns for your data fields, the only editor you would get when editing and inserting items would be the GridTextBoxColumnEditor. This editor type cannot be cast to the other editor types you are specifying.

Please use GridNumericColumn with numeric column editors, GridDateTimeColumn with date time editors, GridCheckBoxColumn with GridCheckBoxColumnEditor, etc.

All the best,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Brady
Top achievements
Rank 1
answered on 26 Oct 2009, 07:51 PM
Hi Veli
I was trying for hours to get my grid working, following Yavor's example here, when I found your answer here, and suddenly, hey, the magic is back.

If you look at the code in Yavor's example, he uses a GridBoundColumn  with a GridNumericColumnEditor.  This gave me no clue that a specific column type was required, so I have been pulling my hair out all night.

I am really enjoying learning all about your suite, but sometimes discovery must come after good advice.

Cheers
Brady
0
Veli
Telerik team
answered on 27 Oct 2009, 08:51 AM
Hello Brady,

Maybe Yavor had something different on his mind. When using column editors, the column editor type must match the one required by the column type itself.

Anyway, I am  glad things worked out for you. Good luck with your project!

Greetings,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Corina
Top achievements
Rank 1
answered on 31 Oct 2011, 01:44 PM
Hi

What kind of editor should be used with a GridTemplateColumn?

I need a link in one of the columns that has the nice value shown to the user, then the href link in background for when they click. What do I attach to make a textbox show under this column for the user to input their link value.

<telerik:GridTemplateColumn DataField="cssFile" HeaderText="CSS File" AllowFiltering="true" UniqueName="cssFile" SortExpression="cssFile" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false">
                                <ItemTemplate>
                                    <a class="normal" href="<%#DataBinder.Eval(Container.DataItem, "cssLink")%>"><%#DataBinder.Eval(Container.DataItem, "cssFile")%></a>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
0
Sebastian
Telerik team
answered on 02 Nov 2011, 03:33 PM
Hi Corina,

For this purpose you can specify EditTemplate for your template column and place asp TextBox or RadTextBox inside this template. Basically, the same configuration you will use for a template column in a standard MS GridView control.

Greetings,
Sebastian
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Fusion Outsourcing Software Pvt. Ltd.
Top achievements
Rank 1
Answers by
Veli
Telerik team
Brady
Top achievements
Rank 1
Corina
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or