Listview elements being shifted right - c#

I'm creating a listview element with 14 columns, and i seems like everything is being read into the listview as it should, however, it is all shifted 1 column to the right?
This is my code:
listView2.Items.Clear();
List<string> Event = new List<string>();
List<string> Currency = new List<string>();
List<string> Ammount = new List<string>();
List<string> DateAndTime = new List<string>();
List<string> customerName = new List<string>();
List<string> mpnumber = new List<string>();
List<string> Comment = new List<string>();
List<string> transactionid = new List<string>();
List<string> transferid = new List<string>();
List<string> paymentpoint = new List<string>();
List<string> myshopnumber = new List<string>();
List<string> Bankaccount = new List<string>();
List<string> Date = new List<string>();
List<string> time = new List<string>();
foreach (ListViewItem item in listView1.Items)
{
Event.Add(item.SubItems[0].Text);
Currency.Add(item.SubItems[1].Text);
Ammount.Add(item.SubItems[2].Text);
DateAndTime.Add(item.SubItems[3].Text);
customerName.Add(item.SubItems[4].Text);
mpnumber.Add(item.SubItems[5].Text);
Comment.Add(item.SubItems[6].Text);
transactionid.Add(item.SubItems[7].Text);
transferid.Add(item.SubItems[8].Text);
paymentpoint.Add(item.SubItems[9].Text);
myshopnumber.Add(item.SubItems[10].Text);
Bankaccount.Add(item.SubItems[11].Text);
Date.Add(item.SubItems[12].Text);
time.Add(item.SubItems[13].Text);
}
List<int> indexOfRandom = new List<int>();
var random = new Random();
var lower = 0;
var upper = Event.Count;
for (int k = 0; k < numberOfRandoms; k++)
{
var rannum = random.Next(lower, upper);
indexOfRandom.Add(rannum);
}
listView2.BeginUpdate();
for (int i = 0; i < numberOfRandoms; i++)
{
ListViewItem randomrows = new ListViewItem();
randomrows.SubItems.Add(Event[i]);
randomrows.SubItems.Add(Currency[i]);
randomrows.SubItems.Add(Ammount[i]);
randomrows.SubItems.Add(DateAndTime[i]);
randomrows.SubItems.Add(customerName[i]);
randomrows.SubItems.Add(mpnumber[i]);
randomrows.SubItems.Add(Comment[i]);
randomrows.SubItems.Add(transactionid[i]);
randomrows.SubItems.Add(transferid[i]);
randomrows.SubItems.Add(paymentpoint[i]);
randomrows.SubItems.Add(myshopnumber[i]);
randomrows.SubItems.Add(Bankaccount[i]);
randomrows.SubItems.Add(Date[i]);
randomrows.SubItems.Add(time[i]);
listView2.Items.Add(randomrows);
}
listView2.EndUpdate();
Hope the question makes sense, feel free to ask me for information
My listview1 is essentially a long list of transactions that works well, however it seems to shift it all 1 to the right when i transfer it to listview2?

The answer was posted by #hansPassant, it was:
"The 1st column is filled with the ListViewItem.Text property, the sub-items start at the 2nd column. So it needs to be ListViewItem randomrows = new ListViewItem(Event[i]); etc..."

Related

Strike through string PowerPoint interop

I have 2 PowerPoints one original and one edited. I each PowerPoint I have a textbox that contains a number of words. The idea is to find any words added or deleted from the original PowerPoint in the edited and put them in a list. I then want to put a strike though these words that have been added or deleted but I can't access any of the properties I need as I have a string and not a TextFrame. Any ideas how I could strike through the words? E.g original "This Text" edited "This New Text" outcome "This New Text".
My code is as fallows
List<int> originalShapesListID = new List<int>();
List<int> editedShapesListID = new List<int>();
List<int> originalListID = new List<int>();
List<int> editedListID = new List<int>();
List<Microsoft.Office.Interop.PowerPoint.Shape> originalList = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
List<Microsoft.Office.Interop.PowerPoint.Shape> editList = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
Microsoft.Office.Interop.PowerPoint.Shape editedShpID;
List<Microsoft.Office.Interop.PowerPoint.Shape> originalListWords = new List<Microsoft.Office.Interop.PowerPoint.Shape>();
List<char> editListWords = new List<char>();
editedShpID = null;
Microsoft.Office.Interop.PowerPoint.Shape originalShpID;
long editedShapeID;
long originalShapeID;
editedShapeID = 0;
originalShapeID = 0;
originalShpID = null;
originalShp = null;
editShp = null;
Microsoft.Office.Interop.PowerPoint.TextFrame txtFrame;
char delimiter = Convert.ToChar(" ");
List<string> one = new List<string>();
List<string> two = new List<string>();
int indexOfWord = 0;
Getting all Text
String pps = "";
foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in Originalslides)
{
foreach (Microsoft.Office.Interop.PowerPoint.Shape originalShape in slide.Shapes)
{
originalShp = originalShape;
if (originalShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
{
var textFrame = originalShape.TextFrame;
if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
{
var textRange = textFrame.TextRange;
pps += originalShape.TextFrame.TextRange.Text;
foreach (char word in pps)
{
l.Add(word);
Debug.WriteLine(word);
}
}
}
originalShapesListID.Add(originalShape.Id);
originalShapeID = originalShape.Id;
originalList.Add(originalShape);
}
originalListID.Add(slide.SlideID);
}
foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in EditedSlides)
{
foreach (Microsoft.Office.Interop.PowerPoint.Shape editShape in slide.Shapes)
{
editShp = editShape;
if (editShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
{
var textFrame = editShape.TextFrame;
if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
{
var textRange = textFrame.TextRange;
pps += editShape.TextFrame.TextRange.Text;
foreach (char word in pps)
{
l.Add(word);
Debug.WriteLine(word);
}
}
}
editedShapesListID.Add(editShape.Id);
editedShapeID = editShape.Id;
editList.Add(editShape);
}
editedListID.Add(slide.SlideID);
}
Checking if there is an added or deleted word
var q = from original in originalList
join editedTmp in editList on original.Id equals editedTmp.Id into g
from edited in g.DefaultIfEmpty()
select new
{
original,
edited
};
//foreach through both lists
foreach (var item in q)
{
List<string> diff;
var originalString = item.original.TextFrame.TextRange.Text;
var editString = item.edited.TextFrame.TextRange.Text;
var firstStringList = originalString.Split(delimiter).ToList();
var secondStringList = editString.Split(delimiter).ToList();
if (secondStringList.Count() > firstStringList.Count())
{
diff = secondStringList.Except(firstStringList).ToList();
//change to blue for added word
}
else
{
diff = firstStringList.Except(secondStringList).ToList();
// change to red for deleted word
}
}

C# WPF dynamic listView without SubItems

I have ListView that is dynamic and Columns name need to change dynamic so i create this:
var gridView = new GridView();
this.lvWorkers.View = gridView;
foreach(string c in columnName)
{
gridView.Columns.Add(new GridViewColumn
{
Header = c.Remove(0, 5),
DisplayMemberBinding = new Binding(c)
});
}
Now i want add Items to this list but there is no SubItems like in Win Forms. I find similar problems, the answer was to make class that is defined. Add Items to Columns in a WPF ListView
I need to add Items dynamic.
Win Forms which is not work.
foreach (OneStudentEvent e in oneEventList)
{
ListViewItem item = new ListViewItem(e.Indeks.ToString());
item.SubItems.Add(e.eventString);
...
lvWorkers.Items.Add(item);
}
EDIT
Now i remove DisplayMemberBinding = new Binding(c)
and add:
lvWorkers.ItemsSource = getList();
private ArrayList getList()
{
ArrayList data = new ArrayList();
for(int i=0; i<20; i++)
{
List<string> tempList = new List<string>();
for(int j = 0; j < 30; j++)
{
tempList.Add(j.ToString());
}
data.Add(tempList);
}
return data;
}
And i don't see string in list but word: (Collection). I know that this is my bad to show collection no single string, but i don't know hot to make it.

How to vertically populate each column of listview with lists

I have data which I import from textfiles, transfer to an sqlite db, then transfer that into LISTS. Now I want to show these lists to the user in a LISTVIEW. Each column should contain a list.
Below, the contents of the second column are perfectly fine. But, the 3rd and the fourth column should be vertically filled with the items that correspond to their color values.
I.e from the code below, I want to populate the 3rd and 4th column with the MaxLen list and PercentPopList list. (Yes I know that I skipped a column, but i'll fix that later)
I'm new to programming and I cant figure out how to make that work.
ListViewItem lvi = new ListViewItem();
foreach (object o in SeqIrregularities)
{
lvi.SubItems.Add(o.ToString());
listView1.Items.Add(lvi);//Adds a new row
lvi = new ListViewItem();
}
listView1.Items.Add(lvi);//Adds a new row
lvi = new ListViewItem();
foreach (object a in MaxLen)
{
lvi.SubItems.Add(a.ToString());
}
listView1.Items.Add(lvi);//Adds a new row
lvi = new ListViewItem();
foreach (object b in PercentPopList)
{
lvi.SubItems.Add(b.ToString());
}
listView1.Items.Add(lvi);//Adds a new row
Your data is in the wrong format. Simplifying, you seem to have data like this:
var firstColumnValues = new List<int>();
var secondColumnValues = new List<int>();
var thirdColumnValues = new List<int>();
While each individual list may make sense on its own, they currently don't relate to each other in any meaningful way. What you need to do is think about the format of the object which represents one "record" of data. Still overly-simplified, something like this:
class RecordOfValues
{
public int FirstValue { get; set; }
public int SecondValue { get; set; }
public int ThirdValue { get; set; }
}
Then you'd just have one list of them:
var listOfRecords = new List<RecordOfValues>();
At this point you would have a list of "records" to bind to the ListView.
As far as I understand that you don't have too much experience and cannot easily come up with an algorithm accounting for the suggested modifications; here you have a sample code showing how to extend your code to populate three different columns from 3 different lists:
List<string> SeqIrregularities = new List<string>();
SeqIrregularities.Add("1");
SeqIrregularities.Add("2");
SeqIrregularities.Add("3");
List<string> MaxLen = new List<string>();
MaxLen.Add("4");
MaxLen.Add("5");
MaxLen.Add("6");
List<string> PercentPopLis = new List<string>();
PercentPopLis.Add("7");
PercentPopLis.Add("8");
PercentPopLis.Add("9");
PercentPopLis.Add("10");
PercentPopLis.Add("11");
int totItems = SeqIrregularities.Count - 1;
if (MaxLen.Count - 1 > totItems) totItems = MaxLen.Count - 1;
if (PercentPopLis.Count - 1 > totItems) totItems = PercentPopLis.Count - 1;
for (int i = 0; i <= totItems; i++)
{
ListViewItem lvi = new ListViewItem();
string item1 = "";
string item2 = "";
string item3 = "";
if (SeqIrregularities.Count - 1 >= i) item1 = SeqIrregularities[i];
if (MaxLen.Count - 1 >= i) item2 = MaxLen[i];
if (PercentPopLis.Count - 1 >= i) item3 = PercentPopLis[i];
lvi.SubItems.Add(item1);
lvi.SubItems.Add(item2);
lvi.SubItems.Add(item3);
listView1.Items.Add(lvi);
}

Exception Title: Check if object is Null

I created table N_Roles_Users in my database, I want to show it's value if username matches with existing login user.
I wrote this code below. But it is generating exception that check if object is Null.
// currentUser="UserA";
public List<string> GetUserRoles( string currentUser)
{
N_Roles_Users allroles = new N_Roles_Users(); //N_Roles_Users is database table name.
List<string> roleslist = new List<string>();
List<char> temp = new List<char>();
temp = allroles.user_name.ToList();
List<char> tempa = new List<char>();
tempa = allroles.role_name.ToList();
for (int i = 0; i < temp.Count; i++) // Loop through List with for
{
if (currentUser == temp[i].ToString())
{
roleslist.Add(tempa[i].ToString());
MessageBox.Show(tempa[i].ToString());
}
}
return roleslist;
}
Can anyone guide me how to resolve this problem?
temp = allroles.user_name.ToList(); is the line of exception i guess.
set allroles.user_name = "some value" before
this line
temp = allroles.user_name.ToList();
Happy Coding :)
Check this condition
// currentUser="UserA";
public List<string> GetUserRoles( string currentUser)
{
N_Roles_Users allroles = new N_Roles_Users();
List<string> roleslist = new List<string>();
List<char> temp = new List<char>();
**if(allroles.user_name.ToList()!=null && allroles.user_name.ToList().Count!=0)
{
temp = allroles.user_name.ToList();
}**
List<char> tempa = new List<char>();
tempa = allroles.role_name.ToList();
for (int i = 0; i < temp.Count; i++) // Loop through List with for
{
if (currentUser == temp[i].ToString())
{
roleslist.Add(tempa[i].ToString());
MessageBox.Show(tempa[i].ToString());
}
}
return roleslist;
}

Use of unassigned local variable

While I am trying to debug this code (in C# WinForms), it shows an error
"use of unassigned local variable" at 'arrlist[i]'
Since I'm comparing it with a database variable, I cannot initialize the size of the array.
This is the code:
if (count != 0)
{
OleDbCommand cmd1 = new OleDbCommand(
"select seat_no, booking_date, show_time "+
"from tickets "+
"where ticket_no = (select max(ticket_no) from tickets)", c);
OleDbDataReader oledb1 = cmd1.ExecuteReader();
oledb1.Read();
string retr_seats = oledb1.GetString(0);
char comma = ',';
string[] strarray = retr_seats.Split(comma);
int ticket_length = strarray.Length;
string[] arrlist;
int i = 0;
foreach(var control in this.Controls)
{
if(control is Label)
{
arrlist[i] = control.ToString();
i++;
}
}
for(var j=0;j<=ticket_length;j++)
{
for (var k = 0; k <= i-1; k++)
{
if (arrlist[k].Contains(strarray[j]))
{
MessageBox.Show(strarray[j]);
}
}
}
}
Please help me
You need to initialize the variable arrlist. Change this line:
string[] arrlist;
To this:
string[] arrlist = new string[this.Controls.Count]; // Must be big enough.
Or better, use a dynamically sized container such as a List<string>.
List<string> arrList = new List<string>();
foreach(var control in this.Controls)
{
if(control is Label)
{
arrlist.Add(control.ToString());
}
}
Or use LINQ to get the result directly:
string[] arrlist = this.Controls
.OfType<Label>()
.Select(control => control.ToString())
.ToArray();
Change your array to a list, and add values to the list. You can then index the list elements directly, or if you need an array, you can use .ToArray() on the list instance.
Also note that your for loop over j will go out of bounds on strarray unless you change the comparison to < ticket_length from <= ticket_length.
...
var arrlist = new List<string>();
foreach(var control in this.Controls)
if(control is Label)
{
arrlist.Add(control.ToString());
}
for(var j=0;j<ticket_length;j++)
for (var k = 0; k < arrlist.Count; k++)
if (arrlist[k].Contains(strarray[j]))
MessageBox.Show(strarray[j]);
string[] arrlist;
....
arrlist[i] = control.ToString();
you lost initialization like: arrlist = new string[count];
The problem is that arrlist is defined, but not initialized.
You need to initialize it, like this:
string[] arrlist = new arrlist[size];
If you don't know how big it will be, it is better to use a list:
List<string> arrlist = new List<string>();
and to add items: arrlist.add("some string");

Categories

Resources