如何在数据表中创建组合框列 [英] How to create a combobox column in a datatable
问题描述
我已经为此苦苦挣扎了好几天,而且我把 DataTables 和 dataGridViews 弄混了.
I've been struggling with this for days, and I'm getting DataTables and dataGridViews all mixed up.
我有一个 WinForms 程序,它有一个 DataGridView、dataGridView1 和一个 DataTable,错误.
I have a WinForms program, which has a DataGridView, dataGridView1, and a DataTable, errors.
public static DataTable errors = new DataTable();
dataGridView1.DataSource = errors;
现在,在一个名为 ValidateText 的方法中,我将文本文件中的数据逐行读取到一个数组中,我还在其中定义了错误数据表的列:
Now, further down, in a method called ValidateText, I read data from a text file, line by line, into an array, where I also define the columns for the errors datatable:
errors.Columns.Add("Account Number");
errors.Columns.Add("Customer Name");
errors.Columns.Add("Country");
errors.Columns.Add("State");
errors.Columns.Add("Ship-to Country");
errors.Columns.Add("Ship-to State");
var lines = File.ReadAllLines(file);
foreach (string line in lines)
{.
.
.
string []items=line.Split('\t').ToArray();
errors.Rows.Add(items[0], items[1],...items[5]);
当我运行它时效果很好.问题是,我想让国家"列中有一个组合框,所以当程序运行时,数据显示在 dataGridView1 中,用户将有机会选择一个新的国家,如果他们想要,从国家/地区"列.在程序的更深处,我确实定义了一个创建 DataGridViewComboBoxColumn 的方法
And that works just fine when I run it. the thing is, I want to make the "Country" column have a combo box in it, so when the program is run, and the data is displayed in the dataGridView1, the user will have the opportunity to select a new country, if they want, from the "Countries" column. And further down in the program, I've indeed defined a method that creates a DataGridViewComboBoxColumn
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
DataGridViewComboBoxColumn buildCountries = new DataGridViewComboBoxColumn();
buildCountries.HeaderText = "List of Countries";
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Keys");
dataTable.Columns.Add("Values");
KeyValuePair<string, string>[] array = CountryList.ToArray();
foreach (KeyValuePair<string, string> kvp in array)
{
dataTable.Rows.Add(kvp.Key, kvp.Value);
}
buildCountries.DataSource = dataTable;
buildCountries.DisplayMember = "Values";
buildCountries.ValueMember = "Keys";
return buildCountries;
}
我遇到的问题是,如何将我在最后一种方法中创建的组合框放入我在上面创建的国家/地区"列中?我觉得我没有得到 DataGridView 和 DataTable 的东西.您将 DataTable 绑定到 DataGridView,但添加此 DataGridViewComboBoxColumn 似乎很难.
The thing that I am having problems with, is how do I get that combo box that I've created in that last method, into the "Countries" column I've created above? I feel like I'm not getting something with DataGridView and DataTable. You bind a DataTable to a DataGridView, but adding this DataGridViewComboBoxColumn seems to be hard.
谢谢,
阿曼达
推荐答案
这只是我的建议,.NET Framework 已经有一个国家/地区列表.所以你可以使用这个类.我忘了是谁创建了这个代码 :)
this is just my suggestion, the .NET Framework had a already list of countries. so you can use this class. i forgot who created this code :)
public static class CountryEntries
{
public static IEnumerable<Country> GetCountries()
{
return from ri in
from ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
select new RegionInfo(ci.LCID)
orderby ri.DisplayName
group ri by ri.TwoLetterISORegionName into g
select new Country
{
CountryId = g.Key,
Title = g.First().DisplayName
};
}
public class Country
{
public string CountryId { get; set; }
public string Title { get; set; }
}
}
这篇关于如何在数据表中创建组合框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!