我正在构建一个必须显示从外部系统接收的数据的应用程序.这些数据可以非常快速地进入,而每行占用的字节数相对较小.这意味着每个时间单元必须添加许多行.我目前正处于这样一个阶段,我看到我收到的数据比我能处理的速度快,这意味着我的内存使用率正在上升.
我认为这很大一部分与绘制实际的dataGridView有关.我对dataGridView做了一些调整,希望它能提高性能.(例如禁用自动尺寸,特殊样式等)
在最近的一次添加中,我添加了行的颜色,这是必需的.目前我的申请表如下:
实际添加发生在具有2个参数的函数中:1.包含列(项)项的列表2.行的颜色.(颜色)
它看起来如下(半伪):
/* Store the color for the row in the color list so it is accessible from the event */
rowColors.Add(rowColor); //Class variable that stored the colors of the rows used in the DataGridCellFormatting event
/* Create the row that is to be added. */
ResultRow resultRow = new ResultRow();
foreach(item in items)
{
resultRow.Set(item); /* It's actually a dictionary because some fields are optional, hence this instead of a direct constructor call) …Run Code Online (Sandbox Code Playgroud) 我有一个User类型的BindingList,User对象有几个属性(UserName,Password等).所以我将一个事件处理程序绑定到BindingList.ListChanged事件,它在添加或删除用户时工作正常,但是,如果用户属性发生更改,它不会引发事件,是否有任何方法可以实现此目的?
bindingListUsers.Add(someUser); // This raises ListChangedEvent
bindingListUsers.Delete(someUser); // This raises ListChangedEvent
bindingListUsers[0].UserName = "Another user name"; // This does NOT raise the event
Run Code Online (Sandbox Code Playgroud) 为了让这个工作变得更好,我一直都很疯狂.
我的代码中有几个类.我将尝试在下面发布相关代码并尽可能缩短
public class ServerSettings
{
private BindingList<Server> serverList = new BindingList<Server>();
public ServerSettings()
{
}
private void readSettings()
{
string list = "/Settings/Server";
XmlNodeList Xn = settings.SelectNodes(list);
foreach (XmlNode xNode in Xn)
{
Server tmpSrv = new Server();
for (int i=0; i<xNode.ChildNodes.Count; i++)
{
if(xNode.ChildNodes[i].Name == "Name")
tmpSrv.Name = xNode.ChildNodes[i].InnerText;
else if(xNode.ChildNodes[i].Name == "Host")
tmpSrv.Host = xNode.ChildNodes[i].InnerText;
else if(xNode.ChildNodes[i].Name == "Username")
tmpSrv.Username = xNode.ChildNodes[i].InnerText;
else if(xNode.ChildNodes[i].Name == "Password")
tmpSrv.Password = xNode.ChildNodes[i].InnerText;
}
tmpSrv.ID = xNode.Attributes["ID"].Value;
serverList.Add(tmpSrv);
}
}
public …Run Code Online (Sandbox Code Playgroud) 我正在实现我自己的BindingList<T>支持排序.到目前为止一切运作良好,但我对如何实施感到困惑RemoveSortCore.文档不是很明确,它只说:
如果在派生类中实现排序,则删除ApplySortCore应用的任何排序
这是否意味着我应该恢复项目的原始顺序?这个MSDN文章中显示的实现只设置_isSorted为false,而没有实际恢复原始顺序,这使得它很无用恕我直言...
如果我希望能够恢复原始订单,我想我需要保留原始集合的副本(或者有更好的方法吗?).但是,如果我这样做,我如何处理集合的修改?
InsertItem,我应该在未分类的副本末尾添加新项吗?RemoveItem,我在已排序的集合中给出了索引.但这意味着如果我还想从未排序的集合中删除该项,我需要找到它的原始位置,这是一个O(n)操作,而RemoveItem通常期望是O(1)操作.同样的问题也适用于SetItem.你会怎么处理?欢迎任何建议
ListBox 对象绑定 BindingList<KeyValuePair<string, string>>
在SelectionChanged事件中,我需要将所选项目作为 KeyValuePair<string, string>
以下代码给出错误,因为KeyValuePair不能用作引用类型.
KeyValuePair<string, string> selectedProperty = listProperties.SelectedItem as KeyValuePair<string, string>;
Run Code Online (Sandbox Code Playgroud)
什么是好的解决方法?
我有一个DataGridView,我将它绑定到一个BindingList<KeyValuePair<string, float>>.这是代码的相关部分:
dgv.AutoGenerateColumns = false;
DataGridViewTextBoxColumn firstColumn = new DataGridViewTextBoxColumn();
firstColumn.DataPropertyName = "Key";
firstColumn.HeaderText = "First Column";
DataGridViewTextBoxColumn secondColumn = new DataGridViewTextBoxColumn();
secondColumn.DataPropertyName = "Value";
secondColumn.HeaderText = "Second Column";
secondColumn.ReadOnly = false;
secondColumn.ValueType = typeof(float);
dgv.Columns.Add(firstColumn);
dgv.Columns.Add(secondColumn);
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.MultiSelect = false;
dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
dgv.ReadOnly = false;
_bindingList = new BindingList<KeyValuePair<string, float>>(_someList);
dgv.DataSource = _bindingList;
Run Code Online (Sandbox Code Playgroud)
但第二列仍然不可编辑.我应该怎么做才能使第二列可编辑而第一列不可编辑?
编辑:我希望更改能够反映在BindingList实例本身上.
编辑2:我在代码的末尾添加了这一行,现在我收到一个错误:
dgv.Columns[1].ReadOnly = false;
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
DataGridView column bound to a read-only field must …
我DataGridView收集了一些对象.在DataGridView,有一个ComboBox名称列表,我正在尝试在每个选择上添加新对象到集合.但问题是AddNew()始终调用子的默认构造函数.:/这是一个代码:
public class Parent:BindingList<Child>
public Child ChildProperty{get;set;}
public new object AddNew()
{
return ChildProperty;
}
public new void AddingNew(object sender, AddingNewEventArgs e)
{
ChildProperty = new Child(this);
e.NewObject = ChildProperty;
}
Run Code Online (Sandbox Code Playgroud)
我必须使用参数化构造函数,因为我需要将父传递给子.
我正在用C#和VS 2010 Ultimate创建一个winform应用程序.我正在使用动态创建的combox填充flowlayoutpanel,所有这些都被数据绑定到相同的绑定列表.当我运行应用程序时,它会正确添加combox,但是当我在一个组合框中选择一个项目时,所有其他项目都会使用相同的选项进行更新.我究竟做错了什么?非常感谢您收到的任何帮助.J.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace TestCompleteForm
{
public partial class Form1 : Form
{
private int comboBoxIndex = 0;
List<string> Existingfiles;
BindingList<string> ExistingSystemsList;
List<string> Selectedfiles;
BindingList<string> SelectedSystemsList;
BindingList<string> ListOfLanguages = new BindingList<string>();
BindingList<string> ListOfSQLServers = new BindingList<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Hosts");
FileInfo[] Files = dinfo.GetFiles("*.txt");
Existingfiles = new …Run Code Online (Sandbox Code Playgroud) 有一个如何修改SortableBindingList以使用稳定排序的示例.但是,有一个SortableBindingList的更新版本.修改此新版本以使用稳定排序的最佳方法是什么?我想我希望SortableBindingList上有一个标志让SortableBindingList的用户决定是否要使用(较慢)稳定排序或(更快)默认排序.
谢谢
我创建一个绑定列表BindingList<RunData>并传递它,CustomMessageBox.Show()但DataGridView不显示列表元素.
public partial class CustomMessageBox : Form
{
#region Fields.
private static CustomMessageBox customMessageBox;
private static String selectedDateTime;
#endregion
#region Properties.
internal String SelectedDateTime
{ get { return CustomMessageBox.selectedDateTime; } }
#endregion
#region Constructors.
private CustomMessageBox()
{
InitializeComponent();
}
#endregion
#region Methods.
internal static DialogResult Show(BindingList<RunData> dataGridViewData)
{
CustomMessageBox.customMessageBox = new CustomMessageBox();
CustomMessageBox.customMessageBox.dataGridViewRunData.AutoGenerateColumns = true;
CustomMessageBox.customMessageBox.dataGridViewRunData.DataSource = dataGridViewData;
return CustomMessageBox.customMessageBox.ShowDialog();
}
#endregion
}
internal class RunData
{
#region Fields.
private String dateTime;
private String …Run Code Online (Sandbox Code Playgroud) 此代码导致DataGridView grid显示空行,尽管它有一个DataPropertyName设置为"MyProp1" 的列:
public class MyClass
{
public int MyProp1;
public int MyProp2;
public int MyProp3;
}
public class MyItems:IListSource
{
BindingList<MyClass> _items = new BindingList<MyClass>();
//..............................
//IListSource
public bool ContainsListCollection
{
get { return false; }
}
//IListSource
public System.Collections.IList GetList()
{
return _items;
}
}
MyItems i = new MyItems();
.............
//MyItems list is populated
.............
grid.DataSource = i;
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?
如果我使用"MyProp1"列创建一个DataTable,其内容将以正确的方式显示.
BindingList<KeyValuePair<string, string>> properties = new BindingList<KeyValuePair<string, string>>();
Run Code Online (Sandbox Code Playgroud)
上面的代码存储大约10-30个对象 as KeyValuePair<string, string>
我需要以某种方式选择一个元素让我们用键"id"来说
我该怎么做?
bindinglist ×12
c# ×12
datagridview ×4
.net ×2
key-value ×2
listbox ×2
binding ×1
combobox ×1
data-binding ×1
drawing ×1
performance ×1
selecteditem ×1
sorting ×1