select item combobox from textblock wpf - c#

i want selected item from combo box when textbox not equal " "
i use this code
if (txt_e1.Text != " ")
{
combobox1.SelectedIndex = combobox1.Items.IndexOf(txt_e1.Text);
}
but dont show me anything
i get txt_e1 from another page and always equal with one of item in combobox
window loded code
combobox1.ItemsSource = database.Mahs.ToList();
combobox1.DisplayMemberPath = "MahName";
combobox1.SelectedValuePath = "MahID";
i can not use
combobox1.text = txt_e1.Text;
because in combobox1 text change i use this code
if (datagrid_customer == null || combobox1 == null)
{ txt_f1.Text = "Not Matching"; }
else
{
for (int i = 0; i < datagrid_customer.Items.Count; i++)
{
if (Convert.ToString((datagrid_customer.SelectedCells[1].Column.GetCellContent(datagrid_customer.Items[i]) as TextBlock).Text) == combobox1.Text)
{
...
}
and i get error return null .
what should i do ?

Related

Store values from selected checkBox to a array

I have a foreach that if the checkbox of the row is selected it should do some calculations and after that store the result values in something,i'm trying to store in a array.In the end i need to have, for example 4 rows that were selected with his values inside the array,after that i need to put this values inside a session,and recover in other page(A confirmation page).I'm trying this.But i cant figure out a way to store the select rows in a array.I dont necessary need to be array if someone knows a better way.
string[] valoresSelecionados;
foreach (GridViewRow grdCount in grdSimulacao.Rows)
{
if (((CheckBox)grdCount.FindControl("chkSeleciona")).Checked)
{
if (double.Parse(grdCount.Cells[5].Text) >= 0)
vlNotas += double.Parse(grdCount.Cells[5].Text);
else
vlDebito += double.Parse(grdCount.Cells[5].Text);
if (!string.IsNullOrEmpty(grdCount.Cells[8].Text) && grdCount.Cells[8].Text != " ")
vlEncargos += double.Parse(grdCount.Cells[8].Text);
_simulacao.Notas.Rows.Add(grdCount.Cells[1].Text);
if (Culture== "English (United States)")
{
//DateTime data =DateTime.Parse(grdCount.Cells[2].Text);
//var dataInicial = data.ToString("mm-DD-yyyy");
_simulacao.Notas.Rows[u]["DTEMISSAO"] = grdCount.Cells[2].Text;
_simulacao.Notas.Rows[u]["DTVENCIMENTO"] = grdCount.Cells[3].Text;
}
else
{
_simulacao.Notas.Rows[u]["DTEMISSAO"] = grdCount.Cells[2].Text;
_simulacao.Notas.Rows[u]["DTVENCIMENTO"] = grdCount.Cells[3].Text;
}
_simulacao.Notas.Rows[u]["PRAZOPGTO"] = "5";
_simulacao.Notas.Rows[u]["VALTOTAL"] = grdCount.Cells[5].Text;
_simulacao.Notas.Rows[u]["DOCCOMPRAS"] = grdCount.Cells[6].Text;
if (DateTime.Parse(grdCount.Cells[3].Text) < DateTime.Parse(txtDtCredito.Text))
dtok = true;
DateTime antigaData = DateTime.Parse(grdCount.Cells[3].Text);
DateTime novaData = DateTime.Parse(txtDtCredito.Text);
DateTime dataAtual = DateTime.Today;
int diaAtual = dataAtual.DayOfYear;
int diaNovo = novaData.DayOfYear;
int diaAntigo = antigaData.DayOfYear;
int anoAtual = dataAtual.Year;
int anoNovo = novaData.Year;
if (diaAntigo <= diaNovo)
{
dataErro++;
}
if (diaNovo == diaAtual || diaNovo == diaAtual + 1 || diaNovo == diaAtual + 2 ||
diaNovo == diaAtual + 3 || diaNovo < diaAtual || anoAtual < anoNovo)
{
error++;
}
else
{
TimeSpan ts = antigaData - novaData;
if ((double.Parse(grdCount.Cells[5].Text)) < 0)
{
}
else
{
_simulacao.Notas.Rows[u]["DIASANTEC"] = ts.Days;
}
}
if (grdCount.Cells[7].Text != " ")
_simulacao.Notas.Rows[u]["DIASANTEC"] = "10";
else
_simulacao.Notas.Rows[u]["DIASANTEC"] = "0";
if (!string.IsNullOrEmpty(grdCount.Cells[8].Text) && grdCount.Cells[8].Text != " ")
{
_simulacao.Notas.Rows[u]["VLFINAL"] = double.Parse(grdCount.Cells[5].Text) - double.Parse(grdCount.Cells[8].Text);
_simulacao.Notas.Rows[u]["VLENCARGOS"] = grdCount.Cells[8].Text;
}
else
{
_simulacao.Notas.Rows[u]["VLFINAL"] = double.Parse(grdCount.Cells[5].Text);
_simulacao.Notas.Rows[u]["VLENCARGOS"] = "0,00";
}
valoresSelecionados[] = _simulacao.Notas.Rows.Add[u];
u++;
}
}
The first Picture show the first grid with all values.I selected 4 for example,after the calculations of the method i need to show only the 4 select in a model (second Picture),under the first grid of Picture two i will put the new grid with the selected values from before.

Combobox c# SelectedItem not work

I am a junior on C#. I have problem with Combobox. When I using SelectedItem is no working exactly, what I want. SelectedItem always return the last item in combobox, but when I using SelectedIndex is return correct result.
Example in combobox is 3 items: Admin, Moderation, Salesman. Is always return Salesman, when I using SelectedItem, even when I select Admin.
There is my code:
class ComboboxItem
class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Value.ToString();
}
}
add items into combobox
private void AddItemIntoComboBoxGroup()
{
string query = "SELECT* FROM kites_mango.staff_group; ";
DBUtility data = new DBUtility();
List<string>[] list = data.Select(query);
if (list != null && list[0].Count() > 0)
{
ComboboxItem item = new ComboboxItem();
for (int i = 0; i < list[0].Count(); i++)
{
item.Value = list[0][i];
item.Text = (list[1][i] + " - " + list[0][i]);
CbBGroup.Items.Add(item);
}
}
}
get selected item
if (CbBGroup.SelectedItem != null)
{
MessageBox.Show(CbBGroup.SelectedIndex + "/" + CbBGroup.SelectedItem.ToString());
}
You're instancing the ComboboxItem object before the for loop. This way, for each loop in your for statement, you're changing the properties of the same object, making objects that were loaded before in the list are also modified. That is, your list could have 3 itens, but they are going to have the same value (Salesman) because all the itens in your list have the same object reference.
Just change from this:
ComboboxItem item = new ComboboxItem();
for (int i = 0; i < list[0].Count(); i++)
{
item.Value = list[0][i];
item.Text = (list[1][i] + " - " + list[0][i]);
CbBGroup.Items.Add(item);
}
To this:
for (int i = 0; i < list[0].Count(); i++)
{
ComboboxItem item = new ComboboxItem();
item.Value = list[0][i];
item.Text = (list[1][i] + " - " + list[0][i]);
CbBGroup.Items.Add(item);
}

Grid View check the value is exist?

I have a drop down list "ddlMitchelLandscape2" ,when add button triggers ,i add the selected item value in to gridview.
I am stuck here ,how to check the gridview, before add the value to grid view. The selected item is already exist in grid view or not when the add button is triggered .
Some one help me how to check the value is exist in gridview before add it to gird view please ?
protected void btnAddMitchellLandscape_Click(object sender, EventArgs e)
{
//validate to make sure Mitchell Landscape is entered
if (!ValidateMitchellPage())
return;
Assessment objAssessment = (Assessment)Session[Session_CurrentAssessment];
if (ddlMitchelLandscape2.GetSelectedItemValue > 0)
{
if (lblMitchellID.Text == string.Empty)
{
//add
AssessmentEntity objAssessmentEntity = new AssessmentEntity();
Assessment.tblMitchellLandscapeIDRow row =
objAssessment.tblMitchellLandscapeID.NewtblMitchellLandscapeIDRow();
row.MitchellLandscapeID = ddlMitchelLandscape2.GetSelectedItemValue;
row.MitchellLandscapeName = ddlMitchelLandscape2.GetSelectedItemText;
}
else
{
//Add button not visible when its not a new row
ctrlHeader.ShowError("Error: Unknown error");
return;
}
//refresh data bound table
PopulateMitchellDetailsToForm(ref objAssessment);
//clear after save
btnClearMitchellLandscape_Click(null, null);
}
}
ValidateMitchellPage()
private bool ValidateMitchellPage()
{
litMitchellError.Text = string.Empty;
if (ddlMitchelLandscape2.GetSelectedItemValue <= 0)
litMitchellError.Text = "Please select Mitchell Landscape";
if (litMitchellError.Text.Trim() == string.Empty)
{
litMitchellError.Visible = false;
return true;
}
litMitchellError.Visible = true;
return false;
}
DataBind to grid view
private void PopulateMitchellDetailsToForm(ref Assessment objAssessment)
{
Assessment.tblMitchellLandscapeIDRow[] MlData
= (Assessment.tblMitchellLandscapeIDRow[])objAssessment.tblMitchellLandscapeID.Select("SaveType <> " + Convert.ToString((int)EnumCollection.SaveType.RemoveOnly));
this.gvMitchellLandscape.DataSource = MlData;
this.gvMitchellLandscape.DataBind();
}
You have to check the selected value from the dropdown or combobox in gridview by checking each row.
You can use following code to get row of gridview.
bool isValueExist=False;
for (int i = 0; i < gridview.Rows.Count; i++)
{
String val = gridview.Rows[i].Cells[0].Value.ToString();
if(val == your_drop_down_value)
{
isValueExist=True;
break;
}
}
You have to change the cell number according to your gridview design.

How do I change the text of a ComboBox item?

I have a combobox filed with the name of dataGridView columns, can I change the text of displayed items in the comboBox to any text I want ?
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
if (dataGridView1.Columns[i].ValueType == typeof(string) &&
i != 6 &&
i != 7 &&
i != 8 &&
i != 9)
comboBox1.Items.Add(dataGridView1.Columns[i].Name);
}
comboBox1.SelectedIndex = 0;
I hope this helps you:
combobox.Items[combobox.FindStringExact("string value")] = "new string value";
The FindStringExact method returns the index of an specific item text, so this can be a better way to change the text of a Combobox item.
Note: This works fine on C#.
If the value you want to use is not suitable as the text in a combobox, I usually do something like this:
public class ComboBoxItem<T> {
public string FriendlyName { get; set; }
public T Value { get; set; }
public ComboBoxItem(string friendlyName, T value) {
FriendlyName = friendlyName;
Value = value;
}
public override string ToString() {
return FriendlyName;
}
};
// ...
List<ComboBoxItem<int>> comboBoxItems = new List<ComboBoxItem<int>>();
for (int i = 0; i < 10; i++) {
comboBoxItems.Add(new ComboBoxItem<int>("Item " + i.ToString(), i));
}
_comboBox.DisplayMember = "FriendlyName";
_comboBox.ValueMember = "Value";
_comboBox.DataSource = comboBoxItems;
_comboBox.SelectionChangeCommitted += (object sender, EventArgs e) => {
Console.WriteLine("Selected Text:" + _comboBox.SelectedText);
Console.WriteLine("Selected Value:" + _comboBox.SelectedValue.ToString());
};
Try this:
ListItem item = comboBox1.Items.FindByText("<Text>");
if (item != null)
{
item.Text = "<New Text>";
}
May be instead of changing the text it would be simple to remove the item from a particular index and insert new item at same index with new text
You can simply change combo box items text by :
my_combo.Items [i_index] = "some string";
The best way you can do that is in this way:
ui->myComboBox->setItemText(index,"your text");

Problem with the DevExpress XtraGridView control

I have one xtragrid control on my devxpress form . I've created the columns of my grid at runtime when i load the form . I'm developing the Field chooser for my grid view which is situated on the same form. For that i used the repositoryItemCheckedComboBoxEditcontrol & in that control i added the column names which will be present in the xtragrid.
Basically i created the columns to the xtragrid with the Visible property to false. When user checks particular column name by using repositoryItemCheckedComboBoxEdit then i set the Visible to true & again if user unchecked the column name then again i set the visible to false. & while creating column i set the width of the column.
Problem which i'm facing is that if user select the all fields from the repositoryItemCheckedComboBoxEdit then the grid control should show the horizontal scroll bar automatically when require.
And another problem is that with the columns is besides setting the width of the column, it is not showing the required width of that column . it shrinks that all column width .
code which i use for creating column to the xtragridview at run time is as follows -
public void AddGridColumn(string fieldName, string caption, int nRowWidth, RepositoryItem Item, object oCollection, string DisplayMember, string ValueMember, string format, FormatType type)
{
DevExpress.XtraGrid.Columns.GridColumn column = ColumnView.Columns.AddField(fieldName);
column.Caption = caption;
column.ColumnEdit = Item;
column.DisplayFormat.FormatType = type;
column.DisplayFormat.FormatString = format;
column.VisibleIndex = ColumnView.VisibleColumns.Count;
column.Width = nRowWidth;
}
code for the field chooser is as follows -
I used this function for filling the items of the repositoryItemCheckedComboBoxEdit control
private void FieldCollection()
{
allFields = new ArrayList();
columnNames = new Dictionary<string, string>();
allFields.Clear();
repositoryItemCheckedComboBoxEdit1.Items.Clear();
for (int i = 0; i < gvBase.Columns.Count; i++)
{
allFields.Add(gvBase.Columns[i].Caption );
if (gvBase.Columns[i].FieldName != "ContactID")
{
if (gvBase.Columns[i].Visible == true)
{
if (gvBase.Columns[i].Caption != "Label1" && gvBase.Columns[i].Caption != "Label2" && gvBase.Columns[i].Caption != "Label3" && gvBase.Columns[i].Caption != "Label4" && gvBase.Columns[i].Caption != "Label5")
repositoryItemCheckedComboBoxEdit1.Items.Add(gvBase.Columns[i].Caption, CheckState.Checked);
if (!columnNames.ContainsKey(gvBase.Columns[i].Caption))
columnNames.Add(gvBase.Columns[i].Caption, gvBase.Columns[i].FieldName);
}
else
{
if (gvBase.Columns[i].Caption != "Label1" && gvBase.Columns[i].Caption != "Label2" && gvBase.Columns[i].Caption != "Label3" && gvBase.Columns[i].Caption != "Label4" && gvBase.Columns[i].Caption != "Label5")
repositoryItemCheckedComboBoxEdit1.Items.Add(gvBase.Columns[i].Caption, CheckState.Unchecked);
if (!columnNames.ContainsKey(gvBase.Columns[i].FieldName))
columnNames.Add(gvBase.Columns[i].Caption, gvBase.Columns[i].FieldName);
}
}
}
cmbFieldChooser.EditValue = "";
}
this is used for the repositoryItemCheckedComboBoxEdit control event -
private void cmbFieldChooser_EditValueChanged(object sender, EventArgs e)
{
ArrayList temp = new ArrayList();
temp.AddRange(allFields);
string[] strFields = cmbFieldChooser.EditValue.ToString().Split(',');
for (int i = 0; i < strFields.Length; i++)
{
if (temp.Contains(strFields[i].Trim()))
temp.Remove(strFields[i].Trim());
if (strFields[i] != "")
{
if (columnNames.ContainsKey(strFields[i].Trim()))
{
if (gvBase.Columns[columnNames[strFields[i].Trim()]].Visible == false)
{
gvBase.Columns[columnNames[strFields[i].Trim()]].Visible = true;
gvBase.Columns[columnNames[strFields[i].Trim()]].BestFit();
}
}
}
}
if (temp.Count < 20)
{
for (int j = 0; j < temp.Count; j++)
{
if (columnNames.ContainsKey(temp[j].ToString().Trim()))
{
gvBase.Columns[columnNames[temp[j].ToString().Trim()]].Visible = false;
}
}
}
cmbFieldChooser.EditValue = repositoryItemCheckedComboBoxEdit1.GetCheckedItems();
if ((cmbFieldChooser.EditValue.ToString()).Split(',').Length > 5)
{
gvBase.OptionsView.ColumnAutoWidth = false;
gvBase.BestFitColumns();
gvBase.HorzScrollVisibility = ScrollVisibility.Always;
}
else
{
gvBase.OptionsView.ColumnAutoWidth = true;
gvBase.HorzScrollVisibility = ScrollVisibility.Never;
}
}
How to resolve this problem?
thanks.
How many columns do you have in your Grid?
I see you have code there to turn off the ColumnAutoWidth once you go past 5 columns (ie 6 columns or more). Have you debugged this condition to ensure the ColumnAutoWidth is indeed being turned off?
As per BestFitColumns Help Doc the BestFitColumns will only calculate for the first n rows as per the BestFitMaxRowCount property unless it it set to -1, could this be a cause?
The other thing that seems a little odd if that you are setting the EditValue of cmdFieldChooser within the cmdFieldChooser_EditValueChanged event... why so?

Categories