0

嗨,我有一个 radgrid,我必须在其上找到一个值,如果找到该项目,则生成消息

下面是我的代码

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
        If IsAlreadyExist() Then
            ram.Alert("")
        Else
            If IsAlreadyAdded() Then
                ram.Alert("")
            Else
                employees()
            End If


        End If
    End Sub

这是 IsAlreadyAdded 方法,我在其中尝试在网格中查找特定值,如果存在,它将返回 false

Private Function IsAlreadyAdded() As Boolean
        'If rgListnk.MasterTableView.Items.Count > 0 Then
        Dim itm As GridDataItem = rgList.MasterTableView.FindItemByKeyValue("DEFAULT", "Y")

        If IsNothing(itm) Then
            Return False
        Else
            Return True
        End If

    End Function

谢谢...

4

1 回答 1

2

您需要遍历 Grid 的每一行才能找到单元格的值。

如果要通过FindItemByKeyValue查找项目,请确保指定DataKeyNames

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadGrid ID="RadGrid1" runat="server"
    OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="Id">
    </MasterTableView>
</telerik:RadGrid>
<asp:Button runat="server" ID="Button1"
    OnClick="Button1_Click" Text="Submit" />

public partial class Default : System.Web.UI.Page
{
    public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid1_NeedDataSource(object sender,
        GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = new List<User>
        {
            new User {Id = 1, FirstName = "Jon", LastName = "Doe"},
            new User {Id = 2, FirstName = "Marry", LastName = "Doe"},
            new User {Id = 3, FirstName = "Eric", LastName = "Newton"},
        };
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var firstItem = RadGrid1.MasterTableView.FindItemByKeyValue("Id", 1);
        if (firstItem != null)
        {
            var firstName = firstItem["FirstName"].Text;
            var lastName = firstItem["lastName"].Text;
        }

        foreach (GridItem item in RadGrid1.MasterTableView.Items)
        {
            if (item is GridDataItem)
            {
                var dataItem = item as GridDataItem;
                var firstName = dataItem["FirstName"].Text;
                var lastName = dataItem["lastName"].Text;
            }
        }
    }
}
于 2013-11-13T15:05:15.500 回答