C# - 在 DaraGridView 中修改 column1 中的值并将它们放在新列中 [英] C# - amend values from column1 in DaraGridView and put them in new column

查看:33
本文介绍了C# - 在 DaraGridView 中修改 column1 中的值并将它们放在新列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a section of code which splits an input string into rows and outputs it into a DataGridView.

这些都填充到第 1 列中.

These are all populated into column 1.

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

I want to further split the values in column 1 into column 2 and 3 etc.

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

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

Desired Output: (if there is no value then it needs to be blank).

Section  Letters  Numbers
abc12    abc      12
def      def      
56                56
jkl78    jkl      78
mno90    mno      90

推荐答案

您必须将每个列定义添加到您的 eOutputDGV 中,然后将三个参数传递给每个 eOutputDGV.Row:

You have to add each column definition to your eOutputDGV and then to pass three parameters to each 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));
    }
}

这篇关于C# - 在 DaraGridView 中修改 column1 中的值并将它们放在新列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆