Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
C#
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);
            
        }                   
    }
}

Below is the code for Browse (at the time selecting the file and reading):
C#
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.
Posted
Updated 7-Jan-16 0:13am
v2
Comments
Suvendu Shekhar Giri 7-Jan-16 6:03am    
So what is the problem?
Member 8010354 7-Jan-16 6:09am    
When i'm doing Find and Replace, it's working for only current page not for all pages. I want it in one go like once i do find and replace, it should find the word in all pages and replace in all pages.
Richard MacCutchan 7-Jan-16 6:09am    
You just need to add the code to automatically load the next page when you reach the end of the grid.
Member 8010354 7-Jan-16 6:55am    
I tried adding. But i'm missing some logic. Can you please suggest me where? Below is the code for NEXT button:

private void btnNext_Click(object sender, EventArgs e)
{
currentPage += 1;
if (currentPage > PageCount)
{
currentPage = PageCount;
//Check if you are already at the last page.
if (recNo == maxRec)
{
MessageBox.Show("You are at the Last Page!");
return;
}
}
LoadPage(MyFOrmat);
}
Richard MacCutchan 7-Jan-16 7:09am    
You just need the same logic at the end of your Find/Replace method.

1 solution

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.
 
Share this answer
 
Comments
Member 8010354 7-Jan-16 7:25am    
Yeah what you told is correct. It's reading from array. Can you give me more hint of sourcing it through programmatically if you got the code cause i'm new learner to this .net and the project is already starteda and i took it from middle.

The array is:
String[] datfile;
dan!sh 7-Jan-16 7:31am    
Search for find and replace in string array.
Member 8010354 7-Jan-16 9:02am    
Didn't get you exactly
dan!sh 7-Jan-16 22:11pm    
You can search on web how to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900