Get rows of a Telerik RadGrid
Asked Answered
G

3

16

I'm working on a RadGrid, and I want to access its rows but it seems it does not have a .Rows property.

Here's what I have tried until now:

enter image description here

How can I access rgCustomers's Rows collection? I want to add a button to each row.

Garceau answered 4/8, 2012 at 9:59 Comment(0)
H
10

According to Telerik's documentation,

"Each dynamic row in the grid represents a record from the specified data source. Dynamic rows are represented by the GridDataItem class (a descendent of GridItem).

Each GridTableView has a set of rows (the Items collection) of type GridDataItem."

So you want to use the Items collection of the grid, which is a collection of GridDataItems.

protected void btnLoad_Click(object sender, EventArgs e)
{
  rgCustomers.DataSource = odsCustomers;
  rgCustomers.DataBind();
  foreach (GridDataItem row in rgCustomers.Items)
  {
  }
}
Halt answered 4/8, 2012 at 18:45 Comment(0)
B
3

I'm assuming it's WPF/Silverlight RadGrid?

If You want to access row control in databound grid (not row data) - You'll have to use ItemContainerGenerator property of RadGrid. For example:

rgCustomers.ItemContainerGenerator.ContainerFromIndex(0);

or

rgCustomers.ItemContainerGenerator.ContainerFromItem(odsCustomers[0]);

will return first row control (of type RadGridViewRow if I remember correctly)

Belshazzar answered 4/8, 2012 at 10:11 Comment(3)
Should I do the same if I want to iterate through rows?Garceau
I'm using Telerik.Web.UI.RadGridGarceau
Ahh, it's Asp.net control. My solution is WPF/Silverlight based. Sorry, I don't know much about Web controls.Belshazzar
T
1

- If you want to add a button on every row:

GridTemplateColumn or GridButtonColumn will do the trick.

- If you want to access the current row:

  1. Use the OnClick event handler of the button.

    <telerik:RadButton ID="BTN_DEMO" runat="server" HeaderText="N°1 DEMO BTN"
    Text='<%#"Click Me iM N°"+((IhateEvalDataSource) Container.DataItem).Stuff_ID %>' 
    OnClick="BTN_DEMO_Click"></telerik:RadButton>
    
  2. Get a reference to the GridDataItem using (sender as RadButton).NamingContainer.

    protected void BTN_BL_Click(object sender, EventArgs e)
    {
        GridDataItem G = ((RadButton)sender).NamingContainer as GridDataItem;
    }
    
  3. Use GetDataKeyValue() method to extract the record ID:

    DEMO_INT = (int)G.GetDataKeyValue("mySweetInt"); 
    DEMO_STRING = (string)G.GetDataKeyValue("MyString");
    
Transportation answered 8/9, 2016 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.