using System.Windows.Forms; namespace DataGridViewDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); InitializeDatagridView(); } private void InitializeDatagridView() { string[] stringlist = "I Love You Tracy".Split(); var bindingSource = new BindingSource(); foreach (string s in stringlist) { bindingSource.Add(new SampleData {WordLength = s.Length, Word = s}); } var dgView = new DataGridView {AutoGenerateColumns = false}; var col1 = new DataGridViewColumn { Name = "Word", ValueType = typeof (string), CellTemplate = new DataGridViewTextBoxCell(), DataPropertyName = "Word" }; var col2 = new DataGridViewColumn { Name = "WordLength", ValueType = typeof (int), CellTemplate = new DataGridViewTextBoxCell(), DataPropertyName = "WordLength" }; dgView.Columns.Add(col1); dgView.Columns.Add(col2); dgView.DataSource = bindingSource; Controls.Add(dgView); } public class SampleData { public string Word { get; set; } public int WordLength { get; set; } } } }
Thanks for advice from this link.
http://dobon.net/vb/dotnet/datagridview/addcolumn.html
0 件のコメント:
コメントを投稿