0

我已经从 datagridview 中的文本文件中读取了数据,现在如果用户在运行时更改了 datagridview 值,我想对文本文件进行更改?这是我读取文件的代码,现在我想在特定位置更新文本文件中的数据并写入它..

    private void button1_Click(object sender, EventArgs e)
    {
        string line = "";
        string text = "";
        bool si = false;
        int w = 0;
        bool t = false;
        bool counter = false;
        int length = 0;
        const Int32 BufferSize = 128;
        int groupid = -1;
        int stringid = -1;
        string[] split;
        int tnog = 0;
        if (file.Contains("random.TST"))
        {
            using (var fileStream = File.OpenRead(this.file))
            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
                while ((line = streamReader.ReadLine()) != null)
                {
                    if (line.Contains('$') && (line.Contains('@') || line.Contains('}')))
                    {
                        split = line.Split(' ', '@', '}');
                    }
                    else if (line.Contains('$'))
                    {
                    }
                    else if (line.Contains('|'))
                    {
                        counter = false;
                    }
                    else if (!(counter))
                    {
                        split = line.Split(' ', '@');
                        for (int i = 0; i < split.Length; i++)
                        {
                            if (t)
                            { }
                            else if (!t)
                            {
                                if (split[i].Trim() == "")
                                {
                                    groupid = 0;
                                }
                                else if (split[i].Trim() != "")
                                {
                                    groupid = Convert.ToInt16(split[i]);
                                    t = true;
                                    tnog++;
                                }
                            }

                            counter = true;
                        }
                    }

                    else if (counter)
                    {
                        //split = line.Split(' ');
                        if (line.Contains('|'))
                        {
                            counter = false;
                        }
                        else if (counter)
                        {
                            split = line.Split(' ', '@', '}');
                            for (int i = 0; i < split.Length; i++)
                            {
                                if (si)
                                {
                                    if (split[i].Trim() == "")
                                    {
                                        //text[i] = " ";
                                    }
                                    else if (split[i].Trim() != "")
                                    {
                                        text += Convert.ToString(split[i]);
                                        text += " ";
                                        length = text.Length;
                                    }

                                }
                                else if (!si)
                                {
                                    if (split[i].Trim() == "")
                                    {
                                        // sid[i] = 0;
                                    }
                                    else if (split[i].Trim() != "")
                                    {
                                        stringid = Convert.ToInt16(split[i]);
                                        si = true;
                                    }

                                }

                            }
                            if ((groupid == -1) || (stringid == -1))
                            { }
                            else
                            {
                                dataGridView1.Rows.Add(groupid, stringid, text, length);
                                text = "";
                                if (tnog % 2 == 0)
                                {
                                    dataGridView1.Rows[w].DefaultCellStyle.ForeColor = Color.Gray;
                                    dataGridView1.Rows[w].DefaultCellStyle.BackColor = Color.Yellow;

                                }
                                else
                                {
                                    dataGridView1.Rows[w].DefaultCellStyle.ForeColor = Color.Plum;
                                    dataGridView1.Rows[w].DefaultCellStyle.BackColor = Color.RoyalBlue;
                                }
                                w++;
                            }

                            si = false;
                            t = false;
                        }


                    }
                }
            textBox1.Text = Convert.ToString(tnog);
        }
        else
        {
            MessageBox.Show("Invalid File:");
        }
    }
4

1 回答 1

0

你真的应该考虑将你的数据绑定到 datagridview (你能给我们一些文件中的示例行吗?),但如果不是......

将您的整个文件读入一个字符串,并将所有相关值替换为唯一的占位符计数器,即{1}、{2}。在将字符串写回文件之前,用 datagridview 数据替换占位符计数器。

于 2013-06-11T16:32:14.810 回答