cihadakt
cihadakt

Reputation: 3214

Event firing continuously

I wrote a method which changes backcolor of the rows before painting gridview in devexpress. It works fine but I realized that my code begins slowing down. Then I've found that the event firing continuously. It never stops. How can I handle this? Is there any way to stop firing event manually after gridview painted or should I try to solve this problem with an another event or another method???

Here is my event:

private void gvStep_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
        try
        {
            DataRowView drw = (DataRowView)gvStep.GetRow(e.RowHandle);
            byte actionTypeID = (byte)drw.Row["ActionType"];

            //string colorCode = (new DivaDs()).GetBackColor(actionTypeID);
            string colorCode = divaDs.GetBackColor(actionTypeID);
            Color backColor = ColorTranslator.FromHtml(colorCode);
            e.Appearance.BackColor = backColor;                
        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message);   
        }

    }

public string GetBackColor(byte actionTypeID)
    {
        string color = string.Empty;
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[DivaSqlSiteConnString].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(@"Select BackColor from ActionTypes where ID = @actionTypeID"))
            {
                SqlParameter param = new SqlParameter("@actionTypeID", actionTypeID);

                cmd.Parameters.Add(param);
                cmd.Connection = conn;
                conn.Open();
                color = cmd.ExecuteScalar().ToString();
                conn.Close();
            }
        }
        return color;
    }

Upvotes: 1

Views: 1622

Answers (3)

bernhof
bernhof

Reputation: 6320

My best guess is that some part of your code is just really slow.

The event only fires for each visible cell in the grid. If you attempt to debug the event, focus will shift to the debugger, and when you return to the application the cells need to be redrawn, causing the event to fire again, thus giving the impression that the event fires continuously. It does not, however.

Here are some pointers to improve performance:

  • You are constructing a new DivaDs every time the event fires
    • Instead, consider reusing the same instance of the class as a member variable
    • What happens in the constructor?
  • Take a closer look at the GetBackColor method or ColorTranslator.FromHtml and see if any modifications can be made to improve performance.

Update

It appears you are querying the database for each cell in the grid. This is a really bad idea.

A simple solution would be to preload all ActionTypes and their background colors (or at least the subset of ActionTypes that is displayed in the grid) before setting the grid's data source.

// member variable
private Dictionary<byte, Color> actionTypeColorDict;

void BuildActionTypeColorDictionary()
{
    string connectionString = ConfigurationManager
      .ConnectionStrings[DivaSqlSiteConnString].ConnectionString;

    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand cmd = conn.CreateCommand())
    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
    {
       // load all action type IDs and corresponding background color:
       cmd.CommandText = @"SELECT ActionTypeID, BackColor FROM ActionTypes";       
       DataTable actionTypeTable = new DataTable();
       adapter.Fill(actionTypeTable);

       // build a dictionary consisting of action type IDs
       // and their corresponding colors
       actionTypeColorDict = actionTypeTable.AsEnumerable().ToDictionary(
           r => r.Field<byte>("ActionTypeID"),
           r => ColorTranslator.FromHtml(r.Field<string>("ColorCode")));
   }
}

Call the BuildActionTypeColorDictionary method before setting the data source of the grid. In the RowStyle or CustomDrawCell events, use the new dictionary member to determine the background color. See the following modified version of your RowStyle code:

private void gvStep_RowStyle(object sender,DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
    try
    {
        DataRow row = gvStep.GetDataRow(e.RowHandle);
        if (row == null)
            return;

        byte actionTypeID = row.Field<byte>("ActionImage");

        // look up color in the dictionary:
        e.Appearance.BackColor = actionTypeColorDict[actionTypeID];
    }
    catch (Exception ex)
    {
        XtraMessageBox.Show(ex.Message);
    }
}

Upvotes: 3

cihadakt
cihadakt

Reputation: 3214

Finally, I found it. RowStyle event only fires same time with gridview's row count

private void gvStep_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            try
            {
                DataRowView drw = (DataRowView)gridView1.GetRow(e.RowHandle);
                if (drw == null)
                    return;

                byte actionTypeID = (byte)drw.Row["ActionImage"];

                string colorCode = divaDs.GetBackColor(actionTypeID);
                Color backColor = ColorTranslator.FromHtml(colorCode);
                e.Appearance.BackColor = backColor;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

Upvotes: 0

E.T.
E.T.

Reputation: 944

How do you know it's firing continuously? Are you debbuging?

This code runs whenever the grid is redrawn, meaning whenever the form gets focus.

  1. This event runs for each cell - so it will run quite a few times.
  2. If you put a break-point in this event you'll never get out of it. It will break, you will debug, when it's done it will return focus to the form - causing the form to be redrawn using this event and the break-point is reached again.

And just a side note - Whenever I use that event I have to put e.Handled = true; in the code so that the cell isn't "drawn" by anyone but me :)

Upvotes: 1

Related Questions