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

insert records from NoRecords template

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 04 May 2017, 02:44 PM

Hi, I'm trying to insert records into my database from code behind in my itemcommand.

I have the form setup in the  No Records Template in a nested RADGrid, but the GridNoRecordsItem does not have a Controls collection.  What am I missing?

Here is some of the pertinent code:

 

        protected void RadGrid2_ItemCommand1(object sender, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "InsertSubGoal":
                    string GoalID = string.Empty;
                    string SubGoal = string.Empty;
                    if (e.CommandArgument.ToString() == "Empty")
                    {
                        var insertedItem = (GridNoRecordsItem)e.Item;
                        GoalID = insertedItem.OwnerTableView.ParentItem.GetDataKeyValue("GoalID").ToString();
                        SubGoal = (insertedItem["SubGoal"].Controls[0] as TextBox).Text;

any help or sample code would be appreciated.

Thanks

Tom

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 09 May 2017, 11:34 AM

Hi Tom,

The NoRecordsTemplate is just to show a message to the user.

If you want to insert a record, the grid must be in insert mode. You can do that by setting the IsItemInserted property of the master table view to true if there is no data. Here is a basic example

<telerik:RadGrid runat="server" ID="rg1" OnNeedDataSource="rg1_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="ID"></telerik:GridBoundColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    some item template
                </ItemTemplate>
                <InsertItemTemplate>
                    insert item template here
                </InsertItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

protected DataTable GetGridData()
{
    DataTable tbl = new DataTable();
    tbl.Columns.Add(new DataColumn("someNumber", typeof(decimal)));
    tbl.Columns.Add(new DataColumn("name", typeof(string)));
    tbl.Columns.Add(new DataColumn("ID", typeof(int)));
    tbl.Columns.Add(new DataColumn("someColumn", typeof(string)));
    tbl.Rows.Add(new object[] { 123456789.123456789, "=cmd|'calc.exe'", 2, "5" });
    tbl.Rows.Add(new object[] { 123456789.567891234, "two", 3, null/*SIMULATE EMPTY VALUE*/ });
    tbl.Rows.Add(new object[] { -123456789.123456789, "three", 4, "5" });
    tbl.Rows.Add(new object[] { -123456789.567891234, "four", 5, "5" });
 
    return tbl;
}
 
protected void rg1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    //get data here
    DataTable ds = GetGridData();
    (sender as RadGrid).DataSource= ds;//will autogenerate columsn by default, as there is data now
 
    //clear it to simulate no data
    ds.Rows.Clear();
    if(ds.Rows.Count == 0)//check for lack of data according to your data access layer logic
    {
        (sender as RadGrid).MasterTableView.IsItemInserted = true;
    }
}


Regards,

Marin Bratanov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Tom
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or