3 回答
TA贡献1877条经验 获得超1个赞
您必须将每个列定义添加到您的定义中eOutputDGV,然后将三个参数传递给每个列eOutputDGV.Row:
private void Button1_Click(object sender, EventArgs e)
{
List<string> eSplit = new List<string>(eInputBox.Text.Split(new string[] { "," }, StringSplitOptions.None));
DataGridViewTextBoxColumn eOutputGrid = new DataGridViewTextBoxColumn();
eOutputGrid.HeaderText = "Section";
eOutputGrid.Name = "Section";
eOutputDGV.Columns.Add(eOutputGrid);
eOutputGrid = new DataGridViewTextBoxColumn();
eOutputGrid.HeaderText = "Letters";
eOutputGrid.Name = "Letters";
eOutputDGV.Columns.Add(eOutputGrid);
eOutputGrid = new DataGridViewTextBoxColumn();
eOutputGrid.HeaderText = "Numbers";
eOutputGrid.Name = "Numbers";
eOutputDGV.Columns.Add(eOutputGrid);
foreach (string item in eSplit)
{
eOutputDGV.Rows.Add(item.Trim(), item.Trim().Substring(0, 3), item.Trim().Substring(3));
}
}
TA贡献1982条经验 获得超2个赞
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dgvAlphaNumbericSplit
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add("abc12", "", "");
dataGridView1.Rows.Add("def", "", "");
dataGridView1.Rows.Add("56", "", "");
dataGridView1.Rows.Add("jkl78", "", "");
dataGridView1.Rows.Add("mno90", "", "");
}
private void Button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(dataGridView1.Rows[row.Index].Cells[0].Value != null)
{
string str = dataGridView1.Rows[row.Index].Cells[0].Value.ToString();
int index = str.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
string chars = "";
string nums = "";
if (index >= 0)
{
chars = str.Substring(0, index);
nums = str.Substring(index);
}
else
{
chars = str;
}
dataGridView1.Rows[row.Index].Cells[1].Value = chars;
dataGridView1.Rows[row.Index].Cells[2].Value = nums;
}
}
}
}
}
结果如下:-
TA贡献1799条经验 获得超9个赞
假设您有一个字符串列表,您可以使用 linq 查询将其整形为预期结果:
dataGridView1.DataSource = list.Select(x=>Process(x)).ToList();
方法是什么Process?它是一个静态方法,负责处理输入字符串并将其转换为所需的模型,假设您有一个如下所示的模型:
public class MyModel
{
public string Letters { get; set; }
public int Numbers { get; set; }
public string Section
{
get
{
return $"{Letters}{Numbers}";
}
}
}
那么处理方法是这样的:
pubic static MyModel Process(string s)
{
// some logic to extract information form `s`
return new MyModel(){ Letters = ..., Numbers = ... };
}
- 3 回答
- 0 关注
- 103 浏览
添加回答
举报