I am using telerik rad grid. I want to have one more radgrid inside Radgrid MAstertableview edit form.
Inside grid is having Boundcolumn,Templatecolumn containing label as item template and combobox as edit item template.
Now how to do add, edit delete operation to inside radgrid.
I searched lot of articles. But didnt find example like this scenario.
Please help me.
Regards,
Soumya,
Bangalore.
14 Answers, 1 is accepted
I have prepared a sample code based on your requirement. You can use FormTemplate for the purpose. And in this example I am accesssing controls inside edit form in UpdateCommand and can update from code. You can apply the same logic for insert and delete operations.
aspx:
<
telerik:RadGrid
ID
=
"grid1"
AutoGenerateColumns
=
"false"
runat
=
"server"
DataSourceID
=
"SqlDataSource1"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridEditCommandColumn
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
telerik:RadGrid
runat
=
"server"
ID
=
"grid2"
AutoGenerateEditColumn
=
"true"
DataSourceID
=
"SqlDataSource1"
AutoGenerateColumns
=
"false"
OnUpdateCommand
=
"grid2_UpdateCommand"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"EmployeeID"
UniqueName
=
"EmployeeID"
></
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
FormTemplate
>
</
EditFormSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
protected
void
grid2_UpdateCommand(
object
sender, Telerik.Web.UI.GridCommandEventArgs e)
{
GridEditableItem item = (GridEditableItem)e.Item;
TextBox txt = (TextBox)item[
"EmployeeID"
].Controls[0];
// accessing values
// your code to Update
}
Thanks,
Princy.
Thanks for replying.
This is the source code of radgrid which am using inside Mastertablevieweditform.But i want to perform add, Edit and delete operation to that grid. Please help me. Am very newer to telrik controls.
I set mastertableview property to (EditMode="inplace" CommandItemDisplay="Top").
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
CellSpacing="0" GridLines="None"
>
<ClientSettings>
<Selecting CellSelectionMode="None" />
</ClientSettings>
<MasterTableView EditMode="inplace" CommandItemDisplay="Top">
<CommandItemSettings ExportToPdfText="Export to PDF" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
Visible="True">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
Visible="True">
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn UniqueName="EmpName" ReadOnly="True" HeaderText="ContactName"
DataField="EmpName" FilterControlAltText="Filter EmpName column" />
<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Country">
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox1" Runat="server" Height="22px"
onselectedindexchanged="RadComboBox1_SelectedIndexChanged" Width="154px">
</telerik:RadComboBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmpCity") %>'>
</asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column"
HeaderText="Edit">
</telerik:GridEditCommandColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableImageSprites="False">
</FilterMenu>
</telerik:RadGrid>
You can perform insert,update operations manuaaly using the following code.
C#:
public
static
string
connection = WebConfigurationManager.ConnectionStrings[
"NorthwindConnectionString3"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(connection);
public
SqlCommand SqlCommand =
new
SqlCommand();
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
string
selectQuery =
"select * from Orders"
;
SqlDataAdapter adapter =
new
SqlDataAdapter(selectQuery,conn);
DataTable dt =
new
DataTable();
conn.Open();
adapter.Fill(dt);
conn.Close();
RadGrid1.DataSource = dt;
}
protected
void
RadGrid1_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
conn.Open();
GridEditableItem editItem = (GridEditableItem)e.Item;
string
name= editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex][
"EmpName"
].ToString();
stringname = (editItem[
"EmpName"
].Controls[0]
as
TextBox).Text;
string
updateQuery =
"Update Orders set EmployeeID='"
+ EmployeeID +
"' where name='"
+ EmpName +
"'"
;
SqlCommand.CommandText = updateQuery;
SqlCommand.Connection = conn;
SqlCommand.ExecuteNonQuery();
conn.Close();
}
protected
void
RadGrid1_DeleteCommand(
object
sender, GridCommandEventArgs e)
{
conn.Open();
GridDataItem editItem = (GridDataItem)e.Item;
string
name= editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex][
"EmpName"
].ToString();
string
delQuery=
"delete from orders where EmpName='"
+name+
"'"
;
SqlCommand.CommandText = delQuery;
SqlCommand.Connection = conn;
SqlCommand.ExecuteNonQuery();
conn.Close();
}
protected
void
RadGrid1_InsertCommand(
object
sender, GridCommandEventArgs e)
{
conn.Open();
GridEditableItem editItem = (GridEditableItem)e.Item;
string
name = (editItem[
"EmpName"
].Controls[0]
as
TextBox).Text;
string
insertQuery =
"insert into Orders (EmpName) values ('"
+name+
"')"
;
SqlCommand.CommandText = insertQuery;
SqlCommand.Connection = conn;
SqlCommand.ExecuteNonQuery();
conn.Close();
}
}
Grid / Automatic Operations
Thanks,
Princy.
I resolved this problem.
Now one more question i want ask is,
In my grid column am using label as ItemTemplate and Combobox as EditItemTemplate like below.
<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column"
HeaderText="District" UniqueName="TemplateColumn">
<EditItemTemplate>
<telerik:RadComboBox ID="cmbDistrict" Runat="server"
AutoPostBack="True" Width="114px"
onselectedindexchanged="cmbDistrict_SelectedIndexChanged"
DataTextField="DistrictName" DataValueField="DistrictId">
</telerik:RadComboBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDistrict" runat="server" Text='<%# Bind("DistrictName") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</telerik:GridTemplateColumn>
Here in Edit click am binding combobox from server side using ItemDataBound.
After binding want to select LabelValue as selected value in combobox.
But problem is not able to find ItemTemplate Label in edit mode.
How can i do this?
Please help me.
Thanks & Regards,
Soumya,
Bangalore
Try the following code snippet to access the label in ItemDataBound event.
C#:
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem)
{
GridEditableItem item = (GridEditableItem)e.Item ;
Label lbl = (Label)item.FindControl(
"lblDistrict"
) ;
}
}
Thanks,
Princy.
I tried with this. but it didnt take value in run mode.
Later i used bound column and disabled it in run mode.
while editing also instead of showing value from label to combobox, i took it from bound column.
GridEditableItem item = (GridEditableItem)e.Item;
TextBox strDistrictId = (TextBox)item["DistrictId"].Controls[0];
Now its working fine.
Thanks & Regards,
Soumya,
Bangalore.
Hello,
I have a Radgrid inside Radgrid MasterTableViewEditForm and I'm trying
to display information based on the selected value of radgrid2 (the inside grid) right next to it.
I tried a lot of the demos but I can't get it to work, please help me. Here's what I have...
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
telerik:RadTabStrip
ID
=
"Contacts"
runat
=
"server"
SelectedIndex
=
"0"
MultiPageID
=
"UserMultiPage1"
>
<
Tabs
>
<
telerik:RadTab
runat
=
"server"
Text
=
"Applications"
PageViewID
=
"AddressInfoPageView"
>
</
telerik:RadTab
>
</
Tabs
>
</
telerik:RadTabStrip
>
<
telerik:RadMultiPage
runat
=
"server"
ID
=
"UserMultiPage1"
SelectedIndex
=
"0"
RenderSelectedPageOnly
=
"false"
>
<
telerik:RadPageView
runat
=
"server"
ID
=
"AddressInfoPageView"
>
<
asp:Label
ID
=
"Label3"
Font-Bold
=
"true"
Font-Italic
=
"true"
Text='<%# Eval("clientID")%>'
Visible="false" runat="server" />
<
div
style
=
"display:inline-block"
>
<
telerik:RadGrid
ID
=
"RadGrid2"
runat
=
"server"
AutoGenerateColumns
=
"false"
DataSourceID
=
"SqlDataSource2"
CssClass
=
"RadGrid"
GridLines
=
"None"
AllowPaging
=
"False"
AllowSorting
=
"True"
ShowStatusBar
=
"true"
AllowAutomaticDeletes
=
"True"
AllowAutomaticInserts
=
"True"
height
=
"320px"
Width
=
"300px"
>
<
ClientSettings
>
<
ClientEvents
OnRowSelected
=
"RowSelected"
/>
<
Selecting
AllowRowSelect
=
"true"
></
Selecting
>
<
Scrolling
AllowScroll
=
"True"
UseStaticHeaders
=
"True"
SaveScrollPosition
=
"true"
></
Scrolling
>
</
ClientSettings
>
<
MasterTableView
CommandItemDisplay
=
"Top"
CommandItemSettings-AddNewRecordText
=
"Add New Client"
GridLines
=
"None"
DataSourceID
=
"SqlDataSource2"
DataKeyNames
=
"clientID"
>
<
Columns
>
<
telerik:GridButtonColumn
CommandName
=
"Edit"
Text
=
"Edit"
>
</
telerik:GridButtonColumn
>
<
telerik:GridBoundColumn
DataField
=
"app"
UniqueName
=
"app"
HeaderText
=
"Application No."
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"clientTypeName"
UniqueName
=
"type"
HeaderText
=
"Relationship"
>
</
telerik:GridBoundColumn
>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
asp:TextBox
ID
=
"TextBox10"
runat
=
"server"
Text='<%# Bind("app")%>' TabIndex="1" Enabled="false"/>
</
FormTemplate
>
</
EditformSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
</
div
>
<
asp:SqlDataSource
ID
=
"SqlDataSource2"
runat
=
"server"
ConnectionString
=
"Data Source=10.131.121.240;Integrated Security=false;Initial Catalog=Contacts_Dev; User ID=TestUser;Password=100.1kthx"
providerName
=
"System.Data.SqlClient"
DeleteCommand
=
"DELETE FROM [Contacts_Dev].[dbo].[Contact_apps] WHERE [clientID] = @clientID"
InsertCommand
=
"INSERT INTO [Contacts_Dev].[dbo].[Contact_apps] ([app], [clientType], [AutoID],) VALUES (@clientID, @app, @clientType, @AutoID)"
SelectCommand
=
"SELECT * FROM [Contacts_Dev].[dbo].[Contact_apps] as a, [Contacts_Dev].[dbo].[Contacts_Type_LUT] as b WHERE a.[clientID] = @clientID AND a.[clientType] = b.[clientType]"
UpdateCommand
=
"UPDATE [Contacts_Dev].[dbo].[Contact_apps] SET [clientID] = @clientID, [clientType] = @clientType, [AutoID] = @AutoID WHERE [clientID] = @clientID"
>
<
SelectParameters
>
<
asp:ControlParameter
Name
=
"clientID"
Type
=
"String"
ControlID
=
"Label3"
PropertyName
=
"Text"
/>
</
SelectParameters
>
<
DeleteParameters
>
<
asp:Parameter
Name
=
"clientID"
Type
=
"Int32"
></
asp:Parameter
>
</
DeleteParameters
>
<
InsertParameters
>
<
asp:Parameter
Name
=
"app"
Type
=
"String"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"clientType"
Type
=
"String"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"AutoID"
Type
=
"Int32"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"clientID"
Type
=
"Int32"
></
asp:Parameter
>
</
InsertParameters
>
<
UpdateParameters
>
<
asp:Parameter
Name
=
"app"
Type
=
"String"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"clientType"
Type
=
"String"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"AutoID"
Type
=
"Int32"
></
asp:Parameter
>
<
asp:Parameter
Name
=
"clientID"
Type
=
"Int32"
></
asp:Parameter
>
</
UpdateParameters
>
</
asp:SqlDataSource
>
<
div
style
=
"display:inline-block"
>
<
table
id
=
"Table4"
style
=
"width:100px; height:500px; display:inline"
>
<
tr
>
<
td
colspan
=
"2"
>
<
b
>Details I would like to display would go here, details based on Application No. Selected</
b
>
</
td
>
</
tr
>
</
table
>
</
div
>
</
telerik:RadPageView
>
A possible solution is to get the DataKeyValue on the client and based on it to display the information by using a client code. Generally from OnRowSelected event arguments you can access the dataKeyValues method and get the need information. Keep in mind you need to set the data field which you want to access to ClientDataKeyNames property of the grid.
Regards,
Kostadin
Telerik
Thank you!!!! that worked.
Now, I'm trying to delete/insert rows in radgrid2 using sqldatasource and in doesn't work...
I examined the provided code and as far as I can see you have configure it correctly. Could you please let me know whether you have receive some client or server exception? Also can you please check whether the grid CRUD operations work when you move the grid outside the edit form?
Regards,
Kostadin
Telerik
I was able to figure it out. I have a hard time finding RadGrid2 from code behind, so I've been using javascript.
Now, I'm trying to programmatically select a row in RadGrid2 based on the value of a textbox. I thought about passing the value of the textbox to the filter, but no luck. I tried setting the filter's default value; but it looks like it can only be accomplished on code behind; Like I said, I've had no luck accessing RadGrid2 on code behind. Any advice will be greatly appreciated.
I forgot to mention that the textbox is outside of RadGrid1 and I also use that textbox to display information on RadGrid1 on button click.
Thanks!
In order to select the items of the grid on the server you can hook OnItemDataBound event handler and set selected property to true to each row that need to be selected. Please check out the following code snippet.
protected
void
RadGrid2_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = e.Item
as
GridDataItem;
if
(shouldSelect)
{
item.Selected =
true
;
}
}
}
Regards,
Kostadin
Telerik
Awesome, Thank you!