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();
}