I'm not sure why this isn't working. I'm trying to display 2 columns from my database side by side in a listview box on my form. When I use this it doesn't display any of the data correctly.
("SELECT Person FROM tblPeople" + " SELECT Occur FROM tblpeople" , conn);
try
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
listView1.Items.Add(reader["People"].ToString());
listView1.Items.Add(reader["Occur"].ToString());
}
So i'm looking for my data to display like this:
John 3
James 4
Frank 1
As the names are coming from column People and the numbers are coming from column Occur.
To get the desired effect, you should set the view style to Details and add the second column value as sub-item.
Basically, you should do something like this:
listView1.View = View.Details;
listView1.Columns.Add("People");
listView1.Columns.Add("Occur");
while (reader.Read())
{
var item = new ListViewItem();
item.Text = reader["People"].ToString(); // 1st column text
item.SubItems.Add(reader["Occur"].ToString()); // 2nd column text
listView1.Items.Add(item);
}
Method Add() adds every time a new item to the collection. If your item consists of values from two objects, probably the easiest way would be to create a new item to encapsulate it, so you can easily bind it to your controls.
Consider something like this:
public class MyNewObject
{
public string People { get;set; }
public string Occur { get;set; }
public MyNewObject(string p, string o)
{
People = p;
Occur = o;
}
}
and then just
while (reader.Read())
{
listView1.Items.Add(new MyNewObject(reader["People"].ToString(),reader["Occur"].ToString()));
}
Related
My project is c# windows form with Entity Framework , and I have DataGridView with a TextColumn. I want to edit the last column TextColumn Cells[3].
To get all values from database to GridView is not problem. I get them.
And I can put a new value but as soon as I click ontherplace then it changes to the old value. Textbox is not keeping the new value that I want to edit.
ReadOnly is false for this cells in column 3 becouse I can write but changes back the new value to the old values.
What kind of events I'am missing and how shall I do to fix this problem? By some how it seems like the hole Gridview is locked. Please Help.
I tried even:
foreach (DataGridViewRow row in dgvOrder.Rows)
{
row.Cells[3].ReadOnly = false;
}
Here below is my code.
private void Treatments_Load(object sender, EventArgs e)
{
try
{
using (MyHealthEntities db = new MyHealthEntities())
{
var orderd = db.Order.Where(x => x.Ordernr == OrNr).FirstOrDefault();
if(orderd != null)
{
var myOrder = (from u in db.....
join d in ...
join m in ...
where u.....
select new
{
OrderId = m.MedId,
Name = m. Name,
Quality = m.Quality,
Description = d.Description
}
).ToList();
if (myOrder != null)
{
dgvOrder.DataSource = myOrder ;
}
}
foreach (DataGridViewRow row in dgvOrder.Rows)
{
row.Cells[3].ReadOnly = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
If I'm not mistaken, a normal System.Collections.Generic.List will not support editing when directly bound to because it does not implement IBindingList (or the necessary ListChanged event) for two-way data binding. You will likely need to wrap the List into a BindingList in order to edit the values. This will not be possible with an anonymous type.
First, create a class to store your List items:
public Class Order {
public int OrderID { get; set; }
public string Name { get; set; }
public byte Quality { get; set; }
public string Description { get; set; }
}
Then in your query,
select new Order
{
OrderId = m.MedId,
Name = m. Name,
Quality = m.Quality,
Description = d.Description
}
Now move it to a BindingList:
if (myOrder != null) {
BindingList<Order> myBindingList = New BindingList<Order>(myOrder);
dgvOrder.DataSource = myBindingList;
}
I would also recommend wrapping the BindingList in a BindingSource, which will prevent you from having to handle rows being added/deleted manually:
if (myOrder != null) {
BindingList<Order> myBindingList = New BindingList<Order>(myOrder);
BindingSource myBindingSource = New BindingSource(myBindingList);
dgvOrder.DataSource = myBindingSource;
}
Apologies if my syntax is a little off, I've been mostly using vb as of late.
EDIT: I missed that BindingList does not implement IContainer, so the above code for binding to a BindingSource will not work because the single-parameter constructor for BindingSource specifically takes an IContainer. If you still want to use a BindingSource, the third constructor of BindingSource should be used instead, like so:
BindingSource myBindingSource = New BindingSource(myBindingList, Nothing);
BindingSource does accept binding to an IBindingList, but only using this constructor or by directly setting the .DataSource property after using the parameterless constructor.
Leaving the erroneous code above so others who read OP's comment will understand what was being referenced.
You can't edit DataGrid itens when you are using "dgvOrder.DataSource = myOrder".
My suggestion is you do and "for each" in your myOrder list and add row by Row into Grid. After, you can do an other "for each", recovery the datas from DataGrid and Save the itens in your db.Order.
I'm reading in a field from a database into a list, like so
PaceCalculator pace = new PaceCalculator();
List<PaceCalculator> Distancelist = new List<PaceCalculator>();
while (Reader.Read()) //Loops through the database and adds the values in EventDistance to the list
{
pace.Distance = (int)Reader["EventDistance"];
Distancelist.Add(pace);
}
I want to put the values into a listbox, but when I do it like this:
listBox1.DataSource = Distancelist;
It only shows the class name, which is PaceCalculator. It shows the right number of values, it just shows the class name instead. I want to see the integers in there.
You have two options,
Override ToString in your class to return the required string
or, if you only want to display Distance then specify that as DisplayMember
like:
listBox1.DisplayMember = "Distance";
listBox1.DataSource = Distancelist;
This will display you the Distance element from your list. Or you can override ToString in your class PaceCalculator like:
public override string ToString()
{
return string.Format("{0},{1},{2}", property1, property2, property3);
}
EDIT:
Based on your comment and looking at your code, You are doing one thing wrong.
this only displays the last value in the list, 46, 8 times
You are adding the same instance (pace) of your class in your list on each iteration. Thus it is holding the last value (46). You need to instantiate a new object in the iteration like:
while (Reader.Read())
{
PaceCalculator pace = new PaceCalculator();
pace.Distance = (int)Reader["EventDistance"];
Distancelist.Add(pace);
}
Specify the property of PaceCalculator to display.
listBox1.DataSource = Distancelist;
listBox1.DisplayMember = "Distance";
The ListBox control allows you to pick a property from the collection to display to the user.
There's also a ValueMember property that allows you to specify the value for each item in the ListBox. Assuming your data included an id called "SomeUniqueRecordId", for instance:
listBox1.ValueMember = "SomeUniqueRecordId";
I am working with a link list. The list has items based on inserted values from textboxes (size, height, stock, price). . Through the button click the Add function takes values from texboxes and appends item but every time I add an item to the list instead of placing at the end of the list it always placed first. I am not sure why such behavior. How can I modify so it can append the item always to the end of the list? (I am displaying my test results on a fifth multiline textbox called results).
Code
public void Add()
{
textBoxResults.Clear();
int stock = Convert.ToInt32(Stock.Text);
string type = Type.Text;
double price = Convert.ToDouble(Price.Text);
int height = Convert.ToInt32(Height.Text);
Tree = new FruitTrees();
Tree.Stock = stock;
Tree.Type = type;
Tree.Price = price;
Tree.Height = height;
Total += Tree.Price * Tree.Stock;
Trees.AddLast(Tree);
}
Is this a school project? Just wondering why you aren't using System.Collections.Generic.LinkedList<T>.
LinkedList<FruitTrees> trees = new LinkedList<FruitTrees>();
It comes with its own AddLast method which does exactly what you want.
Also, I think your class name shouldn't be plural (FruitTree vs FruitTrees).
You could implement your own AddLast, something like this:
FruitTrees item = Trees.Retrieve(0);
while (item.Next != null)
{
item = item.Next;
}
item.Next = NewItem
I have a custom object with several properties, one of which returns a list. This is the code for the object:
public class SearchResult
{
private int eventId;
private String eventTitle;
private int startDate;
private List<String> tags;
// Properties
public int EventId { get { return this.eventId; } }
public String EventTitle { get { return this.eventTitle; } }
public int StartDate { get { return this.startDate; } }
public List<String> Tags { get { return this.tags; } }
public SearchResult(int eventId, String eventTitle, int startDate, List<String> tags)
{
// Constructor code
}
public List<String> GetTags()
{
return this.tags;
}
}
I also have a DataGridViewComboBoxColumn that I want to bind to the Tags property. Basically, each SearchResult object will be displayed in its own row, and I want the List<String> in the Tags property of each object to be displayed in a ComboBox cell in that row. This is the code I have so far for my DataGridView:
BindingList<SearchResult> results = new BindingList<SearchResult>();
results.Add(new SearchResult(1, "This is a title", 2012, new List<String> { "Tag1", "Tag with a long name1" }));
results.Add(new SearchResult(2, "The quick brown fox", 2012, new List<String> { "Stack", "Overflow" }));
results.Add(new SearchResult(3, "In this tutorial, you create a class that is the type for each object in the object collection. ", 2012, new List<String> { "NYSE", "FTSE" }));
results.Add(new SearchResult(4, "another long piece of title text", -999, new List<String> { "Rabbits", "Chickens" }));
MyDataGrid.AutoGenerateColumns = false;
MyDataGrid.AllowUserToAddRows = false;
MyDataGrid.AllowUserToDeleteRows = false;
MyDataGrid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.None;
MyDataGrid.BackgroundColor = System.Drawing.SystemColors.Control;
MyDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
MyDataGrid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
MyDataGrid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
MyDataGrid.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
DataGridViewTextBoxColumn eventIdColumn = new DataGridViewTextBoxColumn();
eventIdColumn.DataPropertyName = "EventId";
eventIdColumn.HeaderText = "Event ID";
eventIdColumn.ReadOnly = true;
eventIdColumn.Width = 84;
DataGridViewTextBoxColumn eventTitleColumn = new DataGridViewTextBoxColumn();
eventTitleColumn.DataPropertyName = "EventTitle";
eventTitleColumn.HeaderText = "Event Title";
eventTitleColumn.ReadOnly = true;
eventTitleColumn.Width = 368;
DataGridViewTextBoxColumn startDateColumn = new DataGridViewTextBoxColumn();
startDateColumn.DataPropertyName = "StartDate";
startDateColumn.HeaderText = "Start Date";
startDateColumn.ReadOnly = true;
startDateColumn.Width = 130;
//I think I need to insert the code for the tags column here, but I'm not sure
MyDataGrid.Columns.Add(eventIdColumn);
MyDataGrid.Columns.Add(eventTitleColumn);
MyDataGrid.Columns.Add(startDateColumn);
//MyDataGrid.Columns.Add(tagsColumn);
MyDataGrid.DataSource = results;
I derived this code from a tutorial I found online, and it works perfectly.
I've been trying to bind the Tags property of SearchResult to a DataGridViewComboBoxColumn, but I'm not sure how. I've been looking at this question, which provides this code:
column.DataPropertyName = "Foo";
column.DisplayMember = "SomeNameField";
column.ValueMember = "Bar"; // must do this, empty string causes it to be
// of type string, basically the display value
// probably a bug in .NET
column.DataSource = from foo in Foo select foo;
grid.DataSource = data;
The reason I'm having trouble is because of a few nuances of the linked question that I don't understand.
According to the documentation and the linked question, DisplayMember should be linked to the property that "contains a description of the instance", but since SearchResult objects are added dynamically and don't have any description associated with them, should I just leave it blank?
ValueMember is giving me similar problems, since I'm unsure what to put even after reading its documentation.
In the linked question, the accepted answer binds the entire datagrid at once using LINQ. Is that how I should be doing this? I'm not sure how to modify that code for my situation, but I thought it would be something along these lines.
:
tagsColumn.DataPropertyName = "Tags";
tagsColumn.DisplayMember = ""; // I'm unsure of what to put here
tagsColumn.ValueMember = ""; // Once again, I don't know what to set this to
I also presume I should have a line that sets the DataSource for the column, e.g.
tagsColumn.DataSource = <some LINQ query, perhaps?>
but I don't know because the only mostly relevant C# source I've been able to find is that question.
UPDATE:
I did find a second question that suggests code similar to this for data binding:
// reference the combobox column
DataGridViewComboBoxColumn cboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];
cboBoxColumn.DataSource = Choice.GetChoices();
cboBoxColumn.DisplayMember = "Name"; // the Name property in Choice class
cboBoxColumn.ValueMember = "Value"; // ditto for the Value property
Based on that, I a) added the GetTags() method to SearchResult and added this code into my DataGridView initialisation code:
DataGridViewComboBoxColumn tagsColumn = new DataGridViewComboBoxColumn();
tagsColumn.DataSource = SearchResult.GetTags(); // ERROR
tagsColumn.DisplayMember = ""; // Still not sure
tagsColumn.ValueMember = ""; // ??
However, Visual Studio gives me an error on the second line when I try to run this:
An object reference is required for the non-static field, method, or property 'SearchResult.GetTags()'
UPDATE 2:
I'm still searching around for this without success. I don't understand how with other properties (e.g. EventId) I can simply declare the data property name as EventId, and it will display in the table, but I cannot do this with ComboBox columns.
Since the objects are instantiated in a separate class and put in a list, it doesn't seem to make sense to me that I should have to loop through the entire array of objects (of which there may be several hundred) to bind the Tags property to the ComboBox column for each instance, when I don't need to loop through the list of SearchResult objects to bind other properties, e.g. EventId.
Why does this binding-properties-by-name only work for some properties and not others?
I don't quite understand why you want to use DataGridViewComboBoxColumn to display a list of elements. This column kind is designed to allow user to select one of many possibilities. It seams it is not your case because you don't have public string SelectedTag{get;set;} property to store it. As I understand your model you have many tags already selected for your SearchResult and you want to display them in grid.
As documentation states:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource
Getting or setting this [DataSource] property gets or sets the DataSource property of the object returned by the CellTemplate property. Setting this property also sets the DataSource property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.
DataGridViewComboBoxColumn simply does not have capability to bind items property to data source because it assumes that there is only one list of elements that is used as data source for all rows of data grid.
I also assume that you would set ReadOnly = true property for this column as you have for all other. If so it would prevent user form seeing list of tags because drop down list would never be displayed.
If you wand to display list of strings in read only mode I would suggest to flatten this list of tags to single string:
public string Tags { get { return string.Join(", ", tags); } }
and display it in text column.
For the error , i can suggest you to make an instance of the class and then call the method as its not static or you can make your method static.
Moreover As you needs the comboboxcolumn ,
DataGridViewComboBoxColumn tagsColumn = new DataGridViewComboBoxColumn();
tagsColumn.DataSource = SearchResult.GetTags(); // ERROR
tagsColumn.DisplayMember = ""; // Still not sure
tagsColumn.ValueMember = ""; // ??
Mostly we have dropdowns for objects like Country(id,name) so DisplayMember = name will be shown as text in dropdown while ValueMember = id will be used in the referencing tables in database.But this is not your case.
Here you have a list of strings to show in dropdown , so you don't need to set them.
As written here
If the DataSource property is set to a string array, then ValueMember
and DisplayMember do not need to be set because each string in the
array will be used for both value and display.
I have quite a few radiobuttonLists in my ASP.net webform. I am dynamically binding them using the method shown below:
public static void PopulateRadioButtonList(DataTable currentDt, RadioButtonList currentRadioButtonList, string strTxtField, string txtValueField,
string txtDisplay)
{
currentRadioButtonList.Items.Clear();
ListItem item = new ListItem();
currentRadioButtonList.Items.Add(item);
if (currentDt.Rows.Count > 0)
{
currentRadioButtonList.DataSource = currentDt;
currentRadioButtonList.DataTextField = strTxtField;
currentRadioButtonList.DataValueField = txtValueField;
currentRadioButtonList.DataBind();
}
else
{
currentRadioButtonList.Items.Clear();
}
}
Now, I want to Display only the first Letter of the DataTextField for the RadioButton Item Text.
For example if the Value is Good I just want to Display G. If it Fair I want to display F.
How do I do this in C#
Thanks
You can't do what you want when you do the binding, so you have 2 options:
Modify the data you get from the table, before you do the binding.
After binding, go through each item and modify its Text field.
So, it you want to display "only the first Letter of the DataTextField for the RadioButton Item Text", you can do:
currentRadioButtonList.DataSource = currentDt;
currentRadioButtonList.DataTextField = strTxtField;
currentRadioButtonList.DataValueField = txtValueField;
currentRadioButtonList.DataBind();
foreach (ListItem item in currentRadioButtonList.Items)
item.Text = item.Text.Substring(0, 1);
If I misunderstood you and you want to display the first letter of the Value field, you can replace the last two lines with:
foreach (ListItem item in currentRadioButtonList.Items)
item.Text = item.Value.Substring(0, 1);
You could add a property to the type that is being bound (the one that contains Good, Fair, etc.) and bind to this property. If you will always be using the first letter, you could make it like so (adding in null checks, of course):
public string MyVar { get; set; }
public string MyVarFirstChar
{
get { return MyVar.Substring(0, 2); }
}