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

Insert/Update DB from Programatically Created Grid

1 Answer 30 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 1
Kenneth asked on 12 May 2017, 03:01 PM
I'm creating a grid programatically based on the "table" a user selects from a dropdown box. I don't have the grid defined in the aspx, just a placehold for it. I want the user to be able to add a row (using the add record button) and update values (using the "edit" column). I can't find how to "wire up" getting the RadGrid1_InsertCommand and RadGrid1_UpdateCommand to be called. AllowAutomaticUpdates and AllowAutomaticInserts are false. IF I had defined the grid in the aspx page I would have included 'OnInsertCommand' and 'OnUpdateCommand', but I can't understand how to do that since I'm doing everything in the code behind. What am I missing? Any help is greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Accepted
Marin Bratanov
Telerik team
answered on 16 May 2017, 09:27 AM

Hi Kenneth,

I have just answered your support ticket with this question and I am pasting my reply here for anyone else having a similar question.


The way to add a handler to a dynamically created control is standard for all handlers for all controls: https://msdn.microsoft.com/en-us/library/ms743596(v=vs.110).aspx.

C# also offers a shortcut like this where VS generates for you the method stub when you press Tab twice after you enter the += operator:

protected void Page_Load(object sender, EventArgs e)
{
    Button btn = new Button();
    btn.Click += btn_Click;
}
 
void btn_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

So here is how you can do this with RadGrid's events that you need for manual CRUD operations:

protected void Page_Init(object sender, EventArgs e)
{
    RadGrid grd = new RadGrid();
    grd.ID = "myGrid";
    grd.NeedDataSource += grd_NeedDataSource;
    grd.UpdateCommand += grd_UpdateCommand;
    grd.InsertCommand += grd_InsertCommand;
    grd.DeleteCommand += grd_DeleteCommand;
    //add columns, properties, add the grid to its container
}
 
void grd_DeleteCommand(object sender, GridCommandEventArgs e)
{
    throw new NotImplementedException();
}
 
void grd_InsertCommand(object sender, GridCommandEventArgs e)
{
    throw new NotImplementedException();
}
 
void grd_UpdateCommand(object sender, GridCommandEventArgs e)
{
    throw new NotImplementedException();
}
 
void grd_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    throw new NotImplementedException();
}

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
Kenneth
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or