我有一个Task
投掷DataGridRow
并执行某项任务。完成后,为该行设置background color
。我添加了取消button
来停止任务,并继续button
继续上次完成的位置。除了改变行的背景颜色外,所有工作都很完美
这是XAML代码,我是WPF的新手,所以它对DataGrid
来说不算太大
<DataGrid
Name="dataGridViewMyGroups"
Grid.Row="0" ColumnWidth="*"
VerticalScrollBarVisibility="Auto"
IsReadOnly="True"
SelectionUnit="FullRow"
SelectionMode="Single"
MouseDoubleClick="dataGridViewMyGroups_MouseDoubleClick">
</DataGrid>
这是一个用于更改背景颜色的C#代码。
DataGridRow rowColor = (DataGridRow)dataGridViewMyGroups.ItemContainerGenerator
.ContainerFromIndex(number);
rowColor.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(223, 227, 238));
当我点击开始Button
并且每个Background
更改Row
颜色时,此代码有效。问题是当我点击取消Button
然后点击继续Button
,然后我得到了NullReferenceException
。继续按钮仅检查DataBase Table
中的最后一个ID。
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++;
}
继续Button
工作的代码,除了更改行的背景。
更新:
取消代码Button
:
if (MessageBox.Show("Do you want to stop posting?", "Confirmation",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
tokenSource.Cancel();
}
继续代码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);
}
答案 0 :(得分:-1)
好的,我想我现在看到你的问题了。它在你自己的代码中:
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++;
}
您的foreach循环运行的次数与DataGrid中的行数一样多。但在某些情况下,根据您的逻辑,在内部增加number
变量两次。也就是说,如果你进入if
语句,你会增加一次,然后再循环结束。因此,无论何时进入if
语句,都会增加行计数两次。
这就是为什么您的计数器的值高于实际的行数。您需要删除if
语句中的增量。
答案 1 :(得分:-2)
我解决了这个问题。所以问题是因为我再次使用Data填充DataGrid
。相反,我没有填写它我只获得ID,我上次停在该表中有一些ID。
Button
代码继续:
int postID;
string fbGroupID;
int listID = int.Parse(cmbSelectList.SelectedValue.ToString());
using (OleDbConnection connection = new OleDbConnection(conn))
{
................
commName.CommandText = "SELECT TOP 1 [FaceBookGroupID],[PostID] FROM [Advertiseing] WHERE [email] = @email AND [ListForGroupID]= @listID ORDER BY [Date-Time] DESC";
................
postID = list[0].PostID;
fbGroupID = list[0].FBGroupID;
*listID = list[0].ListForGroupID;* //Deleted
}
//cmbSelectList.SelectedValue = listID; - Deleted
cmbSavedPosts.SelectedValue = postID;
//loadGroupsInList(); - Deleted
// Next part of code are the same
................