且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#:拆分字符串并将其放在DataGridView的第一列中

更新时间:2022-12-12 10:28:33

下面的代码采用带分隔符的字符串,将其拆分,然后将值添加到datagridview列.尽管我仍在努力寻找为什么要这样做.

The code below takes a delimited string, splits it and then adds the values to a datagridview column. Though I'm still struggling to see why you want to do this.

string csv = "Name,Surname,Telephone,Address";
string[] split = csv.Split(',');

DataGridViewTextBoxColumn subject = new DataGridViewTextBoxColumn();
subject.HeaderText = "Subject Type";
subject.Name = "Subject";

dataGridView1.Columns.Add(subject);

foreach (string item in split)
{
    dataGridView1.Rows.Add(item);
}