Table content to list view - c#

Hello i have got an problem i need to show the contents of an database table the name is Employees.
it needs to get shown in an list view or something simular.
i cant get the listview box to show the table contents.

I think following code should work for you:
private void BindTable(DataTable table)
{
lvItemList.Clear();
foreach (DataRow row in table.Rows)
{
ListViewItem lvItem = new ListViewItem();
lvItem.Text = row["ColumnName"].ToString();
lvItemList.Items.Add(lvItem);
// Or in a one-liner
//lvItemList.Items.Add(row["ColumnName"].ToString();
}
}

You need to override the toString() method and provide the string data you want to display.

Related

How to append selected items in WPF c# listbox?

I want to append all the selected items in listbox. When i am trying the following code i got "System.Data.DataRowView\r\nSystem.Data.DataRowView\r\nSystem.Data.DataRowView\r\nSystem.Data.DataRowView\r\n" How to get the actual data?
StringBuilder sb = new StringBuilder();
foreach (object o in lstBoxRoles.SelectedItems)
{
sb.AppendLine(o.ToString());
}
string s = sb.ToString();
According to comments: You use DataTable and DataRow to present data in listbox (I think it is a DataGrid or ListView btw).
As you see, Selected item contains the whole View of Row.
If your stuff - DataRow - has ToString() implementation, modify your code:
sb.AppendLine(((DataRowView)o).Row.ToString());
If you need a specific column to append:
sb.AppendLine(((DataRowView)o).Row["SpecColumnName"].ToString());

Store elements from a ListView into a List

Since I haven't found anything that helped, I ask my question here:
I have a ListView where I select a whole row by click. Now I want to store these selected items into a List but don't know how this should work exactly.
List<String> itemSelected = new List<String>();
foreach (var selectedRow in listView1.SelectedItems)
{
itemSelected.Add(selectedRow);
}
That doesn't work because I need an index (selectedRow[?]) or something like that. How can I store the values of the first column when clicked the row?
EDIT: The problem is that the ListViewItems have the type "object"
The ListView gets populated this way:
using (SqlConnection connection = new SqlConnection(connectionQuery))
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
col1 = row.Cells[col1.Text].Value.ToString();
col2 = row.Cells[col2.Text].Value.ToString();
col1Cells.Add(col1);
col2Cells.Add(col2);
}
}
You can do something like:
ListViewItem listViewItem = this.listView1.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
if (listViewItem != null)
{
string firstColumn = listViewItem.Text;
string secondColumn = listViewItem.SubItems[0].Text;
// and so on with the SubItems
}
If you have more selected items and only need the values of the first columns you can use:
List<string> values = listView1.SelectedItems.Cast<ListViewItem>().Select(listViewItem => listViewItem.Text).ToList();
It's common to bind a ListView to the List of non-trivial types.
Then you can handle SelectedItemChanged or something like that. You receive the whole object (in type object) which you can cast to your custom type and retrieve any properties you want

Class collection to listview items

What I'm trying to do is make so it selects the whole transaction when a listview item is selected so I don't have to rebuild it from each of it's string components.
I can do
List<Transaction> Transations = getTransations();
foreach(Transaction T in Transactions ){
string[] row = {T.DatabaseIndex.ToString(), T.TimeRan.ToShortTimeString(), T.MerchantID, T.OperatorID, T.TerminalID, T.AccountNumber, T.ExpDate, T.InvoiceNumber, T.PurchaseAmount, T.AuthorizeAmount, T.AcqRefData, T.RecordNo, T.CardType, T.AuthCode, T.CaptureStatus, T.RefNo, T.ResponseOrigin, T.DSIXReturnCode, T.CmdStatus, T.TextResponse, T.UserTraceData, T.Processor};
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
But that doesn't save me any work when I try to retrieve the data when the user picks it.
To be able to use the ListViewItem constructor with a string array for subitem data and actually view your subitems you need to set a details view and define list view columns beforehand.
Here is a running mockup.

Display multiple columns from Database in ListView

I'm not sure why this isn't working. I'm trying to display 2 columns from my database side by side in a listview box on my form. When I use this it doesn't display any of the data correctly.
("SELECT Person FROM tblPeople" + " SELECT Occur FROM tblpeople" , conn);
try
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
listView1.Items.Add(reader["People"].ToString());
listView1.Items.Add(reader["Occur"].ToString());
}
So i'm looking for my data to display like this:
John 3
James 4
Frank 1
As the names are coming from column People and the numbers are coming from column Occur.
To get the desired effect, you should set the view style to Details and add the second column value as sub-item.
Basically, you should do something like this:
listView1.View = View.Details;
listView1.Columns.Add("People");
listView1.Columns.Add("Occur");
while (reader.Read())
{
var item = new ListViewItem();
item.Text = reader["People"].ToString(); // 1st column text
item.SubItems.Add(reader["Occur"].ToString()); // 2nd column text
listView1.Items.Add(item);
}
Method Add() adds every time a new item to the collection. If your item consists of values from two objects, probably the easiest way would be to create a new item to encapsulate it, so you can easily bind it to your controls.
Consider something like this:
public class MyNewObject
{
public string People { get;set; }
public string Occur { get;set; }
public MyNewObject(string p, string o)
{
People = p;
Occur = o;
}
}
and then just
while (reader.Read())
{
listView1.Items.Add(new MyNewObject(reader["People"].ToString(),reader["Occur"].ToString()));
}

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources