Access to Bound Column

4 posts, 1 answers
  1. lupotana
    lupotana avatar
    73 posts
    Member since:
    Nov 2006

    Posted 07 Aug 2013 Link to this post

    Good morning,
    I've a old code for delete row of Grid.

    I've a Bound Column in grid

    <telerik:GridBoundColumn UniqueName="WebUserCode" DataField="WebUserCode" Display="false" />

    and a Grid ButtonColumn

    <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmDialogType="RadWindow"
                      ConfirmText="Confermare l'eliminazione del record ?" ConfirmTitle="Conferma eliminazione"
                      ImageUrl="~/Images/16x16/delete2.png" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%"
                      Text="ELIMINA DATO" UniqueName="DeleteColumn" HeaderText="ELIMINA">
                      <ItemStyle HorizontalAlign="Center" Width="3%" />
                  </telerik:GridButtonColumn>

    this is the old code

    protected void gvList_DeleteCommand(object sender, GridCommandEventArgs e)
       {
           GridDataItem row = gvList.Items[e.Item.ItemIndex];
     
           try
           {
               item.Delete(Int32.Parse(row.Cells[2].Text));
           }
           catch (Exception ex)
           {
               notError.Title = "ELIMINAZIONE FALLITA";
               notError.Title = "L'ELIMINAZIONE DEL DATO HA CAUSATO PROBLEMI";
               notError.Show();
               WebTools.SendMailError(((System.Web.UI.TemplateControl)(this.Page)).AppRelativeVirtualPath, System.Reflection.MethodInfo.GetCurrentMethod().Name, ex, WebUser);
           }
     
           PopulateGridView();
       }

     

    The problem is that with the last version of RadGrid  row.Cells[2].text is NULL, I think for the Visible="False" property.

    How can I access at the bound column for to know the code of the record ?

    Thanks

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Aug 2013 Link to this post

    Hi ,

    If that bound column is your DataKeyName value,then you can access that in DeleteCommand event and delete that row.
    Try the below code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server"  ondeletecommand="RadGrid1_DeleteCommand">
        <MasterTableView DataKeyNames="WebUserCode">
            <Columns>
               . . . . . . . .
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
      {
          GridDataItem edit = (GridDataItem)e.Item;
          string WebUserCode= edit.GetDataKeyValue("WebUserCode").ToString();
         //Code to Delete using WebUserCode       
      }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. lupotana
    lupotana avatar
    73 posts
    Member since:
    Nov 2006

    Posted 07 Aug 2013 Link to this post

    Perfect, but with the old code I don't write the name of DataKeyNames column.
    There is a simple way for access to the collection of DataKeysnames with index and not with the name ?
    Thanks
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Aug 2013 Link to this post

    Hi Iupotana,

    There are two ways how you can achieve this in DeleteCommand to delete a row.
    1)If you want to use a bound column and set its Display="false",it will be accessible in code behind.The better way of accessing a bound column is using its UniqueName.Please check the below code snippet to achieve this scenario.

    ASPX:
    <telerik:GridBoundColumn UniqueName="WebUserCode" DataField="WebUserCode"  Display="WebUserCode" />

    C#:
    protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {   
         GridDataItem item = e.Item as GridDataItem;
         string itemValue = item["WebUserCode"].Text; //Accessing Bound Column value using UniqueName
        }

    2)Another approach to this is, not to use the bound column,you can use the DataKeyNames property of the RadGrid and access it in the code behind.In this case you don't have to add this column to the radgrid and can be accessed easily as I have shown in the above post.

    ASPX:
    <MasterTableView DataKeyNames="WebUserCode">

    C#:
    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
      {
          GridDataItem deleteitem= (GridDataItem)e.Item;
          string WebUserCode=deleteitem.GetDataKeyValue("WebUserCode").ToString();//Access using DataKeyNames  
      }

    If you want to access using the index,you can use the DataKeyNames ,please try the below code snippet.

    C#:
    protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
         GridDataItem dataItem = (GridDataItem)e.Item;
         var value = RadGrid1.MasterTableView.Items[e.Item.ItemIndex].GetDataKeyValue("OrderID");//Accessing the Index using DataKeyValue
        }

    Hope this helps,
    Thanks,
    Princy
Back to Top