WPF Tree View event - c#

I want to make an application in WPF same as windows application. When i use treeview event in wpf i did not find any event similar to treeview_NodeMouseClick of WIndows Application.
//Windows Application Code
private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != null)
{
GetAllchield(e.Node, e.Node.Level);
}
}
// GetAllchield
public void GetAllchield(TreeNode clickednode, int indexDepth)
{
if (clickednode.Nodes.Count > 0 && !clickednode.IsExpanded)
{
clickednode.Collapse();
return;
}
string[] FullPath = clickednode.FullPath.Split('\\');
string rootnode = FullPath[0].ToString();
//get all market for root event type
int eventTypeID = DictionaryAllActiveEventTypes[rootnode];
string[] allMarkets = GetAllMarketForEventID(eventTypeID);
//selecting unque chield node and populating in tree
for (int i = 0; i < allMarkets.Length; i++)
{
if (allMarkets[i].Contains(clickednode.Text))
{
string[] marketDetails = allMarkets[i].Split('~');
int marketID = Convert.ToInt32(marketDetails[0]);
string MarketName = marketDetails[1].ToString();
string MarketStatus = marketDetails[3].ToString();
string EventHeirarchy = marketDetails[6].ToString();
string Menupath = marketDetails[5].ToString();
string[] Arrmenupath = Menupath.Trim(':').Split('\\');
string chieldText = "";
if (indexDepth == 0)
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
else
{
if (Arrmenupath[Arrmenupath.Length - 1] == clickednode.Text)
chieldText = MarketName;
else
{
if (allMarkets[i].Contains(clickednode.Text) && allMarkets[i].Contains(clickednode.Parent.Text) && allMarkets[i].Contains(rootnode))
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
}
//check whether node is already added
//if (chieldText.Contains("MiWay"))
//{ }
if (!string.IsNullOrEmpty(chieldText))
{
if (clickednode.Nodes.Count >= 1)
{
bool doesNodeAlreadyExist = false;
foreach (TreeNode node in clickednode.Nodes)
{
if (node.Text == chieldText)
{
doesNodeAlreadyExist = true;
break;
}
}
if (!doesNodeAlreadyExist)
{
clickednode.Nodes.Add(chieldText);
}
}
else
{
clickednode.Nodes.Add(chieldText);
}
}
}
}
clickednode.Expand();
}
I want to use same as in WPF. Please help me or if you get it.

You can use MouseLeftButtonDown and check sender argument or use SelectedItemChanged

Related

Print Nested Objects in 2D

I have a list of objects with lists of child objects which also have lists of child objects, etc. Here is a simplification with objects A and H:
I would like to print them out in a 2D array, like so:
But I keep getting this:
I'm using recursion for the first time, so tracking which row I'm on for which nested level is difficult. The code I'm using is cluttered with a bunch of carve-outs, so I'm hesitant to post it.
Does anyone have some pseudo-code that could help me fix my alignment issues?
List<int> levelStart = new List<int>();
List<int> levelEnd = new List<int>();
int tempEnding = 0;
public void propertyValues2(Object inv, object[,] dataTest)
{
Type objType = inv.GetType();
PropertyInfo[] properties = inv.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
DisplayNameAttribute DNA = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault();
object propValue = property.GetValue(inv, null);
var elems = propValue as System.Collections.IList;
if (exportAll == true || exportColumns.Contains(DNA.DisplayName))
{
if (elems != null && elems.Count != 0)
{
foreach (var item in elems)
{
if (!item.GetType().FullName.StartsWith("System.String"))
{
levelStart.Add(levelStart[levelStart.Count-1]);
propertyValues2(item, dataTest);
levelStart.RemoveAt(levelStart.Count - 1);
}
else if (exportColumns.Contains(DNA.DisplayName) || exportAll == true)
{
int counter = tempEnding;
if (dataTest[tempEnding, columnIDs[DNA.DisplayName]] != null)
{
//dataRow = level[0];
for (int di = tempEnding; dataTest[di, columnIDs[DNA.DisplayName]] != null; di++)
{
tempEnding = di+1;
}
}
dataTest[tempEnding, columnIDs[DNA.DisplayName]] = item;
if (tempEnding > endingRow)
{
endingRow = tempEnding;
}
}
}
tempEnding = levelStart[levelStart.Count-1];
//dataRow = level[0];//level[0]
}
else if (elems != null && elems.Count == 0)
{
int i = 0;
}
else if (propValue != null && propValue.ToString() != "")
{
//dataRow = level[0];//level[0]
//int counter = level[level.Count-1];
if(DNA.DisplayName == "Procedure" || DNA.DisplayName == "Revenue Code")
{
if (columnIDs.Keys.Contains("Procedure") && columnIDs.Keys.Contains("Revenue Code"))
{
for (int di = tempEnding; dataTest[di, columnIDs["Procedure"]] != null || dataTest[di, columnIDs["Revenue Code"]] != null; di++)
{
tempEnding++;
}
}
}
if (dataTest[tempEnding, columnIDs[DNA.DisplayName]] != null)
{
for (int di = tempEnding; dataTest[di, columnIDs[DNA.DisplayName]] != null; di++)
{
tempEnding++;
}
}
dataTest[tempEnding, columnIDs[DNA.DisplayName]] = propValue.ToString();
if (tempEnding > endingRow)
{
endingRow = tempEnding;
}
}
}
else if (elems != null && elems.Count != 0)
{
levelStart.Add(levelStart[levelStart.Count - 1]);
foreach (var item in elems)
{
if (!item.GetType().FullName.StartsWith("System.String"))
{
propertyValues2(item, dataTest);
}
else if (exportColumns.Contains(DNA.DisplayName) || exportAll == true)
{
int counter = levelStart[levelStart.Count - 1];
if (dataTest[counter, columnIDs[DNA.DisplayName]] != null)//level[level.Count - 1]
{
for (int di = levelStart[0]; dataTest[di, columnIDs[DNA.DisplayName]] != null; di++)
{
counter++;//level[level.Count - 1]
}
}
dataTest[counter, columnIDs[DNA.DisplayName]] = item;//level[level.Count - 1]
if (endingRow < counter)
{
endingRow = counter;
}
}
levelStart[levelStart.Count - 1] = endingRow + 1;
}
levelStart.RemoveAt(levelStart.Count - 1);
}
}
foreach (string columnToCopy in columnsToCopy)
{
if (exportColumns.Contains(columnToCopy) || exportAll == true)
{
for (int i = levelStart[0] + 1; i <= endingRow; i++)//level[0]
{
dataTest[i, columnIDs[columnToCopy]] = dataTest[i - 1, columnIDs[columnToCopy]];
}
}
}
}
Array Structure:
Here is the code I worked on yesterday. It seems to work in my test runs, but I will continue testing today. It has too many row changes while printing for me to keep track of, so I'm not completely sure it always prints correctly:
int startInvoice = 1;
List<int> levelStart = new List<int> { 1 };
List<int> levelEnd = new List<int> { 1 };
List<int> maxWithinLevel = new List<int> { 0 };
int depth = 0;
public int propertyValues3(Object inv, object[,] dataTest)
{
//int maxWithinLevel = 0;
PropertyInfo[] properties = inv.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
DisplayNameAttribute DNA = (DisplayNameAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault();
object propValue = property.GetValue(inv, null);
var elems = propValue as System.Collections.IList;
//if its a list
if (elems != null && elems.Count != 0)
{
levelStart.Add(levelEnd[levelEnd.Count - 1]);
levelEnd.Add(levelStart[levelStart.Count - 1]);
foreach (var item in elems)
{
//if its a class
if (!item.GetType().FullName.StartsWith("System.String"))
{
depth++;
levelStart[levelStart.Count-1] = propertyValues3(item, dataTest);
depth--;
if (levelEnd[levelEnd.Count - 1] < levelStart[levelStart.Count - 1])
{
levelEnd[levelEnd.Count - 1] = levelStart[levelStart.Count - 1];
}
}
//if its a string
else
{
if (dataTest[levelEnd[levelEnd.Count - 1], columnIDs[DNA.DisplayName]] != null)
{
for (int i = levelEnd[levelEnd.Count - 1]; dataTest[i, columnIDs[DNA.DisplayName]] != null; i++)
{
levelEnd[levelEnd.Count - 1] = i + 1;
}
}
dataTest[levelEnd[levelEnd.Count - 1], columnIDs[DNA.DisplayName]] = item; //legit
if (levelEnd[levelEnd.Count - 1] > endingRow)//legit
{
endingRow = levelEnd[levelEnd.Count - 1];
}
}
}
levelStart.RemoveAt(levelStart.Count - 1);
//if (!elems[0].GetType().FullName.StartsWith("System.String"))
if(depth>0)
{
maxWithinLevel.Add(levelEnd[levelEnd.Count - 1]);
}
levelEnd.RemoveAt(levelEnd.Count - 1);
}
//if elems is empty
else if (elems != null && elems.Count == 0)
{
int i = 0;
}
//if it's not a list
else if (propValue != null && propValue.ToString() != "")
{
//handle procedure/revcode
if (DNA.DisplayName == "Procedure" || DNA.DisplayName == "Revenue Code")
{
if (columnIDs.Keys.Contains("Procedure") && columnIDs.Keys.Contains("Revenue Code"))
{
for (int i = levelEnd[levelEnd.Count - 1]; dataTest[i, columnIDs["Procedure"]] != null || dataTest[i, columnIDs["Revenue Code"]] != null; i++)
{
levelEnd[levelEnd.Count - 1]++;
}
}
}
if (dataTest[levelEnd[levelEnd.Count - 1], columnIDs[DNA.DisplayName]] != null)
{
for (int i = levelEnd[levelEnd.Count - 1]; dataTest[i, columnIDs[DNA.DisplayName]] != null; i++)//legit
{
levelEnd[levelEnd.Count - 1]++;
}
}
dataTest[levelEnd[levelEnd.Count - 1], columnIDs[DNA.DisplayName]] = propValue.ToString();//level[level.Count - 1]
if (levelEnd[levelEnd.Count - 1] > endingRow)
{
endingRow = levelEnd[levelEnd.Count - 1];
}
}
}
if (levelStart.Count==0 || levelStart.Max() < maxWithinLevel.Max())
{
return maxWithinLevel.Max() + 1;
}
else return levelStart.Max();
}

Unity Color Matching Game

I have searched the similar games on forum and google but i could not find exactly.
I am making a puzzle game. and user can get point if the nodes (horizontal sticks) are same color then he can get.
when they are in same direction it says colors matched but in generated node whenever i rotate the sticks it says also same.
Can you take a look? and tell me how to fix. Also if you have better idea about this matching I will be appreciated.
---------
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = Time.time;
rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
}
else if (Input.GetMouseButtonUp(0))
{
if (rayhit)
{
if (Time.time - clickTime < .2f)
{
Node node = rayhit.transform.GetComponent<Node>();
if (node != null)
{
for (int i = 0; i < node.sticks.Count; i++)
{
Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
node.sticks[i].transform.localEulerAngles = newAngles;
node.sticks[i].degree = (int)newAngles.z;
//******** HERE IS COLOR MATCHING*******
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
//if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
//{
// Debug.Log("COLORS MATCHED : " + colorMatch[1]);
//}
}
if (colorMatch[1]==colorMatch[2] && colorMatch[2]==colorMatch[3])
{
Debug.Log("COLOR MATCHED");
}
}
}
}
else
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode.isMoved == false)
{
smallestId = 0;
smallestDistance = 999;
for (int i = 0; i < nodes.Length; i++)
{
float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
if (smallestDistance > distance)
{
smallestDistance = distance;
smallestId = i;
}
}
rayhit.transform.position = nodes[smallestId].transform.position;
if (rayhit.transform.parent != nodes[smallestId].transform)
{
if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
{
if (currNode != null)
{
for (int i = 0; i < currNode.sticks.Count; i++)
{
nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
}
Destroy(rayhit.transform.gameObject);
}
}
else
{
if (currNode != null)
{
currNode.isMoved = true;
}
rayhit.transform.SetParent(nodes[smallestId].transform);
}
}
}
}
}
rayhit = new RaycastHit2D();
}
else if (Input.GetMouseButton(0))
{
if(rayhit.transform != null)
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode != null)
if (currNode.isMoved == false)
{
if (Time.time - clickTime >= 0.2f)
{
Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position = newPos;
}
}
}
}
}
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
{
Debug.Log("COLORS MATCHED : " + colorMatch[1]);
}
}
Here is working code. but how i can destroy the matched sticks?

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

Message box for the replaced values count

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

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