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

Item Insert on RadGrid

1 Answer 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 16 Sep 2013, 08:13 PM
I am trying to use the insert button to insert a record into my datatable, i have the following code that i tried, but when i click on the insert button nothing happens, it just seems to refresh the page, can someone please help me out:

ususing System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.ComponentModel;
 
public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["GridData"] == null)
        {
            DataTable table = GetTable();
            Session.Add("GridData", table);
        }
        DefineGridStructure();
    }
 
 
    private void DefineGridStructure()
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = true;
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.ItemInserted += grid_ItemInserted;
         
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        boundColumn.ReadOnly = true;
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Size";
        boundColumn.HeaderText = "Size";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Description";
        boundColumn.HeaderText = "Description";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Quantity";
        boundColumn.HeaderText = "Quantity";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "DurationType";
        boundColumn.HeaderText = "DurationType";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        PlaceHolder1.Controls.Add(grid);
    }
 
    private void grid_ItemInserted(object sender, GridInsertedEventArgs e)
    {
        RadGrid grid = (RadGrid)sender;
        int rowIndex = 0;
        if (Session["GridData"] != null)
        {
            DataTable dtCurrentTable = (DataTable)Session["GridData"];
            DataRow drCurrentRow = null;
 
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
                {
 
                    GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
                    TextBox box1 = item.FindControl("Size") as TextBox;
                    TextBox box2 = item.FindControl("Description") as TextBox;
                    TextBox box3 = item.FindControl("Quantity") as TextBox;
                    TextBox box4 = item.FindControl("Unit") as TextBox;
                    TextBox box5 = item.FindControl("Duration") as TextBox;
                    TextBox box6 = item.FindControl("DurationType") as TextBox;
                    TextBox box7 = item.FindControl("Amount") as TextBox;
 
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Size"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Description"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Quantity"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["Unit"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["Duration"] = box5.Text;
                    dtCurrentTable.Rows[i - 1]["DurationType"] = box6.Text;
                    dtCurrentTable.Rows[i - 1]["Amount"] = box7.Text;
                    rowIndex++;
 
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                Session["GridData"] = dtCurrentTable;
                grid.DataSource = Session["GridData"];
                grid.Rebind();
            }
        }
    }
 
 
 
    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable current = (DataTable)Session["GridData"];
        RadGrid grid = (RadGrid)sender;
        grid.DataSource = current;
    }
 
 
 
    static DataTable GetTable()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Size", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit", typeof(string)));
        dt.Columns.Add(new DataColumn("Duration", typeof(string)));
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
        return dt;
    }
 
}

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 19 Sep 2013, 12:26 PM
Hi Brad,

In order to use AllowAutomaticInserts option for RadGrid, you will need to configure your datasource parameters accordingly:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx

Otherwise, you can use the InsertCommand event handler of the grid:
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx

Hope this helps.

Regards,
Eyup
Telerik
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 the blog feed now.
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or