Assign some data fields to DropDownList item - c#

I want to assign 2 data fields to a DropDownList item because I want to display 2 values at click at an item in different elements (for example, 2 textboxes).
For example: at click on a DDL item, that a value of data field named "example" displayed in one TXTBOX and other of a data field named "definition" displayed in other TXTBOX.

If you have do something like this, you can just separate the two values with some sort of delimiter (, or | or whatever), and then parse them out when it's selected and you have to display them.
Item.Value = "VALUE1,VALUE2";
string[] Values = Item.Value.Split(',');
txt1.Text = Values[0];
txt2.Text = Values[1];

Related

how insert value from ListString into Combobox selected Items and Textbox

I have one list
1,XX000001,2,XX000002,3,XX000003,4,XX000004
I want to split and insert data using combobox selected items.
Like when I select 1 from combobox, textbox.Text show DD000001 . If I select 2, textbox.Text show DD000002
Like now I split data like below, but I don't know how use combobox selected item to extract data into textbox.
if (lsdata[i].StartsWith("XX"))
{
cbWorkOrder2.Items.Add(lsdata[i - 1]);
//listBox1.Items.Add(lsdata[i]); //
}

C3 comboBox different display and value members but need to access both

I have a dataset that has multiple columns which include a Text value to display and a numeric value that I need to use for filtering an another combobox.
MyComboBox.DisplayMember = "Reason";
MyComboBox.ValueMember = "ReasonID";
MyComboBox.DataSource = MyDataTable;
The issue I have is that part of the code I need the ID however for another part of the code I need the text. I can get the ID back but I'm not sure how to access the text when the value changes. I've tried the following
String test1 = MyComboBox.SelectedValue.ToString();
String test2 = MyComboBox.SelectedText.ToString();
Test1 is the ID as I expected. However test2 is "" and I can't see any properties that give the display value instead of the selected value.
Use ComboBox.Text Property
string value = MyComboBox.Text;
Text property contains value of DisplayMember of selected item in your case.
About ComboBox.SelectedText from MSDN
Gets or sets the text that is selected in the editable portion of a
ComboBox.
So this is not a text of selected item

DataSource, bind more then 1 value

I want to bind more then 1 columns to drop down list, so that I can get the column values when user clicks a button,
ddlListMine.DataSource = GetSomeChickens();
ddListMine.DataTextField = "ChickenName";
ddListMine.DataValueField= "NumberOfEggsChickenLay";
ddListMine.Items.Insert(0, new ListItem("Please Please Please Select....", "0"));
ddListMine.DataBind();
I have another column "ChickenType", which I want to access in Selected Index change column.
GetSomeChickens(); returns 6 columns, including ChickenName, NumberOfEggsChickenLay, ChickenType and so on...
Edit
Off course, I can call database again in selected index change method, but there must be a way around i think
The DropDownList doesn't hold the entire object during the binding, only the Text and Value as defined by DataTextField and DataValueField.
In order to get a selected object back, you can have a method to get the ChickenType by passing the ChickenName using Linq like this.
List<Chicken> Chickens = GetSomeChickens();
Var Chicken= Chickens.FirstOrDefault(c => c.ChickenName== ddlListMine.SelectedItem.Text);
if(Chicken!= null)
{
string ChickenType = Chicken.ChickenType ;
}

How to get value of ValueMember of datagridviewcomboboxcolumn

Friends, I'm using datagridviewcomboboxcolumn as column index 1 in my datagridview. I've fetched data from access database table and populated the datagridviewcomboboxcolumn by following manner:
for (int i = 0; reader.Read(); i++)
{
cmbBusCode.Items.Add(reader["BUSINESS_CODE"] + "-" + reader["BUSINESS_DESCRIPTION"]);
cmbBusCode.ValueMember = "BUSINESS_CODE";
}
Then I'm writing this:
cmbBusCode.DisplayIndex = 1;
cmbBusCode.Width = 200;
cmbBusCode.Name = "Code";
cmbBusCode.HeaderText = "Code";
And adding this column as
dgView.Columns.Add(cmbBusCode);
Combobox is being populated and I can select any one from the list. Now when I'm saving data I want to get the ValueMember of the selected item. To get this value I'm using following code but It's giving the string representation of the field "BUSINESS_CODE" not the value..Please help me so that get ValueMemeber of the selected item..
foreach (DataGridViewRow row in dgView.Rows)
{
string cd = row.Cells["Code"].Value.ToString();
}
The "item" you are adding to the column doesn't have a "BUSINESS_CODE" column (essentially, the item as you add it is simply a string), so that doesn't work.
What you need to do is assign items that contain multiple columns. One must be the BUSINESS_CODE column (as it is this column you want to be the value for the underlying field in the DataGridView), the other should be a display column that contains the concatenated value you're adding at the moment (DisplayMember and ValueMember).
Easiest would be to create a typed data set, add a table that contains the two columns I described and fill the table with the data from the data reader.
Then, add the table as the data source for the column.
You can try adding a DataGridViewComboBoxCell instead of a string to the cmbBusCode.Items collection. You can then specify Value in the cell. I'm not sure if this will work, but it's worth a try.

How do I add to a specific column in Listview item?

I create a listview and the number of columns are determined at runtime. I have been reading texts on the web all over about listview( and I still am) but I wish to know how to add items to a specific column in listview I thought something like:
m_listview.Items.Add("1850").SubItems.Add("yes");
would work assuming the "1850" which is the text of the column would be the target column.
ListViewItems aren't aware of your ListView columns.
In order to directly reference the column, you first need to add all the columns to the ListViewItem.
So... lets say your ListView has three columns A, B and C;
This next bit of code will only add data to column A:
ListViewItem item = new ListViewItem();
item.Text = "Column A";
m_listView.Items.Add(item);
To get it to add text to column C as well, we first need to tell it it has a column B...
ListViewItem item = new ListViewItem();
item.Text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");
m_listView.Items.Add(item);
So now we'll have columns A, B and C in the ListViewItem, and text appearing in the A and C columns, but not the B.
Finally... now that we HAVE told it it has three columns, we can do whatever we like to those specific columns...
ListViewItem item = new ListViewItem();
item.Text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");
m_listView.Items.Add(item);
m_listView.Items[0].SubItems[2].Text = "change text of column C";
m_listView.Items[0].SubItems[1].Text = "change text of column B";
m_listView.Items[0].SubItems[0].Text = "change text of column A";
hope that helps!
I'm usially inherit ListViewItem as :
public class MyOwnListViewItem : ListViewItem
{
private UserData userData;
public MyOwnListViewItem(UserData userData)
{
this.userData = userData;
Update();
}
public void Update()
{
this.SubItems.Clear();
this.Text = userData.Name; //for first detailed column
this.SubItems.Add(new ListViewSubItem(this, userData.Surname)); //for second can be more
}
}
where
public class UserData
{
public string Name;
public string Surname;
}
using
listView1.Items.Add(new MyOwnListViewItem(new UserData(){Name="Name", Surname = "Surname"}));
ListViewItem item = new ListViewItem("foo");
item.SubItems.Add("foo2");
this.listView1.Items.Add(item);
ListViews don't support databinding and strongly typed naming. I would recommend considering using a DataGridView instead. Depending on what you need, it may save yourself some sanity points.
Chernikov has it nicely implemented to make things a bit saner.
This is what I do because the columns are added at run time via the users preference.
I am using a listView to display information from a database to the user. ( I probably should use DataGrid, but when I first made the program DataGrid didn't have built in full row select and I was too big of a newb to go through the custom control examples I found)
The user selects which columns they want to show up out of the total, how wide, and the title text.
I created a user preferences table that saved information about the columns to add
ColumnVisible = bool
ColumnText = string = text to display in header
columnWidth = int
ColumName = string = the exact name this column will refer to in my other database
ColumnIndex = int = the display index of the column
I also made a class ColumnPreferences and added a property for each of those columns
Used DataReader to make List<ColumnPreferences>
I sort this list according to display index, so I can iterate through it in the order the columns are displayed.
I do an if statement checking if the next preference in preferences is visible.
If it is then I add the column using ListView1.Columns.Add(ColumnName, ColumnText, ColumnWidth);
At this point the columns are all set, to save where the user drags them around to I do basically the opposite. I iterate through all column in Listview.columns and save the column display index and column width back into my user preference table
When loading the actual data from the table I use this same list of column preferences to check whether I should display the data.
it is again sorted by display index, I check if it is visible, if it is not I skip it and go to the next one
if it is visible I use myDataReader[ ColumnPreference.ColumnName ] to look up the approprate ordinal for the data I want.
I just add each of those results, in order, to a list view item / subitems then add them to the list view
Something like this maybe?
ListViewItem item = m_listview.Items.Add("1850");
item.Subitems.Add(string.Empty); //Make an add method that does this for every column you got and then you can use the indexer below.
item.Subitems[1].Text = "Test";
Index 0 is the main item. Index 1 > are the subitems.
For i = 0 To listAcheivments.Items.Count - 1
Dim arrparam2(,) As String
arrparam2 = New String(,) {{"#emp_code", txtEmpCode.Text}, {"#ach_year", CDate("01-01-" & listAcheivments.Items(i).SubItems(0).Text)}, {"#acheivement", listAcheivments.Items(i).SubItems(1).Text}}
SaveData(arrparam2, "insert_acheivements")
Next

Categories