如何填充datagridview“-”从富文本框分隔文本 [英] How to fill datagridview a "-" delimited text from a richtextbox

查看:99
本文介绍了如何填充datagridview“-”从富文本框分隔文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用RichTextBox中的-分隔文本填充数据网格视图?

How can i fill my datagridview with a - delimited text from my richtextbox?

示例:

我的Richtextbox内容:

my richtextbox content:

000001-Kobe-Bryant-24-Lakers

000002-Lebron-James-23-Cavaliers

000003-Derick-Rose-1-Bulls

000004-Kevin-Durant-35-Thunders

然后我的datafridview上的输出应该是这样

Then the out put on my datafridview should be like this

PlayerID | Name    |  LastName    |    Number      |     Team     |
-------------------------------------------------------------------
000001   |Kobe     |  Bryant      | 24             |Lakers        |
-------------------------------------------------------------------
000002   |Lebron   | James        | 23             |Cavaliers     |
-------------------------------------------------------------------
000003   |Derick   |Rose          | 1              |Bulls         |
-------------------------------------------------------------------
000004   |Kevin    |Durant        |35              |Thunders      |

我不能放置图像,这就是为什么只绘制datagridview的原因。

I cant put image thats why just draw a datagridview.

这里是我正在使用的代码

Here is the code that i am using

    private void button2_Click(object sender, EventArgs e)
    {
        String delimitedText = richTextBox1.Text;
        string[] holder = Regex.Split(richTextBox1.Text, "-");

        // Populating into datagrid

        dataGridView1.Rows[0].Cells[0].Value = holder[0].ToString();
        dataGridView1.Rows[0].Cells[1].Value = holder[1].ToString();
        dataGridView1.Rows[0].Cells[2].Value = holder[2].ToString();
        dataGridView1.Rows[0].Cells[3].Value = holder[3].ToString();
        dataGridView1.Rows[0].Cells[4].Value = holder[4].ToString();
    }

它只是保存第一行。

请有人帮我解决这个问题。

Please someone help me with this.

推荐答案

使用C#拆分选项来获取带分隔符的字符串数组,然后将其填充到数据网格中。

Use the C# Split option to get the delimited strings in an array and then populate it into a data grid.

示例:

String delimitedText = "000001-Kobe-Bryant-24-Lakers";
string[] stringObjects = delimitedText.Split('-');

// Populating into datagrid

dataGridView1.rows[0].cells[0] = stringObjects[0];
dataGridView1.rows[0].cells[1] = stringObjects[1];
dataGridView1.rows[0].cells[2] = stringObjects[2];

谢谢

这篇关于如何填充datagridview“-”从富文本框分隔文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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