Hello,
I have got radgrid which consists of 2 columns, 1 Test checkbox and other is amount. What I would like to do is this when user tick testcheck box I want amount column to be editable.
can you please tell me how to get round of this problem.
<
telerik:RadGrid ID="TestGrid" runat="server" AutoGenerateColumns="False" GridLines="None" ShowHeader="False">
<MasterTableView DataKeyNames="ID">
<MasterTableView EditMode="InPlace">
<Columns>
<telerik:GridTemplateColumn>
<ItemStyle Width="20px" />
<ItemTemplate>
<asp:CheckBox ID="TestCheckBox" runat="server" AutoPostBack="true" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn HeaderText="Amount" DataField="Amount" UniqueName="Amount" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
Many thanks
5 Answers, 1 is accepted
You can achieve this by attaching 'OnCheckedChanged' event to CheckBox in ItemTemplate. In that event handler access the GridItem and put it in edit mode.
In order to show the Update/Cancel button for edit row, you need to add a Edit column in to the Column collection property of GridTableView. Then in PreRender event you can hide edit button for all the grid items except the item in edit mode.
ASPX:
<
Columns
>
. . . . . . . . . .
<
telerik:GridEditCommandColumn
UniqueName
=
"GridEditCommandColumn"
>
</
telerik:GridEditCommandColumn
>
</
Columns
>
C#:
protected
void
TestCheckBox_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridDataItem item = (GridDataItem)chk.NamingContainer;
item.Edit =
true
;
// put the row in edit mode
TestGrid.Rebind();
}
protected
void
TestGrid_PreRender(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
TestGrid.Items)
{
LinkButton btn = (LinkButton)item[
"GridEditCommandColumn"
].Controls[0];
if
(!item.Edit)
btn.Visible =
false
;
}
}
Thanks,
Princy.
In PreRender event you can reset the Checked property of CheckBox like below.
C#:
protected
void
TestGrid_PreRender(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
TestGrid.Items)
{
LinkButton btn = (LinkButton)item[
"GridEditCommandColumn"
].Controls[0];
if
(!item.Edit)
btn.Visible =
false
;
else
{
CheckBox chk = (CheckBox)item.FindControl(
"TestCheckBox"
);
chk.Checked =
true
;
}
}
}
Thanks,
Princy.
Your help has been really great!
I'm displaying TestGrid data on TestPage.aspx. I have Save button on this page.
When user tick on grid row(s) amount gridboundcolumn in datagrid becomes editable so can user add amount in that textbox.
I'm saving that row data in database.
What I need to do now is this when user load test.aspx page i want to load data from the database and tick row(s) and make amount gridboundcolumn editable which has been selected previously.
Many thanks
You can assign the value from database to the CheckBox by using data binding expression on the Checked property of CheckBox.
ASPX:
<
ItemTemplate
>
<
asp:CheckBox
ID
=
"TestCheckBox"
runat
=
"server"
AutoPostBack
=
"true"
Checked='<%#Eval("isapproved") %>' />
</
ItemTemplate
>
And in PreRender event loops through each item and make it editable if the CheckBox for corresponding row is selected.
C#:
protected
void
TestGrid_PreRender(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
TestGrid.Items)
{
CheckBox chk = (CheckBox)item.FindControl(
"TestCheckBox"
);
item.Edit = chk.Checked;
}
TestGrid.Rebind();
}
Note: You should set AllowMultiRowEdit="true" to enable multi-row editing
Thanks,
Princy.