Wednesday, September 18, 2013

How to get selectedItem value from a WPF DataGrid

For the DataGrid you can capture the SelectionChanged event.
In your XAML you would type SelectionChanged= and then press tab to allow it to create a method for you in the C# code.
Once you've done that you should go to the code files that accompanies your XAML file and find the event that was created.
For me it was:
SelectionChangedEvent
In this event I would add logic that would capture the msgId . Here is an example:
 private void SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
        {
             //e.AddedItems contains the selected data
             System.Collections.IList selectedRow = e.AddedItems;
       //Logic to get the msgId from the IList
       // - in my example I am binding to a DataTable
       // - also notice that selectedRow[0] because I am
       // - allowing only 1 row selection at a time.
             if (selectedRow.Count > 0)      
                 msgId = ((AwareAlertUser)selectedRow[0]).MsgID.ToString();                   
        }

Tuesday, September 17, 2013

SQL Server function to seperate or split delimited single string column

CREATE FUNCTION [dbo].[awrfn_Split]
(
@String varchar(200),
@Delimiter char(1)
)  
RETURNS @TempTable TABLE (items varchar(200))  
as  
begin
 
declare @intPosition int,
@vchElement varchar(200)  
     
  if len(@String) < 1 or @String is null
  return  
 
  set @intPosition = 1
  while @intPosition <> 0  
  begin  
    set @intPosition = charindex(@Delimiter, @String)  
    if @intPosition <> 0  
      set @vchElement = left(@String, @intPosition - 1)  
    else  
      set @vchElement = @String  
   
    if len(@vchElement) > 0
      insert into @TempTable(items) values(@vchElement)  

    set @String = right(@String, len(@String) - @intPosition)  

    if len(@String) = 0 break  
  end

return
end

How to detect the enter key press in WPF

This example shows how to detect when the Enter key is pressed on the keyboard.
This example consists of a Extensible Application Markup Language (XAML) file and a code-behind file.

Example
When the user presses the Enter key in the TextBox, the input in the text box appears in another area of the user interface (UI).
The following XAML creates the user interface, which consists of a StackPanel, a TextBlock, and a TextBox.
<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>


The following code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a message is displayed in the TextBlock.
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}