Wpf ComboBoxColumn selected items to string list - c#

question : how i can get a List<string> of all selected items in ComboBoxColumnso i can change the old values on my DB
im new into wpf and overflow so forgive me if i made any mistake

Assuming that 0 is the index of the ComboBoxColumn.
int j = DataGD.Items.Count;
int i = 0;
do
{
DataGridRow row = this.DataGD.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
ComboBox ele = this.DataGD.Columns[0].GetCellContent(row) as ComboBox;
string newS = ele.Text;
list.add(newS);
i++;
} while (i < j);

Related

How can I get row in datagrid WPF?

I would like to get all rows in order to check some rows that are the same in the database , I'm try using many solutions in StackOverlow but any of them work for me :
Get row in datagrid
WPF Datagrid set selected row
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(i);
}
This code return value cannot be null Exception.
I have tried also this line of code and I'm got the same Exception ( the object currentitem was null ).
var currentItem = myDataGrid.SelectedItem as MyObject;
Complete Code:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
grid.ItemsSource = null;
grid.ItemsSource = work;
grid.Items.Refresh();
for (int i = 0; i < grid.Items.Count; i++) // My grid items count equals to 7
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(i); // this value was null
var currentItem = grid.SelectedItem as object; // this value was null
}
}
You could bind the item source of your DataGrid to an ObservableCollection and run a foreach loop over it.
I think that makes things a lot easier.
Finally I got it , I fixed the error by using this code :
for (int i = 0; i < grid.Items.Count; i++)
{
GridModel row = (GridModel)grid.Items[i];
}
// Note : The grid model is a class for the Datagrid ( properties are the same as the columns name of the datagrid

Set Combobox selected item based on its ItemsValue

I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.
Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.
So far i have wrote code upto this point. But it always giving me First Item of a Combobx.
DataGrid gd = (DataGrid)sender;
DataRowView rowSelected = gd.SelectedItem as DataRowView;
if(rowSelected!=null)
{
for (int i = 0; i < comboBox1.Items.Count;i++ )
{
if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
}
comboBox1.SelectedItem= comboBox1.Items[index];
}
textBox9.Text=rowSelected[14].ToString();
}
Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.
for (int i = 0;i <comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = i;
}
}
comboBox1.SelectedIndex = index;
Change your code to
if(rowSelected!=null)
{
int index = comboBox1.Items.IndexOf(rowSelected[14]);
comboBox1.SelectedItem = comboBox1.Items[index];
}
or
Use FindStringExact() method of combobox
int i = comboBox1.FindStringExact("Combo");
if(i >= 0)
{
}

How to make the WPF Datagrid editable while creating dynamically, without giving any Itemsource

I am new to WPF, I am creating a datagrid dynamically by taking the NoOfRows & NoOfColumns as input. But the datagrid is not becoming Editable as I am not passing any Item source.
Is There any way of making the datagrid editable while not giving any Item source?
My Code is Like this (I have taken checkbox columns as datagrid column, I want to check the checkboxes):
var row = Convert.ToInt32(TbxLevels.Text);
var column = Convert.ToInt32(TbxPositions.Text);
Dgrid.Columns.Clear();
Dgrid.Items.Clear();
for (int i = 0; i < column; i++)
{
var c = new DataGridCheckBoxColumn();
c.Header = column;
Dgrid.Columns.Add(c);
}
for (int i = 0; i < row; i++)
{
Dgrid.Items.Add(i);
}

Show values from datagridView in combobox

I have for example this values in datagridView : 1;2;3;4;
I want display this values in combobox like this : 1
2
3
4
My code show only last value in combobox: 4
My code of showing :
string cmbValue = CmbText;
string[] cmb = cmbValue.Split(new[] { ';' },StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cmb.Length; i++)
{
comboBox1.Text = cmb[i];
}
and here is my code of creating array of combobox and calling method to change cell in dataGrid:
string cmbText = comboBox1.Text;
string[] cmb = new string[] { cmbText};
frm1.ChangeCellCmb(2, cmb);
this.Dispose();
Someone know how to do this ? I cant simply set collection of values combobox, because values of combobox are reading from datagrid and it's reading from DB.
Many thanks.
You need to loop and add the required items as follows:
for (int i = 0; i < cmb.Length; i++)
comboBox1.Items.Add(cmb[i]);
This will add all the required items to the drop down menu. To select/display '4' the 3rd entry in cmb by default do
comboBox1.SelectedIndex = 3;
or
comboBox1.SelectedItem = "4";
I hope this helps.

Datagridivew DataGridViewComboBoxColumn select value member

i have datagridview which has one combobox column. i populate combobox column. when i select any text from combobox column then i need to get the value when i read the data in for loop.
dgFilter is here datagridview
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = loadOperators();
dgcoSpec.DisplayMember = "Operatortxt";
dgcoSpec.ValueMember = "Operatorvalue";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Operators";
dgcoSpec.Name = "Operators";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 130;
dgFilter.Columns.Insert(1, dgcoSpec);
here this way i read data from combobox column
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
strOperator = dgFilter.Rows[i].Cells[1].Value.ToString().Trim();
}
but the problem is i am not getting the combox value member rather i am getting display member.
so how to extract value member from for loop. please guide me with code. thanks
The value member is the string that is displayed inside the DataGridViewComboboxCell.
The actual Combobox control only exists during the time frame in which the user is editing the cell.
If you mean that you would like to get the index of the value in the list of the DataGridViewComboboxCell items, you can query the index of the value:
for (int i = 0; i <= dgFilter.Rows.Count - 1; i++)
{
var cell = dgFilter.Rows[i].Cells[1] as DataGridViewComboboxCell;
int index = cell == null || cell.Value == null ? -1 : cell.Items.IndexOf(cell.Value);
Console.WriteLine(index);
}
In this example, I am using -1 as an invalid value.
EDIT Just noticed you are using a DataSource;
See DataGridViewComboBoxColumn name/value how? for possible duplicate.

Categories

Resources