Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a task called Find and Replace in the DataGridView - It's completed.But i want the replaced count in the message box.

Can anyone help me how to achieve this? Below is my Find and Replace code:

C#
private void btnFindandReplace_Click(object sender, EventArgs e)
        {
            f.cmbColumnCombo.DataSource = cmbList;
            f.ShowDialog();

                        if (recNo > 0)
                            recNo = recNo - 30;
                        LoadPage(MyFOrmat, true);
}
 public void LoadPage(string Format, bool isFindAndReplace = false)
        {
            int startRec;
            int endRec;

            if (currentPage == PageCount)
            {
                endRec = (int)maxRec;
            }
            else
            {
                endRec = (int)pageSize * currentPage;
            }

            dataGridView1.Rows.Clear();
            if (recNo == 0)
            {
                dataGridView1.Columns.Clear();
            }

            int rowindex = 0;
            startRec = recNo;
            for (int RowCount = startRec; RowCount <= endRec; RowCount++)
            {
                if (datfile[RowCount].ToString() != "" )
                {
                    if (RowCount == 0)
                    {
                        string[] column = datfile[RowCount].Split('þ');
                        for (int i = 0; i < column.Length - 1; i++)
                        {
                            if (column[i].ToString() != "") //if (column[i].ToString() != "" && column[i].ToString() != "\u0014")
                            {
                                DataGridViewTextBoxColumn dgvtxtcountry = new DataGridViewTextBoxColumn();
                                dgvtxtcountry.HeaderText = column[i].ToString();
                                dgvtxtcountry.Name = column[i].ToString();
                                dataGridView1.Columns.Add(dgvtxtcountry);
                                cmbList.Add(column[i]);
                                // dataGridView1.Columns["Sent Date"].DefaultCellStyle.Format = "dd/MM/yyyy";
                                i += 1;
                            }
                        }
                    }

                    if (RowCount != 0)
                    {
                        dataGridView1.Rows.Add();
                        string[] column = datfile[RowCount].Split('þ');
                        int index = 0;
                        for (int i = 1; i < column.Length - 1; i++)
                        {
                            if (column[i].ToString() != "\u0014")
                            {
                                //if (i == 3)
                                //{
                                //    dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(Format);
                                //}
                                //else
                                //{ 
                                    dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Trim('þ'); 
                                //}

                                if (StaticVar.FnR == true && index == StaticVar.colindx)
                                {

                                    if ((column[i]).Contains(StaticVar.Find) != null)
                                        dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Replace(StaticVar.Find, StaticVar.Replace);
                                }
                                if (StaticVar.DateFormat != "" && StaticVar.DateFormat != null)
                                {
                                    if (StaticVar.datecolidx == ((i - 1) / 2))
                                    {
                                        dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(StaticVar.DateFormat);
                                    }
                                }
                                
                                index += 1;
                                i += 1;
                            }
                        }
                        rowindex += 1;
                    }
                }
                recNo += 1;
                //IsDateFormatted = false;
            }
        }
Posted
Updated 29-Jan-16 0:58am
v2
Comments
Richard MacCutchan 29-Jan-16 7:17am    
Where is this value saved?
Sinisa Hajnal 29-Jan-16 7:19am    
Just increase some variable on each replace and save it somewhere where you can call it after the replace is done.
Member 8010354 29-Jan-16 8:07am    
Thank you:)
ZurdoDev 29-Jan-16 8:05am    
Sinisa said it. This is very, very easy to do. Where are you stuck?
Member 8010354 29-Jan-16 8:07am    
Working on it.

1 solution

I got it by this way:
if (isFindAndReplace)
{
MessageBox.Show(string.Format("No: of values replaced is: {0}", replaced));
}
 
Share this answer
 

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