This is a migrated thread and some comments may be shown as answers.

Fetching SelectedItems values in Grid

1 Answer 612 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jerald
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>
            <MasterTableView>               
                <PagerStyle PageSizes="10, 25, 50, 100, 250, 400" AlwaysVisible="true" />               
                <Columns>
                    <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>                   
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

 

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;
                        dt.Rows.Add
                        (
                            Convert.ToString(de.Properties["givenName"].Value),
                            Convert.ToString(de.Properties["sn"].Value),
                            Convert.ToString(de.Properties["mail"].Value),
                            Convert.ToString(de.Properties["department"].Value),
                            Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", "$1")
                        );
                    }
                    rgAdUsrs.DataSource = dt;
                    rgAdUsrs.DataBind();
                }
            }

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Eyup
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:
https://www.telerik.com/support/kb/aspnet-ajax/grid/details/how-to-bind-radgrid-properly-on-server-side

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:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

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

I hope this will prove helpful.

Regards,
Eyup
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.
Tags
Grid
Asked by
Jerald
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or