简体   繁体   English

如何在DataGrid C#Windows CE中更改不同行的颜色

[英]How to change color of different rows in datagrid c# windows ce

I try to develop ac# application(gathering products by their barcodes) to windows ce handheld device with compact framework 3.5 我尝试将AC#应用程序(通过条形码收集产品)开发到具有紧凑框架3.5的Windows CE手持设备

I have a datagrid and datagrid is bind with a datatable by sql. 我有一个datagrid,并且datagrid通过sql与datatable绑定。 There are 4 columns in my datagrid, 3rd column represents the quantity of the products needs to be collected, and last column comes with the default value 0(quantity collected). 我的数据网格中有4列,第3列代表需要收集的产品数量,最后一列带有默认值0(收集的数量)。 Whenever user enters a product code, quantity of the product increases 1 by 1. 每当用户输入产品代码时,产品数量就会增加1。

I want to make background color of the row blue when user enters corresponding product code(to show which product is being collected) 当用户输入相应的产品代码时,我想将该行的背景颜色设为蓝色(以显示正在收集的产品)

and also I want to make background color green if user collects the needed products. 如果用户收集了所需的产品,我还想将背景色设为绿色。

I tried coloring row by selected index but it does not work. 我尝试按选定的索引为行着色,但是它不起作用。 When selection gone , colours are gone. 选择消失后,颜色消失。

Here is a picture of screen when needed quantity of product was collected. 这是收集所需数量产品时的屏幕图片。 在此处输入图片说明

Here is when I want to see the processed row. 这是我想查看已处理行的时间。 在此处输入图片说明

Below is my code of the keypress event of the textbox(entering of product code) 下面是我的文本框keypress事件的代码(输入产品代码)

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

I couldnt find many info about this. 我找不到有关此的许多信息。 Any help will be important. 任何帮助都很重要。 Thanks for any help! 谢谢你的帮助!

First of all I experienced the same problem. 首先,我遇到了同样的问题。 Make use of DataGridFormatCellEventArgs for coloring the solution. 利用DataGridFormatCellEventArgs为解决方案着色。

explained here Add the DataGrid file to your code in the link. 在此处解释将DataGrid文件添加到链接中的代码中。 ( DataGridFormatCellEventArgs.cs and FormattableTextBoxColumn.cs ) These files contain the Paint class that was used to do the coloring. DataGridFormatCellEventArgs.cs和FormattableTextBoxColumn.cs )这些文件包含用于进行着色的Paint类。

different example 不同的例子

I hope I could help. 希望我能帮上忙。 If you experience difficulties, I can give an example from my own code. 如果您遇到困难,我可以举一个自己的代码举例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM