I have one Task
which go throw DataGridRow
and do some task. When he finish it set background color
for that row. I added cancel button
to stop task, and continue button
to continue where it finished last time. All work perfect except changing background color for row.
This is XAML code, i'm new to WPF so it's not too big for DataGrid
<DataGrid
Name="dataGridViewMyGroups"
Grid.Row="0" ColumnWidth="*"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
SelectionUnit="FullRow"
SelectionMode="Single"
MouseDoubleClick="dataGridViewMyGroups_MouseDoubleClick">
</DataGrid>
Here is a C# code for changing background color.
DataGridRow rowColor = (DataGridRow)dataGridViewMyGroups.ItemContainerGenerator
.ContainerFromIndex(number);
rowColor.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(223, 227, 238));
This code work when I click on start Button
and it change Background
color for each Row
. Problem is when I click cancel Button
and then click on continue Button
, then I got NullReferenceException
. Continue button only check last ID in DataBase Table
.
int number=0;
foreach (GroupsInList group in dataGridViewMyGroups.Items)
{
if (fbgroupID != null && check==true)
{
number++;
if (fbgroupID != groupLink)
{
continue;
}
check = false;
continue;
}
//Do something and change background (code above).
number++;
}
Code for continue Button
work except changing row's background.
UPDATE:
Code for Cancel Button
:
if (MessageBox.Show("Do you want to stop posting?", "Confirmation",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
tokenSource.Cancel();
}
Code for Continue Button
:
int postID;
string fbGroupID;
int listID;
using (OleDbConnection connection = new OleDbConnection(conn))
{
//code for getting value from `DB Table`
postID = list[0].PostID;
fbGroupID = list[0].FBGroupID;
listID = list[0].ListForGroupID;
}
cmbSelectList.SelectedValue = listID;
cmbSavedPosts.SelectedValue = postID;
loadGroupsInList(); //Maybe this is problem because here I update(reload) DataGrid again.
tokenSource = new CancellationTokenSource();
try
{
await TaskPostToGroup(tokenSource.Token, fbGroupID, true);
}
catch (OperationCanceledException ex)
{
System.Windows.MessageBox.Show(ex.Message, "CANCELED", MessageBoxButton.OK, MessageBoxImage.Stop);
}
catch (NullReferenceException)
{
//I don't have solution for changing background color for each row when continue button is clicked
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
DB Table
and call task with foreach loop – Roquefortawait TaskPostToGroup(tokenSource.Token, fbGroupID, true);
is part of code where foreach loop executes and I have code for changing background color. In line for changing background color I getNullReferenceException
. I haveint number
which increment at the end of foreach loop. – Roquefort