2017-08-01 202 views
0

我尝试开发C#应用程序(采集产品由条形码),以Windows CE的掌上设备与Compact Framework的3.5如何改变不同行的颜色在数据网格的C#Windows CE

我有一个DataGrid和DataGrid是绑定与数据表由SQL。我的数据网格中有4列,第3列代表需要收集的产品的数量,最后一列的默认值为0(收集的数量)。每当用户输入产品代码,产品的量由1

我想使行蓝色的背景颜色,当用户输入相应的产品代码(以显示被收集,其产物)增加1

和如果用户收集所需产品,我也想让背景颜色变成绿色。

我试着通过选定的索引着色行,但它不起作用。当选择消失后,颜色消失了。

以下是当需要的产品数量被收集时的屏幕图片。 enter image description here

这是当我想看到处理的行。 enter image description here

下面是我的文本框的按键事件的代码(输入的产品代码)

 private void txtUrunkod_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     foreach (System.Data.DataColumn col in dt.Columns) col.ReadOnly = false; 
     if (e.KeyChar == (char)Keys.Enter) 
     { 
      islemkod = txtUrunkod.Text; 
      islemkod.Trim(); 

      if (islemkod.Contains('/')) 
      { 
       serikodbol = islemkod.Split('/'); 
       urunKodum = serikodbol[0]; 
       DataRow row = dt.Select("urunkodu='" + urunKodum + "'").FirstOrDefault(); 
       int guncelle = Convert.ToInt32(row[3]); 
       guncelle++; 
       row[3] = guncelle; 
      } 
      else if (islemkod.Length == 8) 
      { 
       SqlCommand cmd = new SqlCommand("exec MY_TOPUK_BILGI_GETIR '" + islemkod + "'", conStr); 
       conStr.Open(); 
       SqlDataReader dr = cmd.ExecuteReader(); 
       uk = new DataTable(); 
       uk.Load(dr); 
       conStr.Close(); 
       //toplamaGrid.Select(0); 
       foreach (DataRow row2 in uk.Rows) 
       { 
        urunKodum = row2[0].ToString(); 
       } 
       DataRow row = dt.Select("urunkodu='" + urunKodum + "'").FirstOrDefault(); 
       int guncelle = Convert.ToInt32(row[3]); 
       guncelle++; 
       row[3] = guncelle; 
       int index = -1; 
       bool found = false; 

       foreach (DataRow datr in dt.Rows) 
       { 
        index++; 
        string d = datr["urunkodu"].ToString(); 
        if (datr[0].ToString() == urunKodum) 
        { 
         found = true; 
         break; 
        } 
       } 


       if (found && !row[2].Equals(row[3])) 
       { 

        toplamaGrid.Select(index); 
        toplamaGrid.SelectionBackColor = Color.Blue; 
       } 
       else if (row[2].Equals(row[3])) 
       { 
        toplamaGrid.Select(index); 
        toplamaGrid.SelectionBackColor = Color.Green; 
        //toplamaGrid.UnSelect(index); 
       } 



      } 

      else if (islemkod.Length == 7 && islemkod[0] == 'P') 
      { 

      } 

      else//islemkod.Length != 8 && !islemkod.Contains('/') 
      {// 
       urunKodum = txtUrunkod.Text; 
       txtUrunkod.Visible = false; 
       lblurunkod.Visible = false; 
       txtifAdres.Visible = true; 
       lbladressor.Visible = true; 
       txtifAdres.Focus(); 

      } 
      updated = true; 
      txtUrunkod.Text = ""; 
      sonindex = 0; 
     } 
    } 

我无法找到关于这个很多信息。任何帮助都很重要。谢谢你的帮助!

+0

复制https://stackoverflow.com/questions/408113/changing-row-colors-in-the-compact-framework-datagrid?rq=1。您需要在Paint事件中实现自定义绘图。 – josef

回答

0

首先,我遇到了同样的问题。使用DataGridFormatCellEventArgs为着色解决方案。

explained here将DataGrid文件添加到链接中的代码中。 (DataGridFormatCellEventArgs.cs和FormattableTextBoxColumn.cs)这些文件包含用于执行着色的Paint类。

different example

我希望我能帮上忙。如果你遇到困难,我可以从我自己的代码中举个例子。

相关问题