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

Disable Edit button for only one row in the grid

1 Answer 228 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Deepika
Top achievements
Rank 1
Deepika asked on 14 Dec 2011, 12:18 PM
Hi,
1. I have the grid with 7 columns, in that 4 are readonly during Edit. two of them are editable and one is the "Select" checkbox.
When I click a button external to the grid, I am triggering the initinsert event. During this, I want to copy the data as is for the readonly columns, from the row that is checked to the row that got added thru the initInsert event. How can I do this? [ Editable fields are editable here too]
2. Now that when the user enters data, if he tries to enter same data as above for the new row, then I need to allow him to do so, but should not allow him to edit the old row and need to disable the Edit button for that row.
Please suggest.
Thanks and Regards,
Deepika Karanth

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 Dec 2011, 06:39 AM
Hello,

Please below code for option 1.
Let me know its work or not.
If not then elaborate your both cases and if possible then also provide screen shot.

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert New Row" />
       <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
           OnNeedDataSource="RadGrid1_NeedDataSource"
           onitemdatabound="RadGrid1_ItemDataBound">
           <MasterTableView DataKeyNames="ID">
               <Columns>
                   <telerik:GridTemplateColumn HeaderText="checkbox">
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                   </telerik:GridBoundColumn>
               </Columns>
           </MasterTableView>
           <ClientSettings>
                
           </ClientSettings>
       </telerik:RadGrid>
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           Session["Selecteditem"] = null;
       }
   }
   protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
               new { ID = "1", Name ="Name1"},
               new { ID = "2", Name ="Name2"},
               new { ID = "3", Name ="Name3"},
               new { ID = "4", Name ="Name4"}
           };
       RadGrid1.DataSource = data;
 
   }
   protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
 
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       RadGrid1.MasterTableView.IsItemInserted = true;
       RadGrid1.Rebind();
   }
   protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
   {
       if ((sender as CheckBox).Checked)
       {
           GridDataItem item = ((sender as CheckBox).NamingContainer) as GridDataItem;
            
           Session["Selecteditem"] = item;
       }
       else
       {
           Session["Selecteditem"] = null;
       }
   }
   protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item.IsInEditMode && e.Item is GridEditableItem)
       {
           GridEditableItem edititem = e.Item as GridEditableItem;
           if (edititem.ItemIndex < 0)
           {
               (edititem["Name"].Controls[0] as TextBox).ReadOnly = true;
 
               if (Session["Selecteditem"] != null)
               {
                   GridDataItem gridDataItem = (GridDataItem)Session["Selecteditem"];
                   if (gridDataItem != null)
                   {
                       (edititem["Name"].Controls[0] as TextBox).Text = gridDataItem["Name"].Text;
                   }
               }
           }
       }
       if (e.Item is GridDataItem && Session["Selecteditem"] != null)
       {
           GridDataItem item = e.Item as GridDataItem;
           if (Convert.ToInt32(item.GetDataKeyValue("ID")) == Convert.ToInt32(((GridDataItem)Session["Selecteditem"]).GetDataKeyValue("ID")))
           {
               (item.FindControl("CheckBox1") as CheckBox).Checked = true;
           }
       }
   }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Deepika
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or