猿问

C# - 修改 DaraGridView 中第 1 列的值并将其放入新列中

我有一段代码,它将输入字符串拆分为行并将其输出到 DataGridView 中。


这些都填充到第 1 列中。


我想将第 1 列中的值进一步拆分为第 2 列和第 3 列等。


第 2 列和第 3 列的值需要来自第 1 列而不是原始输入。


例如:


编辑:更多示例输入


输入字符串:


abc12, def, 56, jkl78, mno90

当前代码:


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);

    foreach (string item in eSplit)

    {

        eOutputDGV.Rows.Add(item);

    }

 }

期望的输出:(如果没有值则需要为空)。


Section  Letters  Numbers

abc12    abc      12

def      def      

56                56

jkl78    jkl      78

mno90    mno      90


莫回无
浏览 104回答 3
3回答

冉冉说

您必须将每个列定义添加到您的定义中eOutputDGV,然后将三个参数传递给每个列eOutputDGV.Row:private void Button1_Click(object sender, EventArgs e){&nbsp; &nbsp; List<string> eSplit = new List<string>(eInputBox.Text.Split(new string[] { "," }, StringSplitOptions.None));&nbsp; &nbsp; DataGridViewTextBoxColumn eOutputGrid = new DataGridViewTextBoxColumn();&nbsp; &nbsp; eOutputGrid.HeaderText = "Section";&nbsp; &nbsp; eOutputGrid.Name = "Section";&nbsp; &nbsp; eOutputDGV.Columns.Add(eOutputGrid);&nbsp; &nbsp; eOutputGrid = new DataGridViewTextBoxColumn();&nbsp; &nbsp; eOutputGrid.HeaderText = "Letters";&nbsp; &nbsp; eOutputGrid.Name = "Letters";&nbsp; &nbsp; eOutputDGV.Columns.Add(eOutputGrid);&nbsp; &nbsp; eOutputGrid = new DataGridViewTextBoxColumn();&nbsp; &nbsp; eOutputGrid.HeaderText = "Numbers";&nbsp; &nbsp; eOutputGrid.Name = "Numbers";&nbsp; &nbsp; eOutputDGV.Columns.Add(eOutputGrid);&nbsp; &nbsp; foreach (string item in eSplit)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; eOutputDGV.Rows.Add(item.Trim(), item.Trim().Substring(0, 3), item.Trim().Substring(3));&nbsp; &nbsp; }}

临摹微笑

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{&nbsp; &nbsp; public partial class Form1 : Form&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Form1()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add("abc12", "", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add("def", "", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add("56", "", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add("jkl78", "", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows.Add("mno90", "", "");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void Button1_Click(object sender, EventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (DataGridViewRow row in dataGridView1.Rows)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(dataGridView1.Rows[row.Index].Cells[0].Value != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string str = dataGridView1.Rows[row.Index].Cells[0].Value.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = str.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string chars = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string nums = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index >= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars = str.Substring(0, index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nums = str.Substring(index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars = str;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows[row.Index].Cells[1].Value = chars;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataGridView1.Rows[row.Index].Cells[2].Value = nums;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}结果如下:-

扬帆大鱼

假设您有一个字符串列表,您可以使用 linq 查询将其整形为预期结果:dataGridView1.DataSource = list.Select(x=>Process(x)).ToList();方法是什么Process?它是一个静态方法,负责处理输入字符串并将其转换为所需的模型,假设您有一个如下所示的模型:public class MyModel{&nbsp; &nbsp; public string Letters { get; set; }&nbsp; &nbsp; public int Numbers { get; set; }&nbsp; &nbsp; public string Section&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $"{Letters}{Numbers}";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}那么处理方法是这样的:pubic static MyModel Process(string s){&nbsp; &nbsp; // some logic to extract information form `s`&nbsp; &nbsp; return new MyModel(){ Letters = ..., Numbers = ... };}
随时随地看视频慕课网APP
我要回答