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

RadGrid edit mode?

5 Answers 413 Views
Grid
This is a migrated thread and some comments may be shown as answers.
myrad
Top achievements
Rank 1
myrad asked on 27 Oct 2010, 11:33 AM

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

Sort by
0
Princy
Top achievements
Rank 1
answered on 27 Oct 2010, 12:29 PM
Hello Myrad,

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.
0
myrad
Top achievements
Rank 1
answered on 27 Oct 2010, 04:05 PM
It works great but there is slight problem. when i click on checkbox it gets reset back to unticked.
0
Princy
Top achievements
Rank 1
answered on 28 Oct 2010, 07:32 AM
Hello Myrad,

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.
0
myrad
Top achievements
Rank 1
answered on 08 Nov 2010, 10:50 AM
Hi 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


0
Princy
Top achievements
Rank 1
answered on 09 Nov 2010, 12:25 PM
Hello Myrad,

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.
Tags
Grid
Asked by
myrad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
myrad
Top achievements
Rank 1
Share this question
or