0

I am making a small application for personal use to practice WPF and C#. The application will launch games (or other applications) based on file paths which are stored in a SQL table. My SQL connection works fine and the data is retrieved.

string query = "SELECT Title, [Path] FROM GOG.dbo.Games";

I am using a DataGrid control to display the list of games, and their paths. I have a ‘Launch Game’ button which I would like to get the currently highlighted item in the DataGrid (i.e. the Path) and then take the path string and therefore run it using the Process.Start() method.

Here’s what I have on my Button_Click but I can’t get it to work:

private void LaunchButton_Click(object sender, RoutedEventArgs e)
{
    string gamePath = dataGrid1.SelectedItem.ToString();
    Process.Start(gamePath);
}

Debug says “gamePath "System.Data.DataRowView" string”, and not the value of the actual path e.g: “C:\Windows\System32\calc.exe” which I think is why the application bugs out saying “The system cannot find the file specified” because “System.Data.DataRowView” is of course not a valid program.

How can I get it to put the correct string through instead of “System.Data.DataRowView”?

1
  • to what is the datagrid binded ? Mar 25, 2015 at 11:29

2 Answers 2

0

dataGrid1.SelectedItem gives you System.Data.DataRowView object. So you can use it directly as string. you must get path from it like following.

private void LaunchButton_Click(object sender, RoutedEventArgs e)
{
    var row=dataGrid1.SelectedItem as System.Data.DataRowView;
    if(row!=null)
    {
       string gamePath = row["Path"].ToString();
       Process.Start(gamePath);
    }
}
0
0

cast selected item of DataGrid to DataRowView

DataRowView row = (DataRowView)dataGrid1.SelectedItem; 

then

row["ColumnName"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.