Question

Hi there i have a radgrid on which i have to find a value and if the item is found then generate message

below is my code

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

and here is the IsAlreadyAdded mehtod in which iam trying to find a specific value in grid if it exists it will return 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

Thanks...

Was it helpful?

Solution

You need to loop through each row of the Grid in order to find a value of a cell.

Make sure DataKeyNames is specified if you want to find item by FindItemByKeyValue.

<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;
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top