为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
莫回无 2023-09-09 16:20:27
我有一段代码,它将输入字符串拆分为行并将其输出到 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  Numbersabc12    abc      12def      def      56                56jkl78    jkl      78mno90    mno      90
查看完整描述

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

    }

}


查看完整回答
反对 回复 2023-09-09
?
临摹微笑

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;


                }

            }

        }

    }

}

结果如下:-

https://img1.sycdn.imooc.com//64fc2b1300019f6104460445.jpg

查看完整回答
反对 回复 2023-09-09
?
扬帆大鱼

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 = ... };

}


查看完整回答
反对 回复 2023-09-09
  • 3 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信