DevExpress GridControl sorted list - c#

How to get a list of items, that is in sorted gridControl?
I need to create a new List with only values, sorted by GridControl.

probably not the most elegant solution but as found on the devexpress site because i hit the same problem a while a go:
the main gridview of the gridcontrol has a property DataRowCount, so you could do this;
List<DataRow> dataRows = new List<DataRow>();
for (int i = 0; i < gridView1.DataRowCount; i++) {
DataRow row = gridView1.GetDataRow(i);
dataRows.Add(row);
}
and then you can do whatever, or select a value from the row before you add it to an collection while using the column header:
object result = gridview1.GetDataRow(i)["ID"];

Related

Adding a column string to a list for each selected Row in a DataGridVIew

I want to add the 'OrderID' in the below DataGridView to a list for each row that I select.
I have managed to get it to cycle through a foreach loop but I am trying to figure out how to specify the selected row index, as it currently pulls the same indexed 'OrderID' and not the different ones selected.
See result I am currently getting:
See current code:
List<string> orders = new List<string>();
foreach (var row in grid.SelectedRows)
{
orders.Add(grid.Rows[grid.SelectedRows[0].Index].Cells[0].Value.ToString());
}
I know I shouldn't be using SelectedRows[0], but I cant think of how to index the specific 'row'
I used a for loop instead:
for (int i = 0; i < grid.SelectedRows.Count; i++)
{
orders.Add((grid.Rows[i].Cells["Order ID"].Value).ToString());
}

How to Add Columns from DataTable to ComboBox in C#

I have a ComboBox and a DataSet. I want to add each DataColumn to ComboBox as ComboBox Item.
I have tried this code:
DataColumn[] column_collection=new DataColumn[dataset.Tables[0].Columns.Count];
dataset.Tables[0].Columns.CopyTo(column_collection, 0);
combo_box.Items.AddRange(column_collection);
However, problem is that I just get a Empty List when I open ComboBox. That list has same number of items as there are columns, however there is no Value in it.
try something like this
var columns = dataset.Tables[0].Columns
.OfType<DataColumn>()
.Select(c => c.ColumnName);
combo_box.Items.AddRange(columns.ToArray());
Instead of:
combo_box.Items.AddRange(column_collection);
Write this:
for (int i = 0; i < column_collection.Length;i++)
{
combo_box.Items.Add(column_collection.GetValue(i).ToString());
}

Listbox Selected Item to array

I have a DataTable with 2 columns called "ID" and "Software" that I have used as a DataSource for a lst_Software multiselect listbox.
I'm trying to gather the ID for all the selected items in that have been selected and place that in an int[] array.
Listbox setup:
lst_Software.DataSource = software; //software is a DataTable
lst_Software.DisplayMember = "Software";
lst_Software.ValueMember = "ID";
I've tried below
List<int> list = new List<int>();
for (int i = 0; i < lst_Software.SelectedItems.Count; i++)
{
list.Add(Convert.ToInt32(lst_Software.SelectedValue.ToString()));
}
int[] software = list.ToArray();
I'm finding that I'm only getting the first selected value except it will not iterate through all... I know why though. I'm not using i to get passed through inside the for loop. I'm hoping someone can give me a direction to go to iterate through all the selected values.
Thank you
You're using lst_Software.SelectedValue.ToString() here in the loop so it only returns the one item. You have a for loop but you're not using the index variable. However, all of this is unnecessary really all you need is;
var items = lst_Software.SelectItems;
As that property is already the list of selected items. From there you can cast/convert them as you please.
SelectedValue is just the first selected value. You need to use SelectedItems.

Datagridview_2 combobox value is not valid in C#

I have a datagrid with 2 columns: 1 is normal textbox type and the other column is combobox type.
My user interface has another datagrid_1 which contains a list of names. When a user clicks on the row of datagrid_1 with names. It puts the value selected by the user in the row of datagrid_2 in 1st column and then expects user to select one of the values in the other column (combobox).
I am not sure how to assign a datasource to this combobox. I have tried the following code but I am getting error "Datagridview_2 combox value is not valid."
var source = new BindingSource();
var phase_7 = (phaseeqType.return_Distinct_Phase()
.Select(b => b).AsEnumerable()).ToList();
string[] P_combo = new string[phase_7.Count()];
for (int i = 0; i < phase_7.Count(); i++)
{
P_combo[i] = phase_7.ToString();
}
source.DataSource = phase_7;
dataGridView1.CurrentRow.Cells[1].Value = source;
Can anyone pls help?
Cells don't have a DataSource property, so you would have try casting it to something that does:
Example:
((DataGridViewComboBoxCell)dataGridView1.CurrentRow.Cells[1])
.DataSource = source;

Get a collection of all the DataRows coresponding to selected DataGridView rows

Is there a direct way to do this, or do I need to iterate over the DataGridView.SelectedRows, and then access DataGridViewRow.DataBoundItem for each row to create one?
The DataGridView does not provide a SelectedDataRows and SelectedRows in not Linq-enabled, so Yes, you will have to write a foreach loop.
A generic extension method to add "SelectedDataRows" to DataGridViews:
public static T[] SelectedDataRows<T>(this DataGridView dg) where T : DataRow
{
T[] rows = new T[dg.SelectedRows.Count];
for (int i = 0; i < dg.SelectedRows.Count; i++)
rows[i] = (T)((DataRowView)dg.SelectedRows[i].DataBoundItem).Row;
return rows;
}
This is generic so that you can return a typed data row using Typed Datasets. You could make yours just return a DataRow[] if you wanted. This also assumes that your DataGridView has a DataView bound to it.

Categories