每次单击保存时,将列表绑定到 DataGridView 不显示任何内容并清除对象
Bind list to DataGridView not displaying anything and wiping out objects every time I click save
我正在尝试将 class 列表绑定到我的 WinForms 应用程序上的 DataGridView。当前绑定代码不执行任何操作。我已经调试过了,源代码是正确的,包含带有正确项目的列表。但是我没有在 DataGridView 中看到行。
如何将列表中的内容显示到我的 DataGridView 上?目前,每次我单击“保存”时,它都会清除我的 class 对象,但是我想保留我放入构造函数中的先前值。如果已经存在一个国家/地区,我希望用户弹出框说明他们是否要覆盖此内容 - 这可能吗/我怎样才能最好地实现这一点?
CountryWithDates.cs
class CountryWithDates
{
public string country;
public DateTime firstDate;
public DateTime furtherDate;
public DateTime rolldownDate;
public DateTime endDate;
}
点击保存:
private void Save_Click(object sender, EventArgs e)
{
List<CountryWithDates> countryDates = new List<CountryWithDates>();
for (int i = 0; i < Countries_LB.Items.Count; i++)
{
if (Countries_LB.GetItemChecked(i))
{
countryDates.Add(new CountryWithDates(){country = Countries_LB.Items[i].ToString(),
firstMarkdownDate = DateTime.Parse(firstDatePicker.Text),
furtherMarkdownDate = DateTime.Parse(furtherDatePicker.Text),
rolldownMarkdownDate = DateTime.Parse(rolldownDatePicker.Text),
endMarkdownDate = DateTime.Parse(endMarkdownPicker.Text)});
}
}
//THIS DOESNT DO ANYTHING - I WANT TO BIND THE SOURCE TO THE GRIDVIEW but i dont see the output in the data gridview on the form when i click save
var bindingList = new BindingList<CountryWithDates>(countryDates);
var source = new BindingSource(bindingList, null);
gv_Countries.DataSource = source;
}
如果我这样做,我会:
- 向我的项目添加一个新的数据集
- 打开数据集设计器
- 添加一个 DataTable,将其命名为 CountryWithDates,6 列(国家、第一、进一步、Markdown、End、SaveThisRow(boolean))
- 保存数据集
- 打开表单设计器
- 显示数据源window
- 将表示 CountryWithDates 的节点拖到设计器上
DataGridView 出现,数据集、绑定源等也出现。一切都已绑定。
当需要保存到磁盘并且您只想保存那些选中的项目时,datatable.Copy()
它,然后删除未标记为保存的每一行,然后调用 WriteXml 保存清理后的副本.也许是这样的(未经测试):
private blah SaveButton_Click(blah){
var dt = (CountryWithDatesDataTable)_myDataset.CountryWithDates.Copy();
for(int i = dt.Count - 1; i >= 0; i--){
if(!dt[i].SaveThisRow)
dt[i].Delete(); //mark row deleted
}
dt.WriteXml(@"C:\temp\dt.xml")
}
您应该在 class 中创建 public Porperties 而不是 public Fields:
class CountryWithDates
{
//Following the naming rule is good. I'll leave it to you.
public string country { get; set; }
public DateTime firstDate { get; set; }
public DateTime furtherDate { get; set; }
public DateTime rolldownDate { get; set; }
public DateTime endDate { get; set; }
public CountryWithDates() { }
public override string ToString()
{
return country;
}
}
通过这种方式,您可以将 CountryWithDates
个对象的列表绑定到 CheckedListBox
:
var lst = new List<CountryWithDates>();
//Add new objects ...
//lst.Add(new CountryWithDates { country = "Country 1", firstDate ... });
checkedListBox1.DataSource = null;
checkedListBox1.DataSource = lst;
要从列表中获取和更新选中的项目,创建一个新的 BindingSource
,并将其绑定到 DataGridView
,您只需要做:
checkedListBox1.CheckedItems
.Cast<CountryWithDates>()
.ToList()
.ForEach(item =>
{
item.firstDate = firstDatePicker.Value;
item.furtherDate = furtherDatePicker.Value;
item.rolldownDate = rolldownDatePicker.Value;
item.endDate = endMarkdownPicker.Value;
});
dataGridView1.DataSource = null;
var bs = new BindingSource(checkedListBox1.CheckedItems.Cast<CountryWithDates>(), null);
dataGridView1.DataSource = bs;
我正在尝试将 class 列表绑定到我的 WinForms 应用程序上的 DataGridView。当前绑定代码不执行任何操作。我已经调试过了,源代码是正确的,包含带有正确项目的列表。但是我没有在 DataGridView 中看到行。
如何将列表中的内容显示到我的 DataGridView 上?目前,每次我单击“保存”时,它都会清除我的 class 对象,但是我想保留我放入构造函数中的先前值。如果已经存在一个国家/地区,我希望用户弹出框说明他们是否要覆盖此内容 - 这可能吗/我怎样才能最好地实现这一点?
CountryWithDates.cs
class CountryWithDates
{
public string country;
public DateTime firstDate;
public DateTime furtherDate;
public DateTime rolldownDate;
public DateTime endDate;
}
点击保存:
private void Save_Click(object sender, EventArgs e)
{
List<CountryWithDates> countryDates = new List<CountryWithDates>();
for (int i = 0; i < Countries_LB.Items.Count; i++)
{
if (Countries_LB.GetItemChecked(i))
{
countryDates.Add(new CountryWithDates(){country = Countries_LB.Items[i].ToString(),
firstMarkdownDate = DateTime.Parse(firstDatePicker.Text),
furtherMarkdownDate = DateTime.Parse(furtherDatePicker.Text),
rolldownMarkdownDate = DateTime.Parse(rolldownDatePicker.Text),
endMarkdownDate = DateTime.Parse(endMarkdownPicker.Text)});
}
}
//THIS DOESNT DO ANYTHING - I WANT TO BIND THE SOURCE TO THE GRIDVIEW but i dont see the output in the data gridview on the form when i click save
var bindingList = new BindingList<CountryWithDates>(countryDates);
var source = new BindingSource(bindingList, null);
gv_Countries.DataSource = source;
}
如果我这样做,我会:
- 向我的项目添加一个新的数据集
- 打开数据集设计器
- 添加一个 DataTable,将其命名为 CountryWithDates,6 列(国家、第一、进一步、Markdown、End、SaveThisRow(boolean))
- 保存数据集
- 打开表单设计器
- 显示数据源window
- 将表示 CountryWithDates 的节点拖到设计器上
DataGridView 出现,数据集、绑定源等也出现。一切都已绑定。
当需要保存到磁盘并且您只想保存那些选中的项目时,datatable.Copy()
它,然后删除未标记为保存的每一行,然后调用 WriteXml 保存清理后的副本.也许是这样的(未经测试):
private blah SaveButton_Click(blah){
var dt = (CountryWithDatesDataTable)_myDataset.CountryWithDates.Copy();
for(int i = dt.Count - 1; i >= 0; i--){
if(!dt[i].SaveThisRow)
dt[i].Delete(); //mark row deleted
}
dt.WriteXml(@"C:\temp\dt.xml")
}
您应该在 class 中创建 public Porperties 而不是 public Fields:
class CountryWithDates
{
//Following the naming rule is good. I'll leave it to you.
public string country { get; set; }
public DateTime firstDate { get; set; }
public DateTime furtherDate { get; set; }
public DateTime rolldownDate { get; set; }
public DateTime endDate { get; set; }
public CountryWithDates() { }
public override string ToString()
{
return country;
}
}
通过这种方式,您可以将 CountryWithDates
个对象的列表绑定到 CheckedListBox
:
var lst = new List<CountryWithDates>();
//Add new objects ...
//lst.Add(new CountryWithDates { country = "Country 1", firstDate ... });
checkedListBox1.DataSource = null;
checkedListBox1.DataSource = lst;
要从列表中获取和更新选中的项目,创建一个新的 BindingSource
,并将其绑定到 DataGridView
,您只需要做:
checkedListBox1.CheckedItems
.Cast<CountryWithDates>()
.ToList()
.ForEach(item =>
{
item.firstDate = firstDatePicker.Value;
item.furtherDate = furtherDatePicker.Value;
item.rolldownDate = rolldownDatePicker.Value;
item.endDate = endMarkdownPicker.Value;
});
dataGridView1.DataSource = null;
var bs = new BindingSource(checkedListBox1.CheckedItems.Cast<CountryWithDates>(), null);
dataGridView1.DataSource = bs;