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

Select Items from selected row

1 Answer 229 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 24 Nov 2014, 11:01 PM
I am using telerik:RadGridView along with telerik:RadContextMenu.ContextMenu to generate a right click menu inside my application. I need to be able to grab the servername and session id from the selected row in order to pass to the Disconnect and Logoff functions. However I ma having difficulty grabbing the data I need.

Here is the XAML for the component

<telerik:RadGridView  x:Name="UserSessionGrid" IsReadOnly="True" FontWeight="Bold" AutoGeneratingColumn="UserSessionGrid_AutoGeneratingColumn"  CanUserResizeColumns="False" CanUserDeleteRows="False" CanUserResizeRows="False" ClipboardCopyMode="All" Copied="UserSessionGrid_Copied" >
   <telerik:RadContextMenu.ContextMenu>
     <telerik:RadContextMenu Opened="RadContextMenu_Opened" ItemClick="RadContextMenu_ItemClick">
      <telerik:RadContextMenu.Items>
        <telerik:RadMenuItem Header="Copy" />
        <telerik:RadMenuItem Header="Disconnect" />
        <telerik:RadMenuItem Header="Logoff" />
      </telerik:RadContextMenu.Items>
    </telerik:RadContextMenu>
  </telerik:RadContextMenu.ContextMenu>
</telerik:RadGridView>

Here is the relevant code that handles the Right Click

/// <summary>
/// Handles the ItemClick event of the RadContextMenu control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Windows.RadRoutedEventArgs"/> instance containing the event data.</param>
private void RadContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
  RadContextMenu menu = (RadContextMenu)sender;
  RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
  GridViewRow row = menu.GetClickedElement<GridViewRow>();
  GridViewCell cell = menu.GetClickedElement<GridViewCell>();
  GridViewRowItem rowitem = menu.GetClickedElement<GridViewRowItem>();
  if (clickedItem != null && row != null)
  {
    string header = Convert.ToString(clickedItem.Header);
 
     switch (header)
     {
       case "Copy":
         Clipboard.SetText(cell.Value.ToString());
         break;
       case "Disconnect":
         // Grab Server Name Column and Session ID Column Data
         break;
       case "Logoff":
         // Grab Server Name Column and Session ID Column Data
         break;
       default:
         break;
    }
  }
}
/// <summary>
/// Handles the Opened event of the RadContextMenu control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
  RadContextMenu menu = (RadContextMenu)sender;
  GridViewRow row = menu.GetClickedElement<GridViewRow>();
 
  if (row != null)
  {
    row.IsSelected = row.IsCurrent = true;
    GridViewCell cell = menu.GetClickedElement<GridViewCell>();
    if (cell != null)
    {
      cell.IsCurrent = true;
    }
  }
  else
  {
    menu.IsOpen = false;
  }
}

Here is my session class

class Session
{
    public String Server { get; set; }
    public String Domain { get; set; }
    public String User { get; set; }
    public int sID { get; set; }
    public ConnectionState State { get; set; }
    public IPAddress IP { get; set; }
    public String Workstation { get; set; }
    public DateTime? Connect { get; set; }
    public DateTime? Login { get; set; }
    public TimeSpan Idle { get; set; }
    /// <summary>
    /// Initializes a new instance of the <see cref="Session"/> class.
    /// </summary>
    /// <param name="server">The server.</param>
    /// <param name="domain">The domain.</param>
    /// <param name="user">The user.</param>
    /// <param name="session">The session.</param>
    /// <param name="state">The state.</param>
    /// <param name="ip">The ip.</param>
    /// <param name="workstation">The workstation.</param>
    /// <param name="connect">The connect.</param>
    /// <param name="login">The login.</param>
    /// <param name="idle">The idle.</param>
    public Session (string server, string domain, string user, int session, ConnectionState state, IPAddress ip, string workstation, DateTime? connect, DateTime? login, TimeSpan idle)
    {
        this.Server = server.ToUpper();
        this.Domain = domain.ToUpper();
        this.User = user;
        this.sID = session;
        this.State = state;
        this.IP = ip;
        this.Workstation = workstation.ToUpper();
        this.Connect = connect;
        this.Login = login;
        this.Idle = idle;
    }
}

Which is populated by using the following code

/// <summary>
 /// Handles the DoWork event of the worker control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
 private void worker_DoWork(object sender, DoWorkEventArgs e)
 {
   App.Current.Dispatcher.Invoke((Action)delegate
   {
     UserSessionGrid.IsBusy = true;
   });
   ITerminalServicesManager manager = new TerminalServicesManager();
   foreach (var ServerName in ServerList)
   {
     using (ITerminalServer server = manager.GetRemoteServer(ServerName))
     {
     try
     {
       server.Open();
       foreach (ITerminalServicesSession session in server.GetSessions())
       {
         items.Add(new Session(server.ServerName, session.DomainName, session.UserName, session.SessionId, session.ConnectionState, session.ClientIPAddress, session.WindowStationName, session.ConnectTime,session.LoginTime, session.IdleTime));
         //worker.ReportProgress(session.SessionId);
       }
       server.Close();
 
     }
     catch (Win32Exception) { }
     catch (SystemException) { }
     catch (Exception) { }
     }
   }
 }

1 Answer, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 25 Nov 2014, 12:21 AM
And I figured it out

((Session)UserSessionGrid.SelectedItem).Server
((Session)UserSessionGrid.SelectedItem).SessionID
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Share this question
or