This is a migrated thread and some comments may be shown as answers.
Fetching SelectedItems values in Grid
1 Answer 21 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Jerald asked on 31 Aug 2018, 09:06 PM

I have a Grid where I select a row.  When I select a row I want to capture the email address that's displayed in the Grid.  I typically use a SqlDataSource as the data source for my Grids and don't have any trouble getting the value from a cell on a selected row, but this time I had to use a DataTable.  Please help me modify my code so that I may capture the email address from the selected row on the Grid.


Here is the Grid markup:

<telerik:RadGrid ID="rgAdUsrs" runat="server" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" ShowGroupPanel="true" Skin="Office2010Black" OnSelectedIndexChanged="rgAdUsrs_SelectedIndexChanged" GroupingSettings-CaseSensitive="false">
            <ClientSettings AllowDragToGroup="true" AllowColumnsReorder="true" ReorderColumnsOnClient="true"></ClientSettings>
                <PagerStyle PageSizes="10, 25, 50, 100, 250, 400" AlwaysVisible="true" />               
                    <telerik:GridButtonColumn ButtonType="LinkButton" Text="Select" CommandName="Select"></telerik:GridButtonColumn>
                    <telerik:GridBoundColumn DataField="givenName" HeaderText="First Name" SortExpression="givenName" UniqueName="givenName" FilterControlAltText="Filter givenName column" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="sn" HeaderText="Last Name" SortExpression="sn" UniqueName="sn" FilterControlAltText="Filter sn column" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="mail" HeaderText="Email" SortExpression="mail" UniqueName="mail" FilterControlAltText="Filter mail column" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="department" HeaderText="Department" SortExpression="department" UniqueName="department" FilterControlAltText="Filter department column" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="manager" HeaderText="Manager" SortExpression="manager" UniqueName="manager" FilterControlAltText="Filter manager column" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>                   


Here is the code behind for the SelectedIndexChanged event:

protected void rgAdUsrs_SelectedIndexChanged(object sender, EventArgs e)
            foreach (GridDataItem item in rgAdUsrs.SelectedItems)
                repidtxttest.Text = item["mail"].Text;
                hdnRepId.Value = item["mail"].Text;
                hdnRepGivenName.Value = item["givenName"].Text;
                hdnRepSn.Value = item["sn"].Text;
                hdnRepDept.Value = item["department"].Text;
                hdnRepsMgr.Value = item["manager"].Text;


Here is the code from the data table:

DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5]
                new DataColumn("givenName", typeof (string)),
                new DataColumn("sn", typeof (string)),
                new DataColumn("mail", typeof (string)),
                new DataColumn("department", typeof (string)),
                new DataColumn("manager", typeof (string))
            using (var context = new PrincipalContext(ContextType.Domain, null))
                using (var group = (GroupPrincipal.FindByIdentity(context, "Group1")))
                    var users = group.GetMembers(true);
                    foreach (UserPrincipal user in users)
                        DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
                            Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", "$1")
                    rgAdUsrs.DataSource = dt;


1 Answer, 1 is accepted

Sort by
Telerik team
answered on 05 Sep 2018, 05:40 AM
Hello Jerald,

When binding the grid programmatically, make sure that you are using the NeedDataSource event handler as suggested here:

Then, to get the text, you can use the standard cell.Text approach. If there are controls inside the cell, you can use cell.Controls[0] or cell.FindControl("ControlID") methods. Alternatively, you can define the field in the DataKeyNames collection and make avail of the GetDataKeyValue method:

In addition, I am also sending a sample RadGrid web site.

I hope this will prove helpful.

Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question