I have problem with messages in mail program. If i have 20 mails it's ok load pretty fast but if i have 700 it's a problem it take a long of time. I can go make coffe and back. How I can set max messages to 50.
try
{
client.Connect(comboBox5.Text, 995, true);
client.Authenticate(textBox6.Text, textBox5.Text, OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
int count = client.GetMessageCount();
string htmlContained = "";
for (int i = count; i >= 1; i -= 1)
{
OpenPop.Mime.Message message = client.GetMessage(i);
OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();
if (html != null)
{
htmlContained = html.GetBodyAsText();
} else
{
html = message.FindFirstPlainTextVersion();
if (html != null)
{
htmlContained = html.GetBodyAsText();
}
}
string name = message.Headers.Subject;
if (name == "")
{
name = "Usigned";
}
dt.Rows.Add(new object[] { name.ToString(), message.Headers.From.DisplayName, message.Headers.From.Address, htmlContained, message.Headers.DateSent });
}
client.Disconnect();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
for (int i = count; i >= 1; i -= 1)
->
for (int i = 0; i < count && i < 50; i++)
Or (less redable according to me):
for (int i = count; i >= 1; && (count - i) < 50; i -= 1)
You may also want to implement paging:
var pageSize = 50;
var startPage = x;
for (int i = x * pageSize; i < count && i < pageSize; i++)
Related
I'm trying to reverse my bubble sort so all null elements are pushed to the end of array instead of the beginning as they're getting sorted now. Any advice on how this can be achieved?
for (int outerLoop = 0; outerLoop < students.Length-1; outerLoop++)
{
for (int innerLoop = 0; innerLoop < students.Length-1; innerLoop++)
{
if (students[outerLoop+1] == null)
{
var tempObject = students[outerLoop+1];
students[outerLoop+1] = students[innerLoop];
students[innerLoop] = tempObject;
}
}
}
Corrected code:
for (int outerLoop = 0; outerLoop < students.Length-1; outerLoop++)
{
for (int innerLoop = 0; innerLoop < students.Length-1; innerLoop++)
{
if (students[innerLoop] == null)
{
var tempObject = students[innerLoop+1];
students[innerLoop+1] = students[innerLoop];
students[innerLoop] = tempObject;
}
}
}
This will not sort your array but only drop the nulls at the bottom.
In fact you can do away with the temp variable:
for (int outerLoop = 0; outerLoop < students.Length-1; outerLoop++)
{
for (int innerLoop = 0; innerLoop < students.Length-1; innerLoop++)
{
if (students[innerLoop] == null)
{
students[innerLoop] = students[innerLoop+1];
students[innerLoop+1] = null;
}
}
}
Note:
C# 7 introduced tuples which enables swapping two variables without a temporary one:
int a = 10;
int b = 2;
(a, b) = (b, a);
This assigns b to a and a to b.
Try following. You have to test for outer being null and inner not being null :
for (int outerLoop = 0; outerLoop < students.Length - 1; outerLoop++)
{
for (int innerLoop = outerLoop + 1; innerLoop < students.Length; innerLoop++)
{
if ((students[outerLoop] == null) && (students[innerLoop] != null))
{
var tempObject = students[outerLoop];
students[outerLoop] = students[innerLoop];
students[innerLoop] = tempObject;
}
}
}
There's a faster, easier and stable approach with O(n) time complexity:
int at = 0;
for (int i = 0; i < students.Length; ++i)
if (students[i] != null)
students[at++] = students[i];
for (int i = at; i < students.Length; ++i)
students[i] = null;
However, if you insist on bubble sort: we can implement it in a bit different way
for (bool wrongOrder = true; wrongOrder;) {
wrongOrder = false;
for (int i = 1; i < students.Length; i++) {
if (students[i - 1] == null && students[i] != null) {
// At least 2 items has wrong order, swap them
var temp = students[i - 1];
students[i - 1] = students[i];
students[i] = temp;
wrongOrder = true;
}
}
}
I have a DevExpress gridview with a description column that has column header filters enabled. The problem is that having these filters enabled makes it so an entry is added to the drop down for every data item in the list which isn't really desirable because the entries are generally paragraphs of text. Instead I would like to restrict the options to just All, Blanks, and Non blanks but I haven't been able to find a clear example of how this might be accomplished. Thanks for the help!
ANSWER:
settings.HeaderFilterFillItems = (sender, e) =>
{
if (e.Column.FieldName == "Description")
{
e.Values.Clear();
e.AddShowAll();
e.Values.Insert(1, FilterValue.CreateShowBlanksValue(e.Column, "(Blanks)"));
e.Values.Insert(2, FilterValue.CreateShowNonBlanksValue(e.Column, "(Non Blanks)"));
}
};
This question is similar than yours https://www.devexpress.com/Support/Center/Question/Details/Q477323/gridview-how-to-customize-header-filter-items
And here in the view you have the custom filter items, using settings.HeaderFilterFillItems https://demos.devexpress.com/MVCxGridViewDemos/Filtering/Filtering
#Html.DevExpress().GridView(
settings => {
settings.Name = "gvFiltering";
settings.CallbackRouteValues = new { Controller = "Filtering", Action = "FilteringPartial", EnableCheckedListMode = ViewBag.EnableCheckedListMode };
settings.Width = Unit.Percentage(100);
settings.Columns.Add("CompanyName");
settings.Columns.Add("Country");
settings.Columns.Add("City");
settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";
settings.Columns.Add("Quantity");
settings.Columns.Add("Discount").PropertiesEdit.DisplayFormatString = "p0";
settings.Columns.Add(column => {
column.FieldName = "Total";
column.PropertiesEdit.DisplayFormatString = "c";
column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
column.UnboundExpression = "UnitPrice * Quantity * (1 - Discount)";
});
settings.Settings.ShowHeaderFilterButton = true;
settings.SettingsPopup.HeaderFilter.Height = 200;
var headerFilterMode = ViewBag.EnableCheckedListMode ? GridHeaderFilterMode.CheckedList : GridHeaderFilterMode.List;
foreach(GridViewDataColumn column in settings.Columns)
column.SettingsHeaderFilter.Mode = headerFilterMode;
settings.HeaderFilterFillItems = (sender, e) => {
ASPxGridView grid = (ASPxGridView)sender;
if(e.Column.FieldName == "Total") {
e.Values.Clear();
if(e.Column.SettingsHeaderFilter.Mode == GridHeaderFilterMode.List)
e.AddShowAll();
int step = 100;
for(int i = 0; i < 10; i++) {
double start = step * i;
double end = start + step - 0.01;
e.AddValue(string.Format("from {0:c} to {1:c}", start, end), string.Empty, string.Format("[Total] >= {0} and [Total] <= {1}", start, end));
}
e.AddValue(string.Format("> {0:c}", 1000), string.Empty, "[Total] > 1000");
} else if(e.Column.FieldName == "Quantity") {
int max = 0;
for(int i = 0; i < e.Values.Count; i++) {
int value;
if(!int.TryParse(e.Values[i].Value, out value))
continue;
if(value > max)
max = value;
}
e.Values.Clear();
if(e.Column.SettingsHeaderFilter.Mode == GridHeaderFilterMode.List)
e.AddShowAll();
int step = 10;
for(int i = 0; i < max / step + 1; i++) {
int start = step * i;
int end = start + step - 1;
e.AddValue(string.Format("from {0} to {1}", start, end), string.Empty, string.Format("[Quantity] >= {0} and [Quantity] <= {1}", start, end));
}
}
};
}).Bind(Model).GetHtml()
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;
}
}
}
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.
I have a combobox outside my tabcontrol. In each tab control there is a datagridview full of values. In the combobox you can choose a conversion for all values. For example eV→meV.
When i am in the first tab and use the combobox there are no problems, but after i switch the tab and then wanna use the combobox the program list down however the whole combobox is full of try/catch
private void OpenB_Click(object sender, EventArgs e)
{
string[] result = new string[2];
bool lesen = false;
int Spalte = 0;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//Datagridview will be rested, so all values and rows are removed
for (int i = 1; i < Anzahl; i++)
{
DGV[i].Rows.Clear();
}
Anzahl = openFileDialog1.FileNames.Length;
counter = new int[Anzahl];
try
{
if (tabControl1.TabCount < Anzahl)
{
for (int i = 1; i <= Anzahl; i++)
{
if (i > tabControl1.TabCount)
{
string title = "Tab " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
DataGridView NewDGV = new DataGridView();
NewDGV.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
NewDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
NewDGV.Columns.Add("Energy", "Energy");
NewDGV.Columns[0].ReadOnly = true;
NewDGV.Columns.Add("Count Rate", "Count Rate");
NewDGV.Columns[1].ReadOnly = true;
NewDGV.Location = new System.Drawing.Point(3, 3);
NewDGV.Name = "NewDGV" + Convert.ToString(i);
NewDGV.RowHeadersVisible = false;
NewDGV.Size = new System.Drawing.Size(276, 379);
NewDGV.TabIndex = i;
foreach (DataGridViewColumn col in NewDGV.Columns)
col.SortMode = DataGridViewColumnSortMode.NotSortable;
NewDGV.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
DGV.Add(NewDGV);
tabControl1.TabPages[i - 1].Controls.Add(NewDGV);
}
}
}
else if (tabControl1.TabCount > Anzahl)
{
for (int i = tabControl1.TabCount - 1; i >= Anzahl; i--)
{
tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
}
}
}
catch { }
try
{
//Double arrays and Datagridview will be attuned to the count of data
eV = new double[openFileDialog1.FileNames.Length][];
meV = new double[openFileDialog1.FileNames.Length][];
cm = new double[openFileDialog1.FileNames.Length][];
CR = new double[openFileDialog1.FileNames.Length][];
CRmax = new double[openFileDialog1.FileNames.Length][];
for (int i = 0; i < Anzahl; i++)
{
//Naming the columns after data names
string[] Dateiname = openFileDialog1.FileNames[i].Split('\\');
int L = Dateiname.Length;
tabControl1.TabPages[i].Text = Dateiname[L-1];
}
}
catch
{
}
//Datafiles will be read one by one
DataRead(result, ref lesen, ref Spalte);
}
}
/// Reading loop
///
/// double[] eV2 = Energy values of the current data file in eV
/// double[] meV2 = Energy values of the current data file in meV
/// double[] cm2 = Energy values of the current data file in cm^-1
/// double[] CR2 = Intensities of the current data file in CR
/// double[] CRmax2 = normalizied Intensities of the current data file in 1/CRmax
private void DataRead(string[] result, ref bool lesen, ref int Spalte)
{
for (Spalte = 0; Spalte < Anzahl; Spalte++)
{
string line;
lesen = false;
counter[Spalte] = 0;
try
{
Ursprung = openFileDialog1.FileNames[Spalte];
//initialize stream reader
System.IO.StreamReader file1 = new System.IO.StreamReader(openFileDialog1.FileNames[Spalte]);
//read line per line in stream reader
while (((line = file1.ReadLine()) != null))
{
counter[Spalte]++;
Count2 = counter[Spalte];
Count2 = Count2 / 2;
try
{
string[] splitter = line.Split(' ');
if ((splitter[0] == "S") && (splitter[1] == "0000"))
{
lesen = true;
counter[Spalte] = 0;
}
if (lesen == true)
{
//Rows will be filled an added with data value strings
if (counter[Spalte] % 2 == 0)
{
result[0] = splitter[2];
}
else
{
result[1] = splitter[2];
dataGridView1.Rows.Add();
DGV[Spalte].Rows.Add();
int Zeile = (counter[Spalte] - 1) / 2;
DGV[Spalte][0, Zeile].Value = result[0];
DGV[Spalte][1, Zeile].Value = result[1];
}
}
}
catch
{
continue;
}
}
//Streamreader is closed
file1.Close();
counter[Spalte] = counter[Spalte] / 2;
//Current datagridviw values are saved in arrays
//The conversions will be calculated and saved in new arrays
//So every unit gets its own array
double[] eV2 = new double[counter[Spalte]];
double[] meV2 = new double[counter[Spalte]];
double[] cm2 = new double[counter[Spalte]];
double[] CR2 = new double[counter[Spalte]];
double[] CRmax2 = new double[counter[Spalte]];
//Conversion calculation
for (int i = 0; i < counter[Spalte]; i++)
{
eV2[i] = Convert.ToDouble(DGV[Spalte][0, i].Value);
CR2[i] = Convert.ToDouble(DGV[Spalte][1, i].Value);
meV2[i] = 1000 * eV2[i];
cm2[i] = 8066 * eV2[i];
}
//Current file's arrays are saved in double arrays
eV[Spalte] = eV2;
CR[Spalte] = CR2;
meV[Spalte] = meV2;
cm[Spalte] = cm2;
for (int i = 0; i < counter[Spalte]; i++)
{
CRmax2[i] = CR2[i] / CR2.Max();
}
CRmax[Spalte] = CRmax2;
//Chosen conversion replaces values in datagridview
if (Hilfe == 1)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV2[i];
}
}
else if (Hilfe == 2)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = cm2[i];
}
}
if (Hilfe2 == 1)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][1, i].Value = CRmax2[i];
}
}
}
catch
{
MessageBox.Show("Es ist ein Fehler beim Einlesen eingetreten");
}
}
}
/// Energy Unit
/// Choses between eV, meV, 1/cm
/// Datagridview values are replaced by the unit array values
/// Hilfe... Saves current energy unit
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int L = comboBox1.SelectedIndex;
try
{
if (L == 0)
{
Hilfe = 1;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV[Spalte][i];
}
}
}
catch
{
}
}
if (L == 1)
{
Hilfe = 2;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = cm[Spalte][i];
}
}
}
catch
{
}
}
if (L == 2)
{
Hilfe = 0;
try
{
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = eV[Spalte][i];
}
}
}
catch
{
}
}
}
catch { }
}
Accept these answer if you dont know how to post your own answer?.
Answer:
Problem fixed. The reason was the dgv.autosizemode , when i deactivate it before the conversions it runs.
for (int Spalte = 0; Spalte < Anzahl; Spalte++)
{
DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
for (int i = 0; i < counter[Spalte]; i++)
{
DGV[Spalte][0, i].Value = meV[Spalte][i];
}
DGV[Spalte].AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
}