gpt4 book ai didi

c# 使用不带 BindingSource 的高级 datagridview (ADGV) 过滤器

转载 作者:行者123 更新时间:2023-12-05 01:42:04 26 4
gpt4 key购买 nike

我正在使用找到的高级 DataGridView (ADGV) here为我的应用程序添加过滤功能。

过滤或排序代码如下:

private void advancedDataGridView1_SortStringChanged(object sender, EventArgs e)
{
this.stockHistoryBindingSource.Sort = advancedDataGridView1.SortString;
}

private void advancedDataGridView1_FilterStringChanged(object sender, EventArgs e)
{
this.stockHistoryBindingSource.Filter = advancedDataGridView1.FilterString;
}

但我不能使用它,因为在我的项目中我正在读取一个 XML 文件并使用以下代码将其绑定(bind)到我的 ADGV:

void QueryFoos()
{
IEnumerable<FooViewData> query =
from foo in XmlFiles.FOO.Root.Descendants("foo")
select new FooViewData
{
ID = Convert.ToInt32(foo.Attribute("id").Value),
Num = Convert.ToInt32(foo.Attribute("num").Value),
...
};

advancedDataGridView1.DataSource = query.OrderBy(n => n.ID).ThenBy(r => r.Num).ToList();
}

我试过这样的代码,但我并不惊讶它会在我面前抛出异常:

BindingSource x = (BindingSource)this.advancedDataGridView1.DataSource;
x.Filter = advancedDataGridView1.FilterString;
this.advancedDataGridView1.DataSource = x;

是否有一些解决方法可以使用 ADGV 的过滤和排序?

最佳答案

事实证明,我今天遇到了同样的问题,正在寻找解决方案。基本上,问题在于 ADGV 是为与 DataTable 而不是对象列表一起编写的。

此解决方案适用于我,但您的情况可能会有所不同。

我最后做的是使用 dynamic linq自己对对象列表执行过滤器。破解部分是我转换 ADGV 生成的过滤器字符串并将其转换为动态 linq 期望的字符串。

我们从一些数据开始。我有一个名为 DataPointGridViewModel 的类,如下所示:

public class DataPointGridViewModel 
{
public int DataPointId { get; set; }
public string Description { get; set; }
public bool InAlarm { get; set; }
public DateTime LastUpdate { get; set; }
public double ScalingMultiplier { get; set; }
public decimal Price { get; set; }
}

数据可以是任何东西。这是您将在网格中过滤的数据。显然,您将拥有自己的数据类。您需要将此 DataPointGridViewModel 类替换为您自己的模型/数据对象。

现在,这是您需要添加的代码示例代码。我在 github 上也有一个示例项目:我在 github 上有此代码的工作版本:here

这是您需要添加的代码:

List<DataPointGridViewModel> m_dataGridBindingList = null;
List<DataPointGridViewModel> m_filteredList = null;

private void dataGridView2_FilterStringChanged(object sender, Zuby.ADGV.AdvancedDataGridView.FilterEventArgs e)
{
try
{
if ( string.IsNullOrEmpty(dataGridView2.FilterString) == true )
{
m_filteredList = m_dataGridBindingList;
dataGridView2.DataSource = m_dataGridBindingList;
}
else
{
var listfilter = FilterStringconverter(dataGridView2.FilterString);

m_filteredList = m_filteredList.Where(listfilter).ToList();

dataGridView2.DataSource = m_filteredList;
}
}
catch (Exception ex)
{
Log.Error(ex, MethodBase.GetCurrentMethod().Name);
}
}

这是将 ADGV 过滤器字符串转换为 Dynamic Linq 过滤器字符串的函数:

private string FilterStringconverter(string filter)
{
string newColFilter = "";

// get rid of all the parenthesis
filter = filter.Replace("(", "").Replace(")", "");

// now split the string on the 'and' (each grid column)
var colFilterList = filter.Split(new string[] { "AND" }, StringSplitOptions.None);

string andOperator = "";

foreach (var colFilter in colFilterList)
{
newColFilter += andOperator;

// split string on the 'in'
var temp1 = colFilter.Trim().Split(new string[] { "IN" }, StringSplitOptions.None);

// get string between square brackets
var colName = temp1[0].Split('[', ']')[1].Trim();

// prepare beginning of linq statement
newColFilter += string.Format("({0} != null && (", colName);

string orOperator = "";

var filterValsList = temp1[1].Split(',');

foreach (var filterVal in filterValsList)
{
// remove any single quotes before testing if filter is a num or not
var cleanFilterVal = filterVal.Replace("'", "").Trim();

double tempNum = 0;
if (Double.TryParse(cleanFilterVal, out tempNum))
newColFilter += string.Format("{0} {1} = {2}", orOperator, colName, cleanFilterVal.Trim());
else
newColFilter += string.Format("{0} {1}.Contains('{2}')", orOperator, colName, cleanFilterVal.Trim());

orOperator = " OR ";
}

newColFilter += "))";

andOperator = " AND ";
}

// replace all single quotes with double quotes
return newColFilter.Replace("'", "\"");
}

...最后排序函数如下所示:

private void dataGridView2_SortStringChanged(object sender, Zuby.ADGV.AdvancedDataGridView.SortEventArgs e) 
{
try
{
if (string.IsNullOrEmpty(dataGridView2.SortString) == true)
return;

var sortStr = dataGridView2.SortString.Replace("[", "").Replace("]", "");

if (string.IsNullOrEmpty(dataGridView2.FilterString) == true)
{
// the grid is not filtered!
m_dataGridBindingList = m_dataGridBindingList.OrderBy(sortStr).ToList();
dataGridView2.DataSource = m_dataGridBindingList;
}
else
{
// the grid is filtered!
m_filteredList = m_filteredList.OrderBy(sortStr).ToList();
dataGridView2.DataSource = m_filteredList;
}
}
catch (Exception ex)
{
Log.Error(ex, MethodBase.GetCurrentMethod().Name);
}
}

最后,您需要来自 here 的动态 Linq 库

您可以使用 Nuget 将其引入您的项目中:

Install-Package System.Linq.Dynamic

关于c# 使用不带 BindingSource 的高级 datagridview (ADGV) 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com