查找和替换所有页面 [英] Find and Replace for all pages

查看:94
本文介绍了查找和替换所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在以块的形式将文件读入datagridview。例如:如果一个文件有1000行,我只读100个,点击下一个按钮后我会得到其余100个和下一个.......等等。



我的表单中有查找和替换逻辑。当我点击它时,它只会替换第一页。要替换第二页,我必须再次单击下一页并执行查找和重新打印。



以下是代码放置,请建议我更好的方式:



查找和替换:

Hi All,

I'm reading a file into datagridview in chunks. Ex: If a file is having 1000 lines, i'm reading only 100 and after clicking on next button i will get the rest 100 and next....... so on.

I'm having a Find and Replace logic in my form. When i click on it, it's only replacing first 100. For replacing second page, again i have to click on next page and do Find and Repalce.

Below are the codes placed and please suggest me the better way:

Find and Replace:

Form2 f = new Form2();
f.cmbColumnCombo.DataSource = cmbList;
f.ShowDialog();


for (int i = 0; i <;= dataGridView1.Rows.Count - 1; i++)
{
    if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
    {
        if (!string.IsNullOrEmpty(f.txtfind.Text))
        {
            
            dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);

            dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
            bulidDataRow(i);
            
        }                   
    }
}



以下是Browse的代码(当时选择文件并阅读) :


Below is the code for Browse (at the time selecting the file and reading):

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "Desktop";
    openFileDialog1.Filter = "dat files (*.DAT)|*.DAT|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            FileName = openFileDialog1.FileName;
            string text = System.IO.File.ReadAllText(FileName);
            datfile = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

            maxRec = datfile.Length - 1;
            PageCount = maxRec / pageSize;
            LoadPage(MyFOrmat);



提前谢谢。


Thanks in advance.

推荐答案

您正在阅读整个单击事件中的文件并将其保存在字符串数组中。我假设您使用此数组作为网格的数据源。现在,您可以在字符串数组中替换,然后再将其绑定到datagridview,而不是仅更改每个单元格的文本。这样您就可以一次更新整个数据源。
You are reading the entire file in the click event and holding it in an string array. I assume you are using this array as data source for the grid. Now, instead of just changing the text for each cell, you can do replace in your string array and then bind it again to the datagridview. This way you will be updating entire data source in one go.


这篇关于查找和替换所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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