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

How to retrieve control value when insert, update a row in Radgrid which set AutoGenerateColumns="True"

2 Answers 255 Views
Grid
This is a migrated thread and some comments may be shown as answers.
scvn80
Top achievements
Rank 1
scvn80 asked on 22 May 2014, 04:11 PM
Hello Telerik Teams

     I create RadGrid and set AutoGenerateColumns="True", and use EditMode="PopUp". When edit form pop up. I don't know how to get value from auto generated edit form to insert or update to Database. I google it but I don't find any solution yet. Please help me.

Thank you very much.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="True" ShowHeader="false"
            BorderWidth="1px" Skin="Office2007" Width="495" Height="100%" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnDeleteCommand="RadGrid1_DeleteCommand" OnInsertCommand="RadGrid1_InsertCommand"
            OnUpdateCommand="RadGrid1_UpdateCommand">
            <MasterTableView DataKeyNames="NV_ID" CommandItemDisplay="Top" EditMode="PopUp" Font-Names="Arial"
                ForeColor="Black">
                <CommandItemSettings AddNewRecordText="Thêm mới" ShowRefreshButton="false" />
                <NoRecordsTemplate>
                    No record</NoRecordsTemplate>
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <EditFormSettings CaptionFormatString="Edit NV_ID: {0}" CaptionDataField="NV_ID" >
                    <PopUpSettings />
                </EditFormSettings>
                <Columns>
                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton">
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {       
        var editableItem = ((GridEditableItem)e.Item);
        //create new entity          
        var item_nv = new DM_NHAN_VIEN();
 
        //item_nv.Ma_DonVi = "CNTT";
        item_nv.NV_ID = 99999;
        item_nv.Ten_DD = "test insert";
        item_nv.Ten_TCap = "T_Insert";
        item_nv.Mat_Khau = "";
        DbContext.DM_NHAN_VIENs.InsertOnSubmit(item_nv);
        try
        {
            //submit chanages to Db    
            DbContext.SubmitChanges();
        }
        catch (Exception ex)
        {
            //ShowErrorMessage();
            lblmes.Text = ex.Message;
        }
    }

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 May 2014, 05:06 AM
Hi,

When you set the AutoGenerateColumns property of RadGrid to true, the RadGrid control generates a separate column for each data field in the control's assigned datasource. Based on the datatype of the field, the control will generate:
-GridBoundColumn for string fields
-GridCheckBoxColumn for boolean fields
-GridDateTimeColumn for datetime fields
-GridNumericColumn for numeric fields

Try the following code snippet to access the controls in InsertForm:

C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridEditFormInsertItem)
    {
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
        RadNumericTextBox rntxtID = (RadNumericTextBox)insertItem["ID"].Controls[0];
        string ID = rntxtID.Text;
        TextBox txtCity = (TextBox)insertItem["City"].Controls[0];
        string City = txtCity.Text;
    }
}

Thanks,
Princy
0
scvn80
Top achievements
Rank 1
answered on 27 May 2014, 08:33 AM
I have done it. Your service is excellent.
Thank you very much.
Tags
Grid
Asked by
scvn80
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
scvn80
Top achievements
Rank 1
Share this question
or