get value based on index dropdown - c#

I need to select a value from dropdown based on index. It is a super easy question. cannot find the property:
I though doing something like:
dll.Items[index]
But still do not know how to get value for this index.

You can use dll.Items.FindByIndex(index); or dll.Items.FindByValue(val); according to your needs.

This loops through all items of an ASP combobox:
DataTable dt = (DataTable)comboBox1.DataSource;
for(int i = 0; i < dt.Rows.Count; ++i)
{
string displayText = dt.Rows[i][comboBox1.DisplayMember].ToString();
string valueItem = dt.Rows[i][comboBox1.ValueMember].ToString();
}

can be done like below
dll.Items[index].value
btw, FindByIndex, is like not available

Related

How to Remove an dropdown Item by finding Text

I am databinding my dropdown in the code behind as such
AttributeStatusDdl.Items.Clear();
AttributeStatusDdl.DataSource = StatusDs;
AttributeStatusDdl.DataTextField = "AttributeStatus";
AttributeStatusDdl.DataValueField = "AttributeStatus";
AttributeStatusDdl.DataBind();
Now I would like find any items which is a string 'Test' to be removed... How can i achieve this task..
I have tried using findByText but somehow not able to remove the items with text Test... Thank you in advance
myDropDown.Items.Remove(myDropDown.Items.FindByValue("Test"));
Your code should work, try this one
for(int i= AttributeStatusDdl.Items.Count -1; i>= 0; i--)
{
if (AttributeStatusDdl.Items[i].Text == "a")
AttributeStatusDdl.Items.RemoveAt(i);
}

Changing an FSharpList in C#

FSharpList<FSharpList<int>> newImageList;
FSharpList<int> row;
for(int i = 0; i < CurrentImage.Header.Height)
{
row = PPMImageLibrary.GrayscaleImage(CurrentImage.ImageListData);
newImageList.Head = row;
}
Above I'm trying to take a int list list and set each index to row which is a int list. Obviously I can't do it with .Head, and if I could it would only change the first index. I'm wondering how I could possibly make this work, I'm having a hard time getting any index of newImageList in the first place.
FSharpList is an immutable list. Therefore you cannot assign its Head and Tail properties something. However, you can try adding your FSharp list into a generic C# List or any collection that inherits an IEnumerable. For example from your code:
List<int> newImageList = new List<int>();
for(int i = 0; i < CurrentImage.Header.Height)
{
newImageList.AddRange(PPMImageLibrary.GrayscaleImage(CurrentImage.ImageListData)); // I am assuming your GrayscaleImage method might return multiple records.
}
I hope this helps.

ComboBox.SelectedValue is not Working

I have a WinForms application. I've populated my ComboBox with the following code:
cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;
However, the last line has absolutely no effect. The ComboBox appears with no items selected.
So I was doing some debugging and noticed some odd things. The following image is from the watch window after setting cboGridSize.SelectedIndex to 0.
Watch Window http://www.softcircuits.com/Client/debugwin.jpg
Even though the SelectedItem property contains exactly what I would expect, SelectedValue is still null. Although the documentation for SelectedValue is pathetic, I understood it would contain the value of the selected item (SelectedItem). Instead, the two properties seem completely unrelated. Can anyone see what I have wrong?
As you can see, I have the ValueMember property set. And the DropDownStyle property is set to DropDownList.
EDIT:
Once Nikolay Khil set me straight on the issue here (why the docs for SelectedValue don't do that escapes me), I decided to simply write my own code to accomplish the same task. I'm posting it here in case anyone is interested.
static class ComboBoxHelper
{
public static void LookupAndSetValue(this ComboBox combobox, object value)
{
if (combobox.Items.Count > 0)
{
for (int i = 0; i < combobox.Items.Count; i++)
{
object item = combobox.Items[i];
object thisValue = item.GetType().GetProperty(combobox.ValueMember).GetValue(item);
if (thisValue != null && thisValue.Equals(value))
{
combobox.SelectedIndex = i;
return;
}
}
// Select first item if requested item was not found
combobox.SelectedIndex = 0;
}
}
}
This is implemented as an extension method so I simply change my original code as follows:
cboGridSize.Items.Clear();
for (int i = 2; i <= 12; i++)
cboGridSize.Items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.LookupAndSetValue(4);
Both ValueMember and DisplayMember properties are used only if DataSource property is defined.
So, you should re-write your code as follows:
private readonly BindingList<KeyValuePair<string, int>> m_items =
new BindingList<KeyValuePair<string, int>>();
public YourForm()
{
InitializeComponent();
cboGridSize.DisplayMember = "Key";
cboGridSize.ValueMember = "Value";
cboGridSize.DataSource = m_items;
for (int i = 2; i <= 12; i++)
m_items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;
}
Links:
BindingList class
ObservableCollection class
INotifyCollectionChanged Interface
This does not answer the OP however...the ComboBox SelectedValue must be an integer type.
If you have a short or byte var that holds the value that will set the SelectedValue, it won't work - you will have null/nothing value.
Use an integer.
I know this is an old question but I've just come across this problem myself. I solved with the following - it's slightly hacky but it works:
if(newVal != null)
{
MyComboBox.SelectedValue = newVal;
}
else
{
MyComboBox.SelectedIndex = 0; // the 'None Selected' item
}
Hope this helps someone.
u can set SelectedValue first, and then set Datasource and others

devexpress gridView.Rows?

I want to do it with Devexpress extension (gridview) :
string dataInCell = dataGridView1.Rows[i].Cells[j].Value.ToString();
Like :
gridView1.Rows[i].Cells[j]
If you are trying to get the value of a cell in a specefic row, here is how :
a. If you want the value of a cell of the focused row :
view.GetFocusedRowCellValue("fieldName");
b. If you want the cell value of a row knowing his handle :
view.GetRowCellValue(rowHandle, "fieldName");
Good luck
try this
for (int i = 0; i < gridView.RowCount; i++)
{
dataInCell = Convert.ToString(gridView.GetRowCellValue(i, "FieldName"));
}
To get a spescific row you can use these commands.
GridView.GetDataRow(rowHandle)
or
GridView.GetRow(rowHandle)
but if you want to modify a range of cells, it is usually better to go directly at the datasource
You presumably have set a datasource on the grid? If so, use the datasource and access it via its datasource index.
Using row handles could causes issues when the grid is sorted. I recommend...
int dataIndex = gridView.GetDataSourceRowIndex(rowHandle);
var myData = myDataSource[dataIndex];
Provided you're using a generic collection there is no casting involved and this handles grouping and sorting. Of course what is displayed and what is the data may not be the same thing. E.g. If the data is an enumeration. You would display the displayname for this but the value in the datasource is the enum. Normally I need the underlying value instead of the displayed text.
you can use below code:
dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name","column2-name",...);
with this you can get value with row index that focused on it and select with field`s name,return value type of object and you cast to int,string and ...
such :
string id=(string)dataGridView1.GetRowValues(dataGridView1.FocusedRowIndex,"column1-name");
but it depends to type column1-name
string dataInCell = ((DataRowView)gridControl1.MainView.GetRow(i)).Row.ItemArray[j].ToString();
I think you are looking for this:
string str = gridView1.GetRowCellValue(Convert.ToInt32("ROW_NUMBER"), "COLUMN_NAME").ToString();
you should use GetRowCellValue
string cellValue;
cellValue = gridView1.GetRowCellValue(2, "ID").ToString();
All above step you can, but keep in mind that null values conversation will be thrown an error, so before access it do Convert.IsDBNull().
I think it's the best code for get row column field.
string name= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "name").ToString(),
You can get the value of grid cell using
string cellValue = gvGrid.GetRowValues(visibleIndex, "FieldName").ToString();
where visibleIndex is the row's index. You can just loop like this
if (gvGrid.VisibleRowCount > 0)
{
for (int index = 0; index < gvGrid.VisibleRowCount; index++)
{
string cellValue = gvGrid.GetRowValues(index, "FieldName").ToString();
}
}

Finding an item in asp.ListView with Value when paging is enabled

I'm trying to find the selected Item in an aspx ListView that is on a separate page, then switch the page and select the item. I have the value property of the ListViewItem I am looking for, but cannot seem to get it to work. Here is what I tried:
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectItem(i);
break;
}
}
So lvProject is my list view. The project Variable is an Int64 which represents the UID of my Project. This is also the Value of my ListViewItems. The problem with the code above is that when paging is enabled, and the item is on a different page this will not work because the listView.Items.Count is set to the # of Items on the current page only.
My goal is to find the item, set the listview to display the correct page, and finally select the item. You would figure that I could just set the SelectedValue property, but this is not that simple as it is read only. Any ideas would help greatly, thanks in advance.
--Roman
In order to get the total record count from the object data source, you should use the Selected event as follows:
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
// Get total count from the ObjectDataSource
DataTable dt = e.ReturnValue as DataTable;
if (dt != null) recordCount = dt.Rows.Count; // recordCount being declared outside the method
}
You would then be able to search for the item as follows:
for (int i = 0; i < recordCount; i++)
{
Label lblItem = (Label)lvProject.Items[i].FindControl("IdLabel");
if (lblItem.Text.Equals(itemToSearch))
{
lvProject.SelectedIndex = i;
break;
}
}
Hope it helps!
How do you bind ListView Items?
If you are using database level paging (stored procedure, query) - you should do search in the same way - using database query/stored procedure by passing a search criteria.
If you are bind ListView Items to a collection of items which are provided by a business/data layer - you have to create search method on layer which provides items so this method will be able to loop through the items.
You should set the SelectedIndex property to i
for (int i = 0; i < lvProject.Items.Count; i++)
{
if (((Label)lvProject.Items[i].FindControl("Project_IDLabel")).Text == project.ToString())
{
lvProject.SelectedIndex = i;
break;
}
}

Categories

Resources