Iterating over ListView data - c#

I have a ListView which is bound to a DataTable. I would like to iterate over the DataTable's rows and access their data. I figured, to do this, I would simply iterate over the ListViewDataItems in the ListView. To test that I am properly accessing the data, I tried the following code, which should simply print the string at column 0 for each row.
for (int i = 0; i < MyListView.Items.Count; i++)
{
ListViewDataItem item = MyListView.Items[i];
DataRow row = (DataRow) item.DataItem;
Response.Write(row[0]);
}
However, nothing is printed. To verify that the ListView is not empty (which it shouldn't be as the data is properly rendered on my aspx page), I tried this:
Response.Write(MyListView.Items.Count);
This prints the number 16, which is correct as there are 16 rows in my ListView. I'm guessing I'm just not accessing the data correctly. I'd appreciate some insight on this.

The best way is to stop on breakpoint (at line DataRow row = (DataRow) item.DataItem;) and simply to check what you have .
for example like here :http://msdn.microsoft.com/en-us/library/ms173083(v=VS.90).aspx

I decided the best solution was to just iterate over the data directly in the DataTable rather than the ListViewDataItems.
for (int i = 0; i < myTable.Rows.Count; i++)
{
for (int j = 0; j < myTable.Columns.Count; j++)
{
object data = data.Rows[i][j];
// do stuff with data
}
}

For anyone still seeking the correct answer to this question, the following code will work (VB.NET):
Dim di as ListViewDataItem
For Each di in MyListView.Items
Response.Write(CType(di.FindControl("ExampleLabel"), Label).Text)
Next
Just substitute the Response.Write line with whatever you wanted to do to each list item. The example line is looking for a control called 'ExampleLabel', casts it back to a label then writes the text value onto the page.
Easily adapted to C# for anyone proficient (not I alas).

Related

How to add rows from one datagridview to another empty datagridview by some conditional in c#

I'm using visual studio 2017, C#, Windows Forms to create an index for words in a list of sentences.
I have two datagridview:
dataGridView2: This grid has a single column where each row contains a worded sentence.
dGvTopics: This grid has one column for every word that is repeated in the first sentence (first row) in dataGridView2, the column header text is the word.
Goal: I want to click button to categorize, inserting a row in dGvTopics for each row in dataGridView2 (sentences), place a copy of the sentence as the value for that column if the sentence contains the column header text.
My Code is:
private void btnClassify_Click(object sender, EventArgs e)
{
for (int i = 0; i < dGvTopics.Columns.Count; i++)
{
if (dataGridView2.Rows[i].Cells[0].Value.ToString().Contains(dGvTopics.Columns[i].HeaderText))
{
this.dGvTopics.Rows.Add();
this.dGvTopics.Rows[i].Cells[i].Value = dataGridView2.Rows[i].Cells[0].Value;
}
}
}
We can discuss later why you are doing this at all, there are easier ways :)
You need to understand that there are two dimensions to iterate here, the rows in dataGridView2 and the columns in dGvTopics, this means you will need two looping statements, not just one.
Your current code is looping through the Rows in dataGridView2 but only for the number of columns that are in dGvTopics which is a bit confusing.
PRO TIP: Don't use arbitrary single character variable names that have no meaning. Yes i is ubiquitously used to represent index in code you will find around the web, that doesn't mean it is good practice. i should be reserved for lazy programming where there is a single, single dimension array that you are iterating over, in your example there are 4 different levels of arrays that you accessing, the meaning of i is now ambiguous.
Instead of i, use a meaningful variable name like columnIndex or topicIndex. That way when each line is reviewed in isolation, the code is more self documenting. I would even accept t or c in this code, taking the first initial from the conceptual variable meaning will help spot common errors where the wrong indexer is used for the wrong array.
Yes this make the code wordy and long, but we're not constrained by memory space in the same way as our developer ancestors, this doesn't change the size of the final executable, strive to make your code self-documenting.
If you are programming in a code-memory-constrained environment, like for micro-controllers, or tiny chipsets, then still use meaningful short variables, not arbitrarily selected characters.
Applying the above recommendation highlights this first issue:
for (int columnIndex = 0; columnIndex < dGvTopics.Columns.Count; columnIndex ++)
{
if (dataGridView2.Rows[columnIndex].Cells[0].Value.ToString().Contains(dGvTopics.Columns[columnIndex].HeaderText))
{
this.dGvTopics.Rows.Add();
this.dGvTopics.Rows[columnIndex].Cells[columnIndex].Value = dataGridView2.Rows[columnIndex].Cells[0].Value;
}
}
Now we can see that each iteration is moving down the rows, but across the cells at the same rate, meaning that only the cells in a diagonal formation will even be compared and have a value.
The next issue is that because you are only creating a row when the comparison returns true, this means that the rows in dGvTopics might be less than you are expecting, which means less than the value of i (or columnIndex) which will raise an IndexOutOfRangeException the next successful iteration after any comparison that fails.
You can avoid this problem by iterating over the rows and columns separately and adding one row in dGvTopics for every row in dataGridView2.
We can also make the code clearer by saving a reference to the currentSentence rather than referencing the sentence through the array indexers.
private void btnClassify_Click(object sender, EventArgs e)
{
// remove any existing rows, we will reprocess all records.
this.dGvTopics.Rows.Clear();
// Iterate over the rows in the list of sentences.
for (int rowIndex = 0; rowIndex < dataGridView2.Rows.Count; rowIndex ++)
{
// Create one topic row for every sentence
// row index will always be valid now.
this.dGvTopics.Rows.Add();
// save the sentence value to simplify the comparison code.
string currentSentence = dataGridView2.Rows[rowIndex].Cells[0].Value.ToString();
// iterate over the columns in the topics grid
for (int columnIndex = 0; columnIndex < dGvTopics.Columns.Count; columnIndex ++)
{
if (currentSentence.Contains(dGvTopics.Columns[columnIndex].HeaderText))
{
this.dGvTopics.Rows[rowIndex].Cells[columnIndex].Value = currentSentence;
}
}
}
}
It's not easy to comprehend why you want to do this or how this information will be used. In general for manipulating values in cells we generally recommend that databinding techniques are used instead, that way you do not access rows and cells anymore or but the underlying objects that they represent.
demonstrating this is outside of the scope of this question, but it's an avenue worth researching when you have time.
In solutions like this where there are two grids that represent the same logical component, (in this case each row in each grid represents the same sentence value) the underlying dataobject might be a single list, where one property on the object is the sentence and each topic column is a property on the same object.
Importantly, using databinding means that the next process that needs to use the information that you have displayed or edited in the grids can do so without access to or knowledge about the grids at all... Something to think about ;)
Update
This code may result in many empty cells in the topics grid. We could instead only add rows as they are needed, but to do this will require a lot more effort.
NOTE: Grids render all the cells for each row, In the last couple of rows, there may still be empty cells if at least one of the cells for that row has a value.
private void btnClassify_Click(object sender, EventArgs e)
{
// remove any existing rows, we will reprocess all records.
this.dGvTopics.Rows.Clear();
// Iterate over the rows in the list of sentences.
for (int rowIndex = 0; rowIndex < dataGridView2.Rows.Count; rowIndex ++)
{
// save the sentence value to simplify the comparison code.
string currentSentence = dataGridView2.Rows[rowIndex].Cells[0].Value.ToString();
// iterate over the columns in the topics grid
for (int columnIndex = 0; columnIndex < dGvTopics.Columns.Count; columnIndex ++)
{
if (currentSentence.Contains(dGvTopics.Columns[columnIndex].HeaderText))
{
// first we need to know what row index to add this value into
// that involves another iteration, we could store last index in another structure to make this quicker, but here we will do it from first principals.
bool inserted = false;
for(int lookupRow = 0; lookupRow < this.dGvTopics.Rows.Count; lookupRow ++)
{
// find the first row with a null cell;
if(this.dGvTopics.Rows[columnIndex].Value == null)
{
this.dGvTopics.Rows[lookupRow].Cells[columnIndex].Value = currentSentence;
inserted = true;
break;
}
}
if(!inserted)
{
this.dGvTopics.Rows.Add();
this.dGvTopics.Rows[this.dGvTopics.Rows.Count-1].Cells[columnIndex].Value = currentSentence;
}
}
}
}
}
Many thanks to Mr Chris Schaller,
According to his description, the final code changed as follows after compiling:
private void btnClassify_Click(object sender, EventArgs e)
{
// remove any existing rows, we will reprocess all records.
this.dGvTopics.Rows.Clear();
// Iterate over the rows in the list of sentences.
for (int rowIndex = 0; rowIndex < dataGridView2.Rows.Count; rowIndex++)
{
// save the sentence value to simplify the comparison code.
string currentSentence = dataGridView2.Rows[rowIndex].Cells[0].Value.ToString();
// iterate over the columns in the topics grid
for (int columnIndex = 0; columnIndex < dGvTopics.Columns.Count; columnIndex++)
{
if (currentSentence.Contains(dGvTopics.Columns[columnIndex].HeaderText))
{
// first we need to know what row index to add this value into
// that involves another iteration, we could store last index in another structure to make this quicker, but here we will do it from first principals.
bool inserted = false;
for (int lookupRow = 0; lookupRow < this.dGvTopics.Rows.Count; lookupRow++)
{
// find the first row with a null cell;
if (this.dGvTopics.Rows[lookupRow].Cells[columnIndex].Value == null)
{
this.dGvTopics.Rows[lookupRow].Cells[columnIndex].Value = currentSentence;
inserted = true;
break;
}
}
if (!inserted)
{
this.dGvTopics.Rows.Add();
this.dGvTopics.Rows[this.dGvTopics.Rows.Count - 1].Cells[columnIndex].Value = currentSentence;
}
}
}
}
}

Using DataGridView in Visual Studio C#

I have a string that contains: "# of rows, # of columns, Row'X'Col'X'=Serial#, ...
How do I create a DataGrid table with the number of rows and columns defined, and then place the serial #s into the grid.
Examples:
2,1,R1C1=111,R2C1=112,
2,2,R1C1=211,R1C2=212,R2C1=213,R2C2=214,
thanks
Below is code that does what you are asking; however I must point out some problems with this approach. First, getting the total rows and cols from the first two elements in order to create your table is risky. If that data is wrong, this code will most likely crash or possibly omit data. Example if the input is: 2,2,RXCX=.., RXCX=.., RXCX=.., RXCX=..,RXCX=, RXCX=… This line will only get the first 4 values.
Worse… this will crash… if the input is 2,2,RXCX=.., RXCX=.. Then it will crash when you try to access the 4th element in the splitArray because there isn’t a 4th element. Either way is not good.
My point is to be safe… it would be a better approach to see how much data is actually there before you create the grid. You could get how many items there are with StringArray.Length minus the first two elements. These elements will define the dimensions and allow you to check their validity. This will make sure your loops won’t go out of bounds because the supplied data was wrong. It seems redundant and error prone to supply the dimension values when you can get that info from the data itself.
I still am not 100% sure what you want to accomplish here. It looks like a search of some form. This is what I am picturing…
Looking at your (previous) screen shots it appears to me that after you type into the Serial # text box and click the “Search Txt Files” button it will search for data that came from the input string i.e. “PLX51…” and then have the grid display the “filtered” results that match (or are LIKE) what’s in the Serial # textbox. If this is true, I would ignore the RXCX vales and put the data in a single column. Then wire up an OnKeyPress event for the text box to filter the grid whenever the user types into the Serial # text box.
Otherwise I am lost as to why you would need to create the data in the fashion described. Just because the input has unnecessary data… doesn’t mean you have to use it. Just a thought.
string inputString = "2,2,R1C1=211,R1C2=212,R2C1=213,R2C2=214";
string[] splitArray = inputString.Split(',');
int totalRows = int.Parse(splitArray[0]);
int totalCols = int.Parse(splitArray[1]);
int itemIndex = 2;
// add the columns
for (int i = 0; i < totalCols; i++)
{
dataGridView1.Columns.Add("Col", "Col");
}
// add the rows
dataGridView1.Rows.Add(totalRows);
for (int i = 0; i < totalRows; i++)
{
for (int j = 0; j < totalCols; j++)
{
dataGridView1.Rows[i].Cells[j].Value = splitArray[itemIndex];
itemIndex++;
}
}

C# ListView - Programatically editing column entries

I'm using a ListView with two columns.
To add new items (into column zero) I'm using:
listView1.Items.Add("Hello")
This results in Hello being present in the column with the lowest index.
However, I'd also like to add in line numbers.
I'm trying this with:
for (int c = 0; c < listView1.Items.Count; c++)
{
listView1.Items[c].SubItems.Clear();
listView1.Items[c].SubItems.Add(c.ToString());
}
The problem, I believe, lies with the main item(s) being cleared even though I'm just attemping to clear() the subitems (i.e. the entry in the second column).
So essentially my question is:
How do I individually edit column entries to be able to display the lines/row numbers?
First item (index 0) in SubItems is actually main item in left column.
If you already added sub item like this listViewItem.SubItems.Add(string.Empty); then you can edit that sub item like this:
for (int c = 0; c < listView1.Items.Count; c++)
{
listView1.Items[c].SubItems[1].Text = c.ToString();
}
I may have solved it...
It may be that once a SubItem is added, the main item is moved to SubItem[0].
The following code seems to be working for me, but I'm not sure if a better way exists.
for (int c = 0; c < listView1.Items.Count; c++)
{
listView1.Items[c].SubItems[0].Text = listView1.Items[c].Text;
listView1.Items[c].SubItems.Add(c.ToString());
listView1.Items[c].SubItems[1].Text = c.ToString();
}

C# - DataGridView - Breaking out of for loop without being told to

I have the following code:
for (int i = 0; i < COLUMNS.Count; i++)
{
DataGridViewColumn column = new DataGridViewColumn()
{
Name = COLUMNS.ElementAt(i).Key,
HeaderText = COLUMNS.ElementAt(i).Value,
Width = 60
};
operationsDataGridView.Columns.Add(column);
}
I hope it's clear enough, I'm looping through a Dictionary of Column Name and Column Headers, and adding them as DataGridViewColumn instances to the DataGridView.
However, the program is looping only once, when it should loop 9 times (I've already debugged it, and COLUMNS.Count is, in fact, 9)
Therefore, only the first column is added, as it can be seen in the image:
As for the debugging output, these strange messages appear:
I'm sure it has something to do with this problem.
Interesting Fact!
If I change my code to:
for (int i = 0; i < COLUMNS.Count; i++)
{
operationsDataGridView.Columns.Add(COLUMNS.ElementAt(i).Key,
COLUMNS.ElementAt(i).Value);
}
(Which I think, is esentially the same), the program works fine!!
I was able to reproduce this error on my end, but I was getting an exception as well about "At least one of the DataGridView control's columns has no cell template."
If you change your loop to DataGridViewColumn column = new DataGridViewTextBoxColumn() it will probably work (fixed it on my end at least). Looks like the .add method with the two parameters defaults to this type of column.

C# Index was outside the bounds of the array

I'm hoping someone can help with an exception I've inherited. Basically I'm writing the rows in a datagrid to a text file. Which works fine apart from when the row is hidden, when the row is hidden the exception "Index was outside the bounds of the array" is thrown at the line highlighted below. Thanks for any help.
DataRow dr;
for (int i = 0; i < bindingManagerBase.Count; i++)
{bindingManagerBase.Position = i;
dr = ((DataRowView)bindingManagerBase.Current).Row;
bindingManagerBase.SuspendBinding();
try
{
int rowIndex = dr.Table.Rows.IndexOf(dr);
if (!rowsDisplayStatus[rowIndex]) //<---------Exception here "Index was outside the bounds of the array" //Picture below
{
m_Dgv.Rows[rowIndex].Visible = false;
continue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
bindingManagerBase.ResumeBinding();
}
writeData(tw, dr);
iIndex++;
}
You're getting the row index and then trying to use it with rowsDisplayStatus. You can't use the database row index as an index into your collections.
I would change:
if (!rowsDisplayStatus[rowIndex])
to:
if (!rowsDisplayStatus[i])
How is rowsDisplayStatus populated? If it only contains one element and something is expected to be at index 9, you should take a look at the code that populates it.
As the picture shows, rowsDisplayStatus has 1 item in it... you're trying to pull the 10th item (or the item at index 9)... that index is out of range.
How is "rowsDisplayStatus" populated? Maybe there is a problem in that routine.
I'll explain what mark said, above. (Don't know how to comment, so putting this in an answer)
He's correct you should change the
if (!rowsDisplayStatus[rowIndex])
Into
if (!rowsDisplayStatus[i])
The reason for this is as follows:
The rowIndex grows even when rows where previously deleted from the Rows object. So there could be only one or two rows in the dr.Table.Rows, but they could have indexes (indice) for example of 8 and 9 (because previously rows 1 to 7 where erased or for other reasons).
So you get the current rowIndex by checking the bindingManager.Current.RowIndex property.
But your rowsDisplayStatus is a simple array (or ArrayList) with the correct amount of rows according to i. So for row index:8 (the first row) you should look at rowsDisplayStatus[0] (which is the value of i), and for row index:9 (the second row) you should look at rowsDisplayStatus[1]... etc.
HTH, Moshe
Basically, whenever I've received this error the index (or value that I have set) was non-existant at the time it was being referenced.
For example if I have two Items in a ListBox and I tryi to reference a third Item, I will receive an Index Out Of Range exception (many times in the past)...

Categories

Resources