finding listview column index using simple key string - c#

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;

Related

How to set the text alignment of MS table column according to data types?

I've tried to display the values of properties from a list of class on the table of MS Word like this:
public void CreateTable<T>(IList<T> list)
{
if (list != null)
{
var props = typeof(T).GetProperties();
Range tableLoc = WdDoc.Range();
var table = WdDoc.Tables.Add(tableLoc, list.Count, props.Length);
for (int i = 0; i < props.Length; i++)
{
table.Columns[i + 1].Select();
if (props[i].PropertyType == typeof(double))
{
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
}
else
{
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
}
//...
}
}
I tried to automatically set the text alignment of columns according to fill-in property type.
However, only the first row of each column can be set as the correct alignment I assign.
Other rows will always be set as the latest alignment of for loop.
Why did this happen? Did I miss any snippet?
Also, what are the differences between
WdApp.Selection.Paragraphs.Format.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
and
WdApp.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
Thanks!
The problem comes from using Selection.Range in this specific instance of working with the Word object model. When working with columns you need to use
WdApp.Selection.ParagraphFormat.Alignment
Background
Selection affects exactly what is selected - the entire column.
Selection.Range affects the Range of the selection. In the case of a Column, Selection.Range cannot be the entire column. A Range in Word must be a contiguous run of text, but a column is NOT contiguous. So Selection.Range returns only that part of the selection that is contiguous: the first cell (row). That explains why only the first row of the table is affected by your current code.
While a selected column appears to be contiguous this is just a convenience for the user. Under the covers, everything in the rows between the cells in one column and the next breaks up the text run.
If you're familiar with HTML think of how a table is defined in HTML. The way Word Open XML defines a table works on the same principle. The concept looks something like this:
<table><row><cell><cell></row><row><cell><cell><row></table>
The rows and their content are contiguous.

C# WPF get listview value from multiple selected row

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?

How to get multiple selected values from listbox C# Windows Application

I am using a Listbox on my windows application. The listbox has a multi-selection option, which users select all the roles the new user will have.
My Listbox is bound to a dataset. The table is Roles, with an ID column and Description column.
When I bound the listbox, I chose the data source being the dataset, the display member to be the description, the value member being the ID and the selected value being the dataset - roles.frolesid
Now when i loop through the listbox's selecteditems, i get only the first item's values etc...
I am trying to assign an int to the current selectedvalue, to get the ID to give to the user.
int num = 0;
foreach (var item2 in lstRoles.SelectedItems)
{
num = Convert.ToInt32(lstRoles.SelectedValue);
}
So if I select 3 roles, for instance Administrator, Capturer and Assessor, I only see the Administrator value.
How do I loop and access the next item? I'm not even using
item2
in my code. Could this be utilised?
For instance:
First iteration:
And it just stays on that selected item, never moving to the next item.
To get list of selected values when the data source is a DataTable, you can use this code:
var selectedValues = listBox1.SelectedItems.Cast<DataRowView>()
.Select(dr => (int)(dr[listBox1.ValueMember]))
.ToList();
But in general Item of a ListBox control may be DataRowView, Complex Objects, Anonymous types, primary types and other types. Underlying value of an item should be calculated base on ValueMember.
If you are looking for a good solution which works with different settings and different data sources, you may find this post helpful. It contains an extension method which returns value of an item. It works like GetItemText method of ListBox.
solved it
int num = 0;
int count = fRoleIDs.Count;
while (num != count)
{
var item = lstRoles.Items[num] as System.Data.DataRowView;
int id = Convert.ToInt32(item.Row.ItemArray[0]);
num += 1;
}
This goes into the listbox's items and according to the index, gets the item as a Row. This then gets me the itemarray, which is ID and Description {0,1}

How can I get value of a specific cell in DataTable in c#?

I am new to c# and using windows forms.
I have a dataTable as shown in screenshot, I want to get a value of specific cell in button_Text column based on Button_Name column value and save it in a string. I know I can use this code :
string s = DataTable.Rows[2][1];
But I do not want to use it because I do not want to use numbers between brackets to index the cell value but instead I want to use the a Button_Name column value and button_Text column name to index the cell value.
For example:
string s = DataTable.Rows[ButtonUC1_3][Button_Text]; // but it gives error : "Argument1: can not convert from string to int"
I hope it is clear enough. Anyone knows how to achieve this? Thank you
Use LINQ to DataSet there you can use column names
string value = dataTable.AsEnumerable()
.Where((row) => row.Field<string>("button_Name").Equals("ButtonUC_1"))
.Select((row) => row.Field<string>("button_Text"))
.FirstOrDefault();
Here Rows Must be Int formet,But Columns string allowed,
below one is error
string s = DataTable.Rows["ButtonUC1_3"]["Button_Text"];
the correct formet is,
string s = DataTable.Rows[2]["Button_Text"];
It will give the cell value,.
DataTable Rows don't have names, they're indexed only by number. If you want to access the rows through the names you specified, you need to use your own mapping, e.g. var rowMapping = new Dictionary<string,int>() { { ButtonUC1_3, 0}, ... } and then access the DataTable like this DataTable.Rows[rowMapping[ButtonUC1_3]][Button_Text].
Obviously, this is not the only way how to do it, you could define an enum, integer constants, etc. It all depends how many rows you have and how you use your DataTable.

combobox item selected To be fixed

I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;

Categories

Resources