简体   繁体   中英

changing color of particular row in datagrid C# windows ce compact framework

I am developing ac# application on my windows ce handheld with compact framework 2.0 . I have an grid like one below

name        location  
----------------------
John          US
Flex          UK    

I have used datagrid for the grid formation . Note : DataGrid(new System.Windows.Forms.DataGrid()) not DataGridView . There is an text box above the grid. If the user keys in uk in the text box i have to change the color of the second row in the datagrid. How do i acheive it ??

 private System.Windows.Forms.DataGrid scnDataGrd = new System.Windows.Forms.DataGrid();

I have addded an coloumn named status . And i want to display an image over there.If the text from the textbox matches.

Adding coloumns to tables and filling values based on csv :

if (data.StartsWith("Coloumns"))
        data = data.Substring(index + 1, data.Length - (index + 1));
    data = data.Trim(); 
    string[] values = data.Split(',');
    // Add the index on the column on which data is stored in csv
    List<int> csvIndex = new List<int>();
    for (int i = 0; i < values.Length; i++)
    {
        values[i] = values[i].Trim();
        if (values[i] != "Status")
            csvIndex.Add(csvHeaders.IndexOf(values[i]));                        
    }

    rfidindex = (csvHeaders.IndexOf("RFID") == -1) ? csvHeaders.IndexOf("rfid") : csvHeaders.IndexOf("RFID");
    //Image myImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("search.png"));
     //ImageConverter imageConverter = new ImageConverter();
    Image img = AssetGatherMobileQuickTraQ.Properties.Resources.search;
        //Properties.Resources.my_image;
    string csvdata = strRdr.ReadToEnd();
    csvdata = csvdata.Replace('\r', ' ');
    string[] tagDets = csvdata.Split(new char[] { '\n' });
    csvItems = new Hashtable();
    DataTable dataTable1 = (DataTable)scnDataGrd.DataSource;

    for (int i = 0; i < tagDets.Length; i++)
    {
        string[] individTagDet = tagDets[i].Split(new char[] { ',' });

        if (individTagDet.Length <= 1)
            break;

        csvItems.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim(), individTagDet);                                                                       
        DataRow row = dataTable1.NewRow();
        row.BeginEdit();
        //System.Drawing.Image imgTest = System.Drawing.Image. FromFile("C:\\Test.jpg");
        //System.Drawing.Image img = System.Drawing.Image.ReferenceEquals
        //dataTable1.Columns
        Bitmap image1 = new System.Drawing.Bitmap(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\search.png");


        //scnDataGrd.Dara
        row[0] = image1;
        //DataGrid
        for (int j = 0; j < 3; j++)
        {
            row[j+1] = individTagDet[csvIndex[j]].Replace("\"",string.Empty).Trim();                            
        }
        //scnDataGrd.TableStyles

        csvRfidList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
        missingTagList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());

        row.EndEdit();
        dataTable1.Rows.Add(row);
        scnDataGrd.DataSource = dataTable1;
    }

If the text maches it arrives here :

DataTable dataTable = (DataTable)scnDataGrd.DataSource;
int index1 = -1;
bool found = false;
DataGridTableStyle ts = new DataGridTableStyle();


foreach (DataRow dr in dataTable.Rows)
{
    index1++;
    if (dr[tableColIndex].ToString() == textValue)
    {
        scnDataGrd.Select(index1);
        scnDataGrd.SelectionForeColor = Color.Red;
        break;
    }
    else {

        dr[0] = "Not Found";
    }
}

In Status Coloumn, Instead of image it displays as System.Data.Bitmap

try this for change backColor

        int index = -1;
        bool found = false;

        foreach (DataRow dr in myDataSet.Tables[0].Rows)
        {
            index++;
            string d = dr["location"].ToString();
            if (dr[0].ToString() == txtbox1,Text)
            {
                found=true;
                break;
            }
        }
        if(found)
        {
            scnDataGrd.Select(index);
            scnDataGrd.SelectionBackColor = Color.Blue;
        }

if you want to change fore color replace SelectionBackColor to SelectionForeColor . Here myDataSet is a binding element of DataGrid from this first we find location of Data, after that select that row from

scnDataGrd.Select(index);

and change colour of selecting row from

scnDataGrd.SelectionForeColor = Color.Blue;

hope it helps.

EDIT

you can update font color by replacing

scnDataGrd.SelectionBackColor = Color.Blue;

to

scnDataGrd.SelectionForeColor = Color.Blue;

you can't give image like that, you have to make custom image view for that, but if you want thisfrom check box, you can implement like that :

if (data.StartsWith("Coloumns"))
    data = data.Substring(index + 1, data.Length - (index + 1));
data = data.Trim(); 
string[] values = data.Split(',');
// Add the index on the column on which data is stored in csv
List<int> csvIndex = new List<int>();
for (int i = 0; i < values.Length; i++)
{
    values[i] = values[i].Trim();
    if (values[i] != "Status")
        csvIndex.Add(csvHeaders.IndexOf(values[i]));                        
}

rfidindex = (csvHeaders.IndexOf("RFID") == -1) ? csvHeaders.IndexOf("rfid") : csvHeaders.IndexOf("RFID");
//Image myImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("search.png"));
 //ImageConverter imageConverter = new ImageConverter();
Image img = AssetGatherMobileQuickTraQ.Properties.Resources.search;
    //Properties.Resources.my_image;
string csvdata = strRdr.ReadToEnd();
csvdata = csvdata.Replace('\r', ' ');
string[] tagDets = csvdata.Split(new char[] { '\n' });
csvItems = new Hashtable();
DataTable dataTable1 = (DataTable)scnDataGrd.DataSource;

for (int i = 0; i < tagDets.Length; i++)
{
    string[] individTagDet = tagDets[i].Split(new char[] { ',' });

    if (individTagDet.Length <= 1)
        break;

    csvItems.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim(), individTagDet);                                                                       
    DataRow row = dataTable1.NewRow();
    row.BeginEdit();
    //System.Drawing.Image imgTest = System.Drawing.Image. FromFile("C:\\Test.jpg");
    //System.Drawing.Image img = System.Drawing.Image.ReferenceEquals
    //dataTable1.Columns
    Bitmap image1 = new System.Drawing.Bitmap(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\search.png");


    //scnDataGrd.Dara
    row[0] = image1;
    //DataGrid
    for (int j = 0; j < 3; j++)
    {
        row[j+1] = individTagDet[csvIndex[j]].Replace("\"",string.Empty).Trim();                            
    }
    //scnDataGrd.TableStyles

    csvRfidList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());
    missingTagList.Add(individTagDet[rfidindex].Replace("\"", string.Empty).Trim());



    row.EndEdit();
    dataTable1.Rows.Add(row);

    DataColumn cCurrent = new DataColumn("Current", typeof(bool));
    dataTable1.Rows.Add(cCurrent);
    scnDataGrd.DataSource = dataTable1;
}

and in your criteria, where you make condition implement like this..

 DataTable dataTable = (DataTable)scnDataGrd.DataSource;
        int index1 = -1;
        bool found = false;
        DataGridTableStyle ts = new DataGridTableStyle();


        foreach (DataRow dr in dataTable.Rows)
        {
            index1++;
            if (dr[tableColIndex].ToString() == textValue)
            {
                scnDataGrd.Select(index1);
                scnDataGrd.SelectionForeColor = Color.Red;

                dr["Current"] = true;
                break;
            }
            else
            {

                dr[0] = "Not Found";
            }
        }

where current in

dr["Current"] = true;

is DataColumn which we implemented before in third last line. on previous code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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