Message box for the replaced values count - c#

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:
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;
}
}

Declare the replaced variable and increment it every time you replace a value. Show a message box at the end of the method.
public void LoadPage(string Format, bool isFindAndReplace = false) {
int replaced = 0;
....
if ((column[i]).Contains(StaticVar.Find) != null) {
dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Replace(StaticVar.Find, StaticVar.Replace);
replaced++;
}
....
if(isFindAndReplace)
MessageBox.Show(string.Format("{0} occurrence(s) replaced.", replaced));
}

Related

How to move between Datagridview Cells by Enter key in edit mode C#

The code I will attach it works for me to add new row only if all cells not empty but when I tried to edit any pervious row contain data it takes me to next row and next column
Example
if I click on rowIndex = 0 , ColumnIndex = 1 and I press Enter it takes me to rowIndex = 1 , ColumnIndex = 2
.
private void grid1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int iColumn = grid1.CurrentCell.ColumnIndex;
int iRow = grid1.CurrentCell.RowIndex;
if (iColumn == grid1.Columns.Count - 2)
{
foreach (DataGridViewRow rw in this.grid1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString()))
{
flag2 = true;
}
else
flag2 = false;
}
}
if (flag2)
{
MessageBox.Show("Fill the Blanks");
}
else
{
grid1.Rows.Add();
grid1.CurrentCell = grid1[0, iRow + 1];
}
}
else
{
DataGridViewCell cell = grid1[iColumn + 1, iRow];
NextCell(iColumn, iRow);
}
}
.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
public void NextCell(int col, int row)
{
int count = grid1.ColumnCount;
int colindex = col + 1;
try {
if (colindex < count)
{
DataGridViewCell cell = grid1[colindex, row];
if (cell.Visible == true)
{
grid1.CurrentCell = cell;
}
else
{
NextCell(colindex, row);
}
}
}
catch { }
}
Thank You all I solve it by me self.
private void grid1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// I left it empty
}
..
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
bool insiderFlage = false;
bool flag2 = true;
int icolumn = grid1.CurrentCell.ColumnIndex;
int irow = grid1.CurrentCell.RowIndex;
if (keyData == Keys.Enter)
{
if (icolumn == grid1.Columns.Count - 2)
{
foreach (DataGridViewRow rw in this.grid1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString()))
{
flag2 = true;
}
else
flag2 = false;
}
}
if (flag2)
{
for (int i = 0; i < grid1.ColumnCount; i++)
{
if (grid1.CurrentRow.Cells[i].Value == null || grid1.CurrentRow.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(grid1.CurrentRow.Cells[i].Value.ToString()))
{
insiderFlage = true;
}
}
if (insiderFlage) MessageBox.Show("Fill All Blanks", "!");
else
grid1.CurrentCell = grid1[0, irow + 1];
}
else
{
grid1.Rows.Add();
grid1.CurrentCell = grid1[0, irow + 1];
}
}
else
{
grid1.CurrentCell = grid1[icolumn + 1, irow];
}
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}

OutOfMemoryException while while opening OracleConnection

A coworker and I got the task to fix an problem on a SQL-Client.
The error is a System.OutOfMemoryException.
I figured that it could be because of the client has to many connections that doesn't get closed properly.
I tested a bit around but I can't seem to get it working.
Anyone have a suggestion?
This is the code where the error happens:
public void SQLIntoDataGridView(OracleCommand cmd, int startIndex, int maxIndex, int ersteSpalte, int maxSpalten, DataTable datenTabelle, DataGridView dgv, int sqlNr, DataTable dt)
{
OracleConnection test = new OracleConnection();
int i = 0;
int j = 0;
int spalten = ersteSpalte;
int id;
int spaltenZaehler = ersteSpalte;
int maxrows = datenTabelle.Rows.Count;
int readZeile = 0;
int writeZeile = 0;
int startrow = 0;
int startSpalte = 0;
int endSpalte = 0;
Stopwatch watch = new Stopwatch();
watch.Start();
test.ConnectionString = "Data Source=; User ID=; Password=";
using (test)
{
if (test.State == ConnectionState.Closed)
test.Open(); // <-- this is where the error occures
using (OracleDataReader reader = cmd.ExecuteReader())
{
datenTabelle.Load(reader);
dataGridViewErgebnis.DataSource = dt;
dataGridViewTest.DataSource = datenTabelle;
dataGridViewErweitern(sqlNr);
#region Rechnungsempfänger X
if (startIndex == 27 || startIndex == 24)
{
while (j < dataGridViewErgebnis.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString() == "")
{
readZeile++;
i = readZeile;
j++;
writeZeile++;
}
else if (dataGridView1.Rows[startrow].Cells[0].Value.ToString() == dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString())
{
if (startIndex == 24)
{
startSpalte = 7;
endSpalte = 9;
spaltenZaehler = startSpalte;
}
else if (startIndex == 27)
{
startSpalte = 10;
endSpalte = 14;
spaltenZaehler = startSpalte;
}
spaltenZaehler = startSpalte;
maxSpalten = endSpalte;
while (spaltenZaehler <= maxSpalten)
{
i = startrow;
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dgv.Rows[i].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
readZeile++;
i = readZeile;
j++;
writeZeile++;
spaltenZaehler = startSpalte;
startrow = 0;
}
else
{
startrow++;
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dataGridViewTest.Rows[i].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
}
}
#endregion
else
{
while (i < dt.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[i].Cells[0].Value.ToString() == dataGridViewTest.Rows[j].Cells[0].Value.ToString())
{
while (spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
if (startIndex == 10 && j == datenTabelle.Rows.Count - 1)
{
j--;
j--;
i++;
}
if (j <= datenTabelle.Rows.Count - 1)
{
j++;
}
spaltenZaehler = ersteSpalte;
}
else
{
i++;
while (id <= maxIndex)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = "";
id++;
}
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
else if (j == datenTabelle.Rows.Count && startIndex != 10)
{
j--;
if (i <= dt.Rows.Count - 2)
{
i++;
}
}
}
}
watch.Stop();
StoppUhr(watch);
}
}
}
I would rewrite the code like this.
.
.
.
OracleConnection test = new OracleConnection();
.
.
.
using (OracleConnection test = new OracleConnection("Data Source=; User ID=; Password=")) // <-- SQLconnection here
{
test.Open(); // <-- Only Open connection
using (OracleDataReader reader = cmd.ExecuteReader())
{
.
.
.
}
}

How to make my code evaluate the whole arithmetic expression?

I am trying to make a string calculator, it works fine with two numbers but I always encounter a problem when evaluating multiple operations:
7*2+4=
Also can you help me with my multiplication and division code. I don't understand why it prints 0 even with just two numbers(7*5)
using System;
using System.Text.RegularExpressions;
namespace Sariling_Calcu
{
class Program
{
private static string exp;
private static int[] i = new int[1000];
private static char[] oper = new char[10];
private static int cntr2;
private static int result;
private static int pluscount;
private static int subcount;
private static int mulcount;
private static int divcount;
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < exp.Length; cntr++)
{
foreach (string item in strNum)
{
if (!string.IsNullOrEmpty(item))
{
i[cntr] = int.Parse(item);
cntr += 1;
}
}
}
}
static void counter()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
pluscount++;
}
else if (exp[cntr] == '-')
{
subcount++;
}
else if (exp[cntr] == '*')
{
mulcount++;
}
else if (exp[cntr] == '/')
{
divcount--;
}
}
}
static void oprtr()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] != '1'
&& exp[cntr] != '2'
&& exp[cntr] != '3'
&& exp[cntr] != '4'
&& exp[cntr] != '5'
&& exp[cntr] != '6'
&& exp[cntr] != '7'
&& exp[cntr] != '8'
&& exp[cntr] != '9')
{
if (exp[cntr] == '+')
{
result += i[cntr2];
cntr2 += 1;
pluscount--;
if (pluscount == 0 && subcount == 0 && mulcount==0 && divcount==0)
{
cntr2 += 3;
result += i[cntr2];
}
}
else if (exp[cntr] == '-')
{
result -= i[cntr2];
cntr2 += 1;
subcount--;
result = -result;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result -= i[cntr2];
}
}
else if (exp[cntr] == '*')
{
if (result == 0)
{
result += 1;
}
result *= i[cntr2];
cntr2 += 1;
mulcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result *= i[cntr2];
}
}
else if (exp[cntr] == '/')
{
if (result == 0)
{
result += 1;
}
result /= i[cntr2];
cntr2 += 1;
divcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result /= i[cntr2];
}
}
}
}
}
static void Main(string[] args)
{
Console.Write("Expression: ");
exp = Console.ReadLine();
counter();
getNum();
oprtr();
Console.Write("Answer: \n" + result);
Console.ReadLine();
}
}
}
you could use LINQ to reduce your code to a few lines but looks like its a school assignment where you would have to go with Arrays and loops.
I have tried to refine your code a bit and did a few fixes, change getNum(), counter() and oprtr() methods as below and let me know if it works, then I would add some comments in the code to explain changes I made.
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < strNum.Length; cntr++)
{
if (!string.IsNullOrEmpty(strNum[cntr]))
{
i[cntr] = int.Parse(strNum[cntr]);
}
}
}
static void counter()
{
cntr2 = 0;
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
oper[cntr2] = '+';
cntr2++;
}
else if (exp[cntr] == '-')
{
oper[cntr2] = '-';
cntr2++;
}
else if (exp[cntr] == '*')
{
oper[cntr2] = '*';
cntr2++;
}
else if (exp[cntr] == '/')
{
oper[cntr2] = '/';
cntr2++;
}
}
}
static void oprtr()
{
result = i[0];
cntr2 = 1;
for (int cntr = 0; cntr < oper.Length; cntr++)
{
if (oper[cntr] == '+')
{
result += i[cntr2];
}
else if (oper[cntr] == '-')
{
result -= i[cntr2];
}
else if (oper[cntr] == '*')
{
result *= i[cntr2];
}
else if (oper[cntr] == '/')
{
if (i[cntr2] == 0)
{
throw new DivideByZeroException();
}
result /= i[cntr2];
}
cntr2 += 1;
}
}

Calculate datagridview Cell Sepratly based on data c#

Datagridview calculate Amount each cell Differently based on data from database if addition then addition should be done if subtraction then subtraction to be done.
i have created this method but it affects all cell with answer. only the above cell should be calculated.
public void datagridviewone()
{
int addvalue = 0, subractvalue = 0;
ds = cs.Select("select ChargesProperty from FrieghtGridview");
list.Clear();
addition.Clear();
subtraction.Clear();
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
if(ds.Tables[0].Rows[j][0].ToString() == "Answer")
{
list.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Plus")
{
addition.Add(j);
}
if(ds.Tables[0].Rows[j][0].ToString() == "Minus")
{
subtraction.Add(j);
}
}
if (list.Count > 0)
{
for(int j = 0; j < list.Count; j++)
{
if(addition.Count > 0)
{
for(int adval = 0; adval <= addition.Count-1; adval++)
{
if
(dataGridView1.Rows[addition[adval]].Cells[3].Value.ToString()
!= "")
{
addvalue +=
Convert.ToInt32(dataGridView1.Rows[addition[adval]].Cells[3].Value);
}
}
}
if (subtraction.Count > 0)
{
for (int adval = 0; adval <= subtraction.Count - 1;
adval++)
{
if
(dataGridView1.Rows[subtraction[adval]].Cells[3].Value.ToString() != "")
{
subractvalue +=
Convert.ToInt32(dataGridView1.Rows[subtraction[adval]].Cells[3].Value);
}
}
}
var finalans = addvalue - subractvalue;
dataGridView1.Rows[list[0]].Cells[3].Value = finalans;
addvalue = 0;
subractvalue = 0;
finalans = 0;
}
}
}

Performance issue with traversing&&filtering datagridview in c#

Currently, I am doing a log analyzer for my personal project.
My issue here is that I am new to c# and I have an performance issue with my tool.
Everytime the device(iOS) is interacted, I get an output syslog from a library and it comes in to the output handler.
public void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine, iosSyslogger form, string uuid)
{
string currentPath = System.Environment.CurrentDirectory;
bool exit = false;
if (exit == true) return;
try
{
form.BeginInvoke(new Action(() =>
{
form.insertLogText = outLine.Data;
}));
using (System.IO.StreamWriter file = new System.IO.StreamWriter(currentPath + #"\syslog" + uuid + ".txt", true))
{
file.WriteLine(outLine.Data);
}
}
catch
{
return ;
}
//*Most of the logic for outputing the log should be dealt from this output Handler
}
Then, I write the outline.Data to Data grid view. My concern is that I need to be able to search and filter through data gridview.
Curently I am using visibility = false for search filtering ( if the row does not match the given filter specification I set the row to visibility =false)
This requires the program to traverse the entire datagridview to check whether the condition is met.
Will there be any better way to filter and search within ?
(When I have thousands of lines of row, it takes at least 20 seconds to process it)
Below is the code for filtering, and searching through the results function.
private void searchResult(string term)
{
if (term != null)
{
int i = 0;
while (i < dataGridView1.Rows.Count - 1)
{
if (dataGridView1.Rows[i].Cells[3] == null)
{
i++;
continue;
}
if (dataGridView1.Rows[i].Visible == false)
{
i++;
continue;
}
else if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains(term) || dataGridView1.Rows[i].Cells[3].Value.ToString().Contains(term) || dataGridView1.Rows[i].Cells[4].Value.ToString().Contains(term))
{
string multirow = dataGridView1.Rows[i].Cells[5].Value.ToString();
int count = Convert.ToInt32(multirow);
if (count > 0)
{
int z = 0;
for (z = 0; z <= count; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
i = i + z;
}
else
{
dataGridView1.Rows[i].Visible = true;
i++;
}
}
else
{
dataGridView1.Rows[i].Visible = false;
i++;
}
}
}
}
public filteringThelist(){
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Visible = false;
int count1,count2,count3=0;
count1 = 1;
count2 = 1;
count3 = 1;
int z = 0;
foreach (KeyValuePair<string, string> entry in totalSelected)
{
if (entry.Value == "devicename" && dataGridView1.Rows[i].Cells[1].Value != null)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString().Trim().Equals(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
}
else if (devicename.CheckedItems.Count > 1&&count1!= devicename.CheckedItems.Count)
{
count1++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
else if (entry.Value == "process" && dataGridView1.Rows[i].Cells[2].Value != null)
{
if (dataGridView1.Rows[i].Cells[2].Value.ToString().Trim().Equals(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
}
else if (processlistname.CheckedItems.Count > 1 && count2 != processlistname.CheckedItems.Count)
{
count2++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
else if (entry.Value == "loglevel" && dataGridView1.Rows[i].Cells[3].Value != null)
{
if (dataGridView1.Rows[i].Cells[3].Value.ToString().Trim().Contains(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
continue;
}
else if (loglevelCheckBox.CheckedItems.Count > 1 && count3 != loglevelCheckBox.CheckedItems.Count)
{
count3++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
// do something with entry.Value or entry.Key
}
string multirow = dataGridView1.Rows[i].Cells[5].Value.ToString();
int count = Convert.ToInt32(multirow);
if (count > 0&& dataGridView1.Rows[i].Visible==false)
{
for (int k = 0; k <= count; k++)
{
dataGridView1.Rows[i + k].Visible = false;
}
}
i = i + z;
}
The performance problem is because your DataGridView is redrawing at each modification.
Don't fill the DataGridView directly from your Data. Rather create a DataTable and bind it to DataGridView with a BindingSource.
Then use the "Filter" property of the binding source to view extracts of dataTable in the DataGridView. The "Filter" property is a string whose content is similar to that of a WHERE SQL clause.

Categories

Resources