C# 获取 Telerik RadGrid 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11807646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:20:17  来源:igfitidea点击:

Get rows of a Telerik RadGrid

c#asp.nettelerikradgrid

提问by Mahdi Tahsildari

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

我正在研究一个 RadGrid,我想访问它的行,但它似乎没有.Rows属性。

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.

如何访问rgCustomers的 Rows 集合?我想为每一行添加一个按钮。

采纳答案by Daniel

According to Telerik's documentation,

根据Telerik 的文档

"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).

“网格中的每个动态行都代表来自指定数据源的一条记录。动态行由 GridDataItem 类(GridItem 的后代)表示。

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

每个 GridTableView 都有一组 GridDataItem 类型的行(Items 集合)。”

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

所以要使用网格的Items集合,也就是GridDataItems的集合。

protected void btnLoad_Click(object sender, EventArgs e)
{
  rgCustomers.DataSource = odsCustomers;
  rgCustomers.DataBind();
  foreach (GridDataItem row in rgCustomers.Items)
  {
  }
}

回答by Varius

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

我假设它是 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:

如果您想访问数据绑定网格(不是行数据)中的行控件 - 您必须使用 RadGrid 的 ItemContainerGenerator 属性。例如:

rgCustomers.ItemContainerGenerator.ContainerFromIndex(0);

or

或者

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

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

将返回第一行控件(如果我没记错的话,属于 RadGridViewRow 类型)

回答by Drag and Drop

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

- 如果你想在每一行添加一个按钮:

GridTemplateColumnor GridButtonColumnwill do the trick.

GridTemplateColumnGridButtonColumn可以解决这个问题。

- 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() methodto extract the record ID:

    DEMO_INT = (int)G.GetDataKeyValue("mySweetInt"); 
    DEMO_STRING = (string)G.GetDataKeyValue("MyString");
    
  1. 使用按钮的 OnClick 事件处理程序。

    <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. 使用 (sender as RadButton).NamingContainer 获取对 GridDataItem 的引用。

    protected void BTN_BL_Click(object sender, EventArgs e)
    {
        GridDataItem G = ((RadButton)sender).NamingContainer as GridDataItem;
    }
    
  3. 使用GetDataKeyValue() 方法提取记录 ID:

    DEMO_INT = (int)G.GetDataKeyValue("mySweetInt"); 
    DEMO_STRING = (string)G.GetDataKeyValue("MyString");