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

Access to Bound Column

3 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
lupotana
Top achievements
Rank 1
lupotana asked on 07 Aug 2013, 02:04 PM

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

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 07 Aug 2013, 02:17 PM
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
0
lupotana
Top achievements
Rank 1
answered on 07 Aug 2013, 02:50 PM
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
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Aug 2013, 10:58 AM
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
Tags
Grid
Asked by
lupotana
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
lupotana
Top achievements
Rank 1
Share this question
or