C# WPF get listview value from multiple selected row - c#

im using WPF in c#.
How to get all the value of the row selected from column M.
Image of listview

From ListView.SelectedItems Property,
foreach (var item in myListView.SelectedItems)
{
//do something with the values, maybe add to a list?
DataRowView drv = (DataRowView)lstNames.SelectedItem;
String valueOfItem = drv["NameAndScore"].ToString();
}
Basically replace "NameAndScore" by your column name.
Reference: Why I get “System.Data.DataRowView” instead of real values in my Listbox?

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

Get combobox selected value in edit row in GridView?

I have create a code for GrideViewServer.FooterRow to get selected value of dropdownlist in footer row(insert). Now I want to do same thing for edit row to get selected value from combobox, however they are not in footer row.
Here what I write for footer row,
string architecture = ((DropDownList)GridViewServer
.FooterRow.FindControl("DropDownArchitecture")).Text;
Now I want to write for edit row, how can I write it? Something like this?
string architecture = ((AjaxControlToolkit.ComboBox)GridViewServer
.EditRow.FindControl("ComboBox1")).Text;
Can't find options function for edit section.
You don't have only one EditRow, you have a collection of rows of this type, to find a control inside, you need to iterate using for or foreach:
foreach (GridViewRow r in GridViewServer.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
string architecture = ((AjaxControlToolkit.ComboBox)r.FindControl("ComboBox1")).Text;
}
}
I found the answer. Here what I wrote,
((AjaxControlToolkit.ComboBox)GridViewServer.Rows[GridViewServer.EditIndex].FindControl("Combobox2")).Text;

finding listview column index using simple key string

I've assigned descriptive names to each of 4 columns in my ListView: Head, Mouth, Nose, Eyes. How can I access these columns using that text? I would love to find a way to do it as elegantly and concisely (with a #define) as is done in C:
if (Column[MOUTH] == 6)...
Not very elegant, but this should work (in this example I get the text from the first selected item in the listView. You can also use listView.Items)
string selectedItemMouthColumn = listView.SelectedItems[0].SubItems[listView.Columns.IndexOf(MOUTH)].Text;
Include a key for the column when you create it. E.g.:
// C# - first string is column key, second is column title
MyListView.Columns.Add("Name","File Name");
MyListView.Columns.Add("Size","File Size");
You can then use the key string to get the column index.
var ColIndex = MyListView.Columns.IndexOfKey("Size");
alternatively:
// Example: Get name for first selected listview item:
ListView lv = MyListView;
string FName = lv.SelectedItems[0].SubItems[lv.Columns.IndexOfKey("Name")].Text;

How to get CurrentCell location in DataGridView

HI
How to get CurrentCell location in DataGridView. i need for when my load will load every comboboxes set above on datagridview current cell. and get same size. how can i do that?
You can get the zero-based index of the column that contains the currently selected cell by using the ColumnIndex property of the CurrentCell property:
if (myDataGridView.CurrentCell != null)
{
int columnIndex = myDataGridView.CurrentCell.ColumnIndex;
}
Or, you can get the column object itself that contains the currently selected cell by using the OwningColumn property:
if (myDataGridView.CurrentCell != null)
{
DataGridViewColumn columnObject = myDataGridView.CurrentCell.OwningColumn;
}
Note that I'm still not entirely sure if this is what you're asking for. My forms don't have columns, so I'm assuming that you mean the DataGridView. And your answer still doesn't really explain what exactly you mean by "location," but this should tell you everything you need to know about the column that contains the current cell.

Categories

Resources