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

DataBinding of a combobox inside of a Grid Using Entity Framwork

1 Answer 211 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bahram
Top achievements
Rank 2
Bahram asked on 18 Jun 2011, 07:38 AM

Hello everybody ,

I have a combobox inside of a Radgrid and want to be bound on runtime.

There is a very suit sample in Q1 2011 version of Telerik Demo under grid title(Gridà.Net3.5àEntity Framework Manual CRUD Operations) but it is very simple and there are just some common data Management about textbox.

But I tend to do that with a relational model,Suppos that we have two tables ,USERS and GENDER related with a f_key.

USER{UserID int ,Username nvarchar , GenderID int}

GENDER{GenderID int ,Longname nvarchar}

My  aspx file is like this :

  <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"

                AllowPaging="true" AllowSorting="true" OnDeleteCommand="RadGrid1_DeleteCommand"

                OnInsertCommand="RadGrid1_InsertCommand" AllowFilteringByColumn="true" OnUpdateCommand="RadGrid1_UpdateCommand"

                OnUnload="RadGrid1_Unload">

                <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="false" DataKeyNames="USERID"

                    InsertItemPageIndexAction="ShowItemOnCurrentPage">

                    <Columns>

                        <telerik:GridBoundColumn   DataField="UserID" HeaderText="UserID" UniqueName="UserID" />

                        <telerik:GridBoundColumn DataField="Username" HeaderText="Username" UniqueName="Username" />

                        <telerik:GridTemplateColumn DataField="GENDER" HeaderText="GENDER" UniqueName="GENDER" >

                            <InsertItemTemplate>

                                <telerik:RadComboBox ID="RadComboBox1" runat="server"

                                    Width="140px" />

                            </InsertItemTemplate>

                            <EditItemTemplate>

                                <telerik:RadTextBox ID="RadTComboBox1" runat="server"

                                    ReadOnly="true" Width="140px" />

                            </EditItemTemplate>

 

                    </Columns>

                </MasterTableView>

            </telerik:RadGrid>

 

 

 

How I do my CRUDoperation with this realational model with EntityFramework.

I should demonstarte that I have an edmx file with two entities:tbl_Users , tbl_Gender and I tend to do this operation in Codebehind.

Some common code is such as this,if we use only textbox :

protected void RadGrid1_Unload(object sender, EventArgs e)

    {

        if (DbContext != null)

            DbContext.Dispose();

    }

 

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)

    {

        RadGrid1.DataSource = DbContext.tbl_User.Select("it.UserID,it.Username,...");

       

    }

 

    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)

    {

        GridEditableItem item = e.Item as GridEditableItem;

        int _userId = (int)item.GetDataKeyValue("UserID");

        tbl_User _tbl_user = DbContext.tbl_User.Where(p => p.UserID == _userId).FirstOrDefault();

        item.UpdateValues(_tbl_user);

        DbContext.SaveChanges();

    }

 

    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)

    {

        GridEditableItem item = e.Item as GridEditableItem;

        Hashtable values = new Hashtable();

        item.ExtractValues(values);

        tbl_User _tbl_user = new tbl_User();

        item.UpdateValues(_tbl_user);

        DbContext.AddTotbl_User(_tbl_user);

        DbContext.SaveChanges();

    }

 

    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)

    {

        int _userId = (int)(e.Item as GridDataItem).GetDataKeyValue("UserID");

        tbl_User _tbl_user = DbContext.tbl_User.Where(p => p.USERID == _userId).FirstOrDefault();

        DbContext.DeleteObject(_tbl_user);

        DbContext.SaveChanges();

    }

 

 

1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 23 Jun 2011, 08:33 AM
Hi Bahram,

To achieve the desired functionality you could try binding the RadComboBox into the RadGrid ItemDataBound event and then extract its selected value and update entities into the UpdateCommand and InsertCommand. For example:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        // Edit mode
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            RadComboBox combo = item["GENDER"].FindControl("RadComboBox1");
            // Get the fkey for the Gender. The item.DataItem contains a data object to which the RadGrid's row is bound
            int genderID = (item.DataItem as USERS).GenderID;
 
            // Bind RadComboBox to GENRED table
            combo.DataTextField = "GenderName";
            combo.DataValueField = "GenderID";
            combo.DataSource = GENREDDATASOURCE;
            combo.DataBind();
 
            // Select combo's item with genger id equals to genderID
        }
 
        //Insert mode
        if (e.Item is GridEditFormInsertItem)
        {
            GridEditFormInsertItem item = e.Item as GridEditFormInsertItem;
            RadComboBox combo = item["GENDER"].FindControl("RadComboBox1");
            // Bind RadComboBox to GENRED table
            combo.DataTextField = "GenderName";
            combo.DataValueField = "GenderID";
            combo.DataSource = GENREDDATASOURCE;
            combo.DataBind();
        }
    }
 
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem item = e.Item as GridEditableItem;
     RadComboBox combo = item["GENDER"].FindControl("RadComboBox1");
// Get combo's selected value and update USERS table;
        ...
    }

I hope this helps.

Greetings,
Radoslav
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Bahram
Top achievements
Rank 2
Answers by
Radoslav
Telerik team
Share this question
or