我有一个返回IList <T>的函数,它是DataGridView的DataSource.我了解到DataGridView不会对IList进行排序.我读了这个stackoverflow问答,我正在尝试实现SortableBindingList.我一定做错了,因为我的DataGridView是空的.我还尝试使用TextBox从SortableBindingSource访问一个元素,但也没有.
using Microsoft.SqlServer.Management.Controls;
public partial class Form1 : Form
{
IBusinessLayer businessLayer;
IList<Category> categories;
SortableBindingList<Category> catSortable;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
businessLayer = new BusinessLayer();
categories = businessLayer.GetAllCategories();
catSortable = new SortableBindingList<Category>(categories);
categoryBindingSource.DataSource = catSortable;
categoryDataGridView.DataSource = categoryBindingSource;
textBox1.Text = catSortable[0].CategoryName;
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了Microsoft.SqlServer.Management.Controls,这看起来不错吗?
namespace Microsoft.SqlServer.Management.Controls
{
public class SortableBindingList<T> : BindingList<T>
{
public SortableBindingList();
public SortableBindingList(IList<T> list);
protected override bool IsSortedCore { get; }
protected override ListSortDirection …Run Code Online (Sandbox Code Playgroud) 好的,我在学习 XmlSerializer 时遇到了一些障碍。我遵循了所有推荐的步骤,但我的程序没有返回任何内容,或者返回 null。我创建了一个 XML 文件,如下所示:
<?xml version='1.0' encoding='UTF-8' ?>
<WeeklyJobs>
<DailyJobs Date = "02/03/2012"/>
<DailyJobs Date = "02/04/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/05/2012" TotalJobs = "1">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/06/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" …Run Code Online (Sandbox Code Playgroud) 我对其中一个工作项目的要求存在问题.我正在做的是将数据表绑定到DataGridView(DGV)的数据源.然后我遍历DataGridView并检查单元格的值是否为1*或2**,并使用工具提示和红色背景格式化这些单元格.如果我使用按钮事件触发这一切,一切都很好.但是,如果我希望在使用DataBindingComplete事件加载表单时自动执行此操作,则它无法正常工作.问题是DataBindingComplete多次触发.我读了这个问题,这给了我一些选择尝试,没有一个工作.这是代码:
public partial class TestForm2 : Form
{
private DataTable dt;
private int methodCalls = 0;
private bool isFormatted = false;
public TestForm2()
{
InitializeComponent();
buildDataTable();
dataGridView1.DataBindingComplete += dataGridView1_DataBindingComplete;
}
private void TestForm2_Load(object sender, EventArgs e)
{
bindData();
}
private void button1_Click(object sender, EventArgs e)
{
formatDataGridView();
}
private void bindData()
{
dataGridView1.DataSource = dt;
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//If this code is commented out the program will work just fine
//by just clicking …Run Code Online (Sandbox Code Playgroud)