0

So I have DataGrid with DataContext set to a DataTable, and is loaded via FillJobsGrid() when Window_Loaded. This works OK, but I wanted to be able to select a specific item when launching via a command line, the args part works without an issue, just 2 args with a switch and a value. The value is the Id of a row in the JobsGrid, what I cant see is how to select this row. before running BackUpSelectedClicked() For inspiration amongst many other pages I looked here but which ever way I approach this I have never got a datagrid.Items.count. I guess this is about which event to run my code from. Before the mod I had:

private void FillJobsGrid()
{
    using (MySqlClientWrapper db = new MySqlClientWrapper(MyConnString))
    {
        string sql = "SELECT * FROM backupjobs";
        SetJobsTable(db.GetDataTable(sql));
        JobsGrid.DataContext = GetJobsTable();
    }

}

The code I was hoping would do the trick was:

if (RunFromCommandLine)
{
    foreach (DataRowView job in JobsGrid.Items)
    {
        if (job != null)
        {
            DataRow jobRow = job.Row;
            long id = Convert.ToInt32(jobRow["Id"]);

            if (Convert.ToInt32(CommandLineJobId) == id)
            {
                JobsGrid.SelectedItem = job;
            }
        }
    }
    BackUpSelectedClicked();
}

I have tried inserting it in FillJobsGrid, and placing it in DataGrid.DataContextChanged, Initialised, Loaded etc. Any help appreciated.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
Andrew Seabrook
  • 397
  • 2
  • 17
  • Please check that this condition evaluates to `true`: `if (Convert.ToInt32(CommandLineJobId) == id)`. – BionicCode Nov 05 '19 at 11:05
  • Yes this was an error - it should have been ToInt64, but the problem is that the foreach loop is never walked as JobsGrid.Items.Count == 0 – Andrew Seabrook Nov 05 '19 at 11:18
  • How are you initializng `JobsGrid.ItemsSource`? Can you verify that you get items from the database and also the `DataTable` which I assume is the data source is also not empty? – BionicCode Nov 05 '19 at 11:24
  • `This works OK`: does your ``DataGrid`` actually display the items you want? It's not clear what it means. – Corentin Pane Nov 05 '19 at 11:24
  • Don't assign the data source to the `DataContext`. Rather assign it to the `ItemsSource`: `JobsGrid.ItemsSource = myDataTable`. Then the `Items` collection should contain the expected data. – BionicCode Nov 05 '19 at 11:26
  • @BionicCode JobsGrid.ItemsSource = GetJobsTable(); gives error cannot explicitly convert type DataTable to IEnumerable. Data asssignment works fine DataContext.Rows.Count shows 3 - it displays in the grid too. – Andrew Seabrook Nov 05 '19 at 11:34
  • @CorentinPane yes the display is fine, just cant programatically select because JobsGrid.Items.Count == 0 - obviously need other method or I am to early? – Andrew Seabrook Nov 05 '19 at 11:35
  • 1
    Yes, call `JobsGrid.ItemsSource = GetJobsTable().DefaultView` instead – BionicCode Nov 05 '19 at 11:36
  • @BionicCode DefaultView ... I'm very rusty. Moving forward again and appreciate your help, but exiting the foreach I get another error - "Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Data.DataRowView'." This is after iterating the loop and before jumping to next line. Any thoughts? – Andrew Seabrook Nov 05 '19 at 11:47
  • Change your loop to: `foreach (DataRowView job in JobsGrid.Items.OfType())` – BionicCode Nov 05 '19 at 11:56
  • Not to worry - New Item row is the issue - Set CanUserAddRows="False" rather than filtering out the iteration. Thanks @BionicCode – Andrew Seabrook Nov 05 '19 at 11:56
  • As you wish. The first item in the collection is the placeholder row which is not a `DataRowView`. `CanUserAddRows="False"` will remove this row. – BionicCode Nov 05 '19 at 11:59

0 Answers0