using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { publicpartialclass Form10 : Form { public Form10() { InitializeComponent(); } privatevoid button1_Click(object sender, EventArgs e) { string[] str =newstring[] { "one", "two", "three" }; this.dataGridView1.DataSource = str; } } }
我们需要包装一下数组,方法如下: 图片预览
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { publicpartialclass Form10 : Form { public Form10() { InitializeComponent(); } privatevoid button1_Click(object sender, EventArgs e) { Item[] items =new Item[] { new Item("one"), new Item("two"), new Item("three") }; this.dataGridView1.DataSource = items; } } class Item { privatestring _text; publicstring Text { get{ return _text; } } public Item(string text) { this._text = text; } } }