在UI提交时,DataGridView与冻结列绑定

时间:2011-09-22 15:07:48

标签: c# winforms data-binding datagridview

我有一个带有datagridview的表单,我需要在用户完成搜索时填充该表单。在执行搜索之前,我不会将数据绑定到网格,因为绑定源取决于它们执行的搜索。

我有以下代码通过调用存储过程来执行数据加载:

private void SearchData()
{
    string returnMessage = string.Empty;
    DateTime dateSelected = this.DatePicker.Value.Date;

    // clear the DataGridView prior to running the search
    DataGridView.DataSource = null;

    switch (m_QueueSelected)  // this is grabbed via the search criteria combobox
    {
        case 70:    //  search 1
            Data.SearchOneTableAdapter.Fill(DataSet.spSearchOne, ref returnMessage, m_QueueSelected, dateSelected);

            SearchOneBindingSource.DataSource = DataSet.spSearchOne;
            DataGridView.DataSource = SearchOneBindingSource;
            break;
        case 80:    // search 2
            Data.SearchTwoTableAdapter.Fill(DataSet.spSearchTwo, ref returnMessage, m_QueueSelected, dateSelected);

            SearchTwoBindingSource.DataSource = DataSet.spSearchTwo;
            DataGridView.DataSource = SearchTwoBindingSource;
            break;
        default:
            Data.SearchDefaultTableAdapter.Fill(DataSet.spSearchDefault, ref returnMessage, m_QueueSelected, dateSelected);

            SearchDefaultBindingSource.DataSource = DataSet.spSearchDefault;
            DataGridView.DataSource = SearchDefaultBindingSource;
            break;
    }
}

在我对网格进行数据绑定后,我对列执行了一些格式化,包括使网格中的第一列冻结,这样当它们滚动到第一列时将始终显示。我收到一条错误消息,上面写着“you cannot load a frozen column after another frozen column”之类的内容因为我使用了多个绑定源,所以我发现在加载任何其他数据源之前我需要首先删除数据源。所以我使用上面的代码:

DataGridView.DataSource = null;

这解决了冻结列错误消息。但是,昨天我实施了column header filter,这导致了一些问题。使用DataBindingComplete事件

构建过滤器并将其应用于列
private void DataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    string filterStatus = DataGridViewFilterableColumnHeaderCell.GetFilterStatus(DataGridView);
    if (!String.IsNullOrEmpty(filterStatus))
    {
        RecordCountLabel.Text = filterStatus;
    }
}

我遇到的问题是,因为我更改了DataGridView.DataSource = null,所以没有用于过滤器识别的列/单元,当我尝试执行其他搜索时它失败。如果我停止使列冻结,那么我不需要将数据源设置为null,我没有问题。

长话短说,有人知道如何重复使用带有多个绑定源的datagridview并使用冻结列而不必将数据源设置为null?

0 个答案:

没有答案