设置datagrid行背景颜色WPF-循环 [英] Set datagrid row background color WPF - Loop

查看:632
本文介绍了设置datagrid行背景颜色WPF-循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Task会抛出DataGridRow并执行某些任务.当他完成操作时,为该行设置background color.我添加了取消button来停止任务,并继续button来继续上次完成的地方.除更改行的背景颜色外,所有其他功能均完美无缺.

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.

这是XAML代码,我是WPF的新手,因此对于DataGrid

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>

这是用于更改背景颜色的C#代码.

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)); 

当我单击开始Button时,此代码有效,并且每个Row都会更改Background颜色.问题是当我单击取消Button然后单击继续Button时,我得到了NullReferenceException. 继续"按钮仅检查DataBase Table中的最后一个ID.

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++;
}

继续执行Button工作的代码,但更改行的背景除外.

Code for continue Button work except changing row's background.

更新: 取消代码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);
        }

推荐答案

我解决了这个问题.所以问题是因为我再次用数据填充了DataGrid.取而代之的是,我没有填充它,而只获得了我上次在该表中停止使用的ID.

I resolved this problem. So the problem is because I fill DataGrid again with Data. Instead that I don't fill it I only get ID where I stopped from last time in that Table with some 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
  ................

这篇关于设置datagrid行背景颜色WPF-循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆