簡體   English   中英

C# 如何從外部觸發 DataGridView CellFormatting 事件?

[英]C# how to fire DataGridView CellFormatting event from outside?

我有一個 DGV,它對所有列中的值都有一些條件格式,但前 N 列(項目列)。 前 N 列(類別列)具有表格其余部分的參考值。 類似的東西:

Category1 Category2 Item1 Item2 Item3 Item4
1         2         1     2     1     2
56        57        57    56    56    56

我還有一個字典,用於設置所有列的標題與參考列的標題的對應關系。

{Item1, Category1}
{Item2, Category1}
{Item3, Category1}
{Item4, Category2}

將 Items1-4 的每個單元格與 CellFormatting 事件處理程序中的相應類別(使用字典)進行比較,然后如果匹配,則將單元格着色為綠色,否則為紅色。

換句話說,在 CellFormatting 事件的處理程序中,我使用字典來檢查特定列應該從 N 引用列中具有哪些值。

現在我有一個完全獨立的控件(另一個帶有組合框的 DGV),它允許用戶更改該字典(切換每個項目所屬的類別)。 更改字典時如何手動引發 CellFormatting 事件?

這是我的單元格格式事件處理程序:

        private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            try
            {

                if (e.ColumnIndex > N_of_ReferenceColumns)
                {

                    if (e.Value.ToString() == this.dataGridView.Rows[e.RowIndex].Cells[dataGridView.Columns[convertItemToCategory(dataGridView.Columns[e.ColumnIndex].Name)].Index].Value.ToString())
                    {

                        e.CellStyle.BackColor = Color.Green;
                        e.CellStyle.SelectionBackColor = Color.DarkGreen;
                    }
                    else
                    {
                        e.CellStyle.BackColor = Color.Red;
                        e.CellStyle.SelectionBackColor = Color.DarkRed;
                    }
                }
            }
            catch
            { }

        }

這是我在 Combobox 中為每個項目更改 Category 值的處理程序:

        private void DictionarydataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateDictionary(DictionarydataGridView.Rows[e.RowIndex].Cells[1].Value.ToString(), DictionarydataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
        }

這就是我更新字典以及在條件格式邏輯中使用它的方式:

    public static IDictionary<string, string> Dictionary = new Dictionary<string, string>();

        public void UpdateDictionary(string key, string value)
        {
            Dictionary[key] = value;

        }
        public static string convertItemToCategory(string key)
        {
            string value = "";
            if (Dictionary.TryGetValue(key.ToUpper(), out value))
            {
                return value;
            }
            else
            {
                return key.ToUpper();
            }

        }

我需要做的是當我更新字典時,還引發 CellFormatting 事件,以便根據新選擇更新條件格式。

一種方法是將更新邏輯打包到一個單獨的方法中,然后從每個事件處理程序單獨調用它,我不確定如何處理所有 e.CellStyle.BackColor 等等......

有任何想法嗎?

一個永遠不會失敗的快速黑客是通過重置數據源來強制數據網格重新綁定到數據源。

該線程對以下主題進行了冗長的討論: C# refresh DataGridView when updates or insert on another form

您可以在表單上放置一個輔助方法:

public void Refresh()
{
    datagridview1.DataSource = datagridview1.DataSource; // should re-evaluate all logic related to data bindings
    datagridview1.Refresh(); // forces the control to repaint
}

這可能是最密集的解決方案,但它應該可以完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM