简体   繁体   中英

Changing Row Back Color on DevExpress GridView on condition

I have two grid view and a button on a click of button I copy data from gridView1 to gridView2 This is my code:

private async void btnAdd_Click(object sender, EventArgs e)
    {
        int[] selectedRows = gridView1.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)

        {
            DataRow rowGridView1 = (gridView1.GetRow(selectedRows[i]) as DataRowView).Row;
            for (int j = 0; j < gridView3.RowCount; j++)
            {
                if (rowGridView1["BS"].ToString() == gridView3.GetRowCellValue(j, "ProjectN").ToString() &&
                    rowGridView1["Repère"].ToString() == gridView3.GetRowCellValue(j, "Parts").ToString())
                {
                    DataRow row = dt.NewRow();
                    row[0] = rowGridView1["BS"];
                    row[1] = gridView3.GetRowCellValue(j, "Parts");
                    row[2] = gridView3.GetRowCellValue(j, "Profile");
                    row[3] = rowGridView1["Quantité"];

                    dt.Rows.Add(row);
                }
                else if(j == (gridView3.RowCount -1)) 
                {
                    gridView1.RowStyle += (senderr, ee) => {

                        if (gridView1.GetFocusedDataRow() == rowGridView1)
                        {
                            ee.Appearance.BackColor = Color.Red;
                            ee.HighPriority = true;
                        }
                    };
                }
            }
        }

I want to change the back color of gridView1 rows that do not match the condition so i can verified them
Unfortunately, all rows become red.
how can I fix this problem.
Thanks in advance

Answer by Giannis Paraskevopoulos

Here code modified, it is done with notepad and it remains the test.

First the background color of gridview1 that must be changed according to the values of "BS" and "repère", this is done in the RowStyle function.

private void gridView1_RowStyle(object sender, RowStyleEventArgs e)  {  

    If (e.RowHandle = this.gridView1.FocusedRowHandle)  
    {
       string strBS = gridView1.GetRowCellValue(row, "BS");
       string strRepere = gridView1.GetRowCellValue(row, "Repère");
        if (SearchRow(strBS,strRepere)==true)
         {
           e.Appearance.BackColor = Color.Red  
           e.HighPriority = True  
          }
    }
}

private bool SearchRow(string strBS, string strRepere)  {  
    bool RowExist=false;
    for (int j = 0; j < gridView3.RowCount; j++)
    {
      if (gridView3.GetRowCellValue(j, "ProjectN").ToString() ==strBS &&
        gridView3.GetRowCellValue(j, "Parts").ToString()==strRepere)
      {
         RowExist=true;
         Break;
      }
    }
   return RowExist;    
}

Adding gridview1 Rows to gridview3

private async void btnAdd_Click(object sender, EventArgs e)
{
        int[] selectedRows = gridView1.GetSelectedRows();
        for (int i = 0; i < selectedRows.Length; i++)
        {
            DataRow rowGridView1 = (gridView1.GetRow(selectedRows[i]) as DataRowView).Row;
            for (int j = 0; j < gridView3.RowCount; j++)
            {
                if (rowGridView1["BS"].ToString() == gridView3.GetRowCellValue(j, "ProjectN").ToString() &&
                    rowGridView1["Repère"].ToString() == gridView3.GetRowCellValue(j, "Parts").ToString())
                {
                    DataRow row = dt.NewRow();
                    row[0] = rowGridView1["BS"];
                    row[1] = gridView3.GetRowCellValue(j, "Parts");
                    row[2] = gridView3.GetRowCellValue(j, "Profile");
                    row[3] = rowGridView1["Quantité"];

                    dt.Rows.Add(row);
            dt.AcceptChanges();
                }
            }
       }

       gridView1.Datasource=dt;
       grid.DataBind();
}

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