如何在用户对 gridview 中的列进行排序后保持格式

How to maintain formatting after user sort columns in gridview

当我从数据库中获取数据时,行是用颜色编码的。但是,当用户单击列进行排序时,颜色格式将被丢弃,所有行都变为白色。我搜索了答案,发现一些人和我有同样的问题。他们已经实现了某种事件处理程序(例如 DataBindingCompleteCellFormatting)以保持或 re-instantiate排序后格式化。但是我没有让这个工作。谁能解释一下原因,或者告诉我解决这个问题的另一种方法?

这是从数据库中获取数据并填充 gridview 的代码

public static OdbcConnection DbConnection; // Create an object for the DB connection  
public static MainWindow mw = Form.ActiveForm as MainWindow;

public static void TestSqlToGridView()
{
    // https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.datasource?view=netframework-4.7.1

    //var mw = Form.ActiveForm as MainWindow;
    ConnectToDB();

    DbConnection.Open();

    BindingSource bindingSource = new BindingSource();

    // Automatically generate the DataGridView columns.
    SuspendDrawing(mw.dataGridView); // wait with drawing until all data is read
    bindingSource.DataSource = GetData( Laddstatus() );
    mw.dataGridView.DataSource = bindingSource;

    SetRowColor(); // Change the rows color           

    mw.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader; // Adjusting the size of header cells !!! AllCells = SLOW !!!
    ResumeDrawing(mw.dataGridView); // draw all cells

    // Set the DataGridView control's border.
    mw.dataGridView.BorderStyle = BorderStyle.Fixed3D;

    DbConnection.Close();
}

这是我尝试重新开始格式化的方式

void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    // This event is raised when the contents of the data source 
    // change or when the value of the DataSource, DataMember, or BindingContext 
    // property changes.

    Print("DatabindingComplete!"); //DEBUG
    SetRowColor();
}

但出于某种原因,当我按 headers 列进行排序时,似乎从未调用过该事件。我必须放在特定位置吗?

感谢您的帮助!

如您所言:

Private Sub dgwList_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles dgwList.DataBindingComplete
    Call ColorMyRows()
End Sub

C#:

Private void dgwList_DataBindingComplete(Object sender, DataGridViewBindingCompleteEventArgs e)
{
    ColorMyRows();
}

这是我的一种方法,只要您使用 DataSource 填充 DataGridView,它就可以工作。

但是第二次查看您的代码,您有 SuspendDrawing,然后您 进行数据绑定 然后您再次 ResumeDrawing! 这将禁用此事件

[I]t seem[s] like the event is never called. Do I have to put in a specific location?

是-差不多。您已经添加了事件处理程序,但您需要 subscribe (attach) to the event。这可以通过多种方式完成:

  1. 在设计模式下双击Properties -> Events下的DataBindingComplete事件:

    这会为您的 Form.cs 文件存根一个空处理程序,并附加到 Form.designer.cs 文件中的事件:

    this.dataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView1_DataBindingComplete);
    
  2. 以编程方式 在表单构造函数或 Load 事件中:

    this.dataGridView1.DataBindingComplete += this.dataGridView1_DataBindingComplete;
    

这应该会给你想要的结果:


为什么?

通过订阅事件,您将在基本事件完成后将处理程序附加到 运行。否则,它永远不会被调用。可以多次订阅多个处理程序。例如,以下内容:

this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete1;
this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete2;
this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete3;
this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete1;

private void DataGridView1_DataBindingComplete1(object sender, DataGridViewBindingCompleteEventArgs e)
{
    Console.WriteLine("First");
}

private void DataGridView1_DataBindingComplete2(object sender, DataGridViewBindingCompleteEventArgs e)
{
    Console.WriteLine("Second");
}

private void DataGridView1_DataBindingComplete3(object sender, DataGridViewBindingCompleteEventArgs e)
{
    Console.WriteLine("Third");
}

每次 dataGridView1.DataBindingComplete 触发时都会产生以下输出:

/*
First
Second
Third
First
*/

注意只订阅 (+=) 一次事件 - 否则它可能会产生奇怪的结果、资源泄漏、and/or 拖延您的 运行 时间(例如,当expensive/large 处理程序重复附加)。这可以通过取消订阅 (-=) 事件来解决。