I need to populate a list that has 3 columns.
First: Icon Image.
Second: A string.
Third: A string.
I started to wire up a DataGridView and it seem awfully large and powerful for my needs. I am not interested in displaying data in a grid similar to a spreadsheet and I do not require all the functionality that comes with this control. Is there a better alternative?
Thanks!
You could go for the ListView in View.Details view, as this is similar to the view that you see in Windows Explorer when in Details view.
Create a form and place on it an ImageList and a ListView named imageList1 and listView1 respectively. Then place the following code into the forms load method:
listView1.Columns.Add("Image");
listView1.Columns.Add("Text1");
listView1.Columns.Add("Text2");
listView1.SmallImageList = imageList1;
var icon = Icon.ExtractAssociatedIcon(#"c:\windows\explorer.exe");
imageList1.Images.Add(icon);
var item = new ListViewItem();
item.ImageIndex = 0;
var subItem1 = new ListViewItem.ListViewSubItem();
subItem1.Text = "Text 1";
var subItem2 = new ListViewItem.ListViewSubItem();
subItem2.Text = "Text 2";
item.SubItems.Add(subItem1);
item.SubItems.Add(subItem2);
listView1.Items.Add(item);
The code really couldn't hurt for some tidying up, but it calls out quite explicitly what it's doing and should hopefully make it quite clear how to use a ListView for this purpose.
You could try ListView. You'll want to use the Columns property and Details mode.
Related
Is there a way to show the subitems of an item in a listview below it? I don't want to separate the item in first column and subsequent subitems in subsequent columns, I want it all in a single column and if there were to be more items I want every item separated by a line. Something like this but in C#.
Maybe if it is not possible could it be to make every item like an expandable list?
My code for now is:
lstEvents.View = View.Details;
lstEvents.ForeColor = Color.Black;
lstEvents.Columns.Add("Events");
ListViewItem item = new ListViewItem("Event name");
item.SubItems.Add("23/04/2012");
item.SubItems.Add("$20000");
item.SubItems.Add("this would be the event description");
im using windows forms
In Tile view, each "column" appears as a "row" under the primary column. This will give you what you asked for, but probably not what you want.
You can owner draw the Tile view to produce something like this
(source: sourceforge.net)
You can do this all yourself using just a plain ListView, but the ObjectListView project makes this much easier.
You could subclass the listview and do it yourself.
Or, you can tag certain rows as subrows of the one above it using .Tag = IndexOfPreviousRow and track the relationships in a model. It's kinda hacky, though.
As far as doing it natively with the .NET listview, I don't believe you can.
I have two ListBox in my winforms application, I assigne a datasource for both of them as follow:
private void MakeMeasurementUnits()
{
var units = new List<MeasurementUnit>
{
new MeasurementUnit {Name = "Current", SiUnit = "A"},
new MeasurementUnit {Name = "Voltage", SiUnit = "V"},
new MeasurementUnit {Name = "Time", SiUnit = "s"},
new MeasurementUnit {Name = "Temprature", SiUnit = "°C"}
};
lbxXunit.DataSource = units;
lbxYunit.DataSource = units;
}
The strange thing is (or maybe because it is my first time!!), in the form when I click on items of one of these lisboxes, the same item in the second listbox gets selected as well. Is this a default behaviour? how to prevent this? If this is default behaviour, what is useful about it?
I found the quick remedy to be making two different datasources (same thing with another name)
The listbox seems to cache the binding source. This is default behavior. If you want to avoid this, the easy way is to create a copy of the list to bind to the second data source:
lbxXunit.DataSource = units;
lbxYunit.DataSource = units.ToList();
This is useful when you have multiple views of the same data and want to synchronize the selection of these items.
Yes, this is normal behaviour. It happens because the ListView control uses a BindingSource object to track the currently selected item. (A List has no way to track a selected item without a BindingSource.)
By default, a DataSource in a WinForms control uses a BindingSource created for it by the WinForms system itself.
You can read more about the BindingSource at:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
There is an article here which might help too:
http://blogs.msdn.com/b/bethmassi/archive/2007/09/19/binding-multiple-comboboxes-to-the-same-datasource.aspx
The behavior you have noted is the default/correct behavior for winforms controls. You can achieve what you are after by setting a new BindingContext for your second listbox control without creating a copy of your data source.
BindingContext
This is correct behaviour. The datasource management in WindowsForms keeps track of the selected item on control and manipulates binded data too.
The resolution you've found already: is assign 2 different data sources objects to these controls.
How do I dynamically create a DataGridView in C#? Could you please provide an example?
You can create it like any other controls.
place a PLACEHOLDER control in your page (this will serve as start point)
so your page looks like
<body>
<form id="form" runat="server" />
<asp:PlaceHolder id="ph" runat="server" />
</body>
Then, in your code behind, just create and add controls to the Place Holder
// Let's create our Object That contains the data to show in our Grid first
string[] myData = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I" };
// Create the Object
GridView gv = new GridView();
// Assign some properties
gv.ID = "myGridID";
gv.AutoGenerateColumns = true;
// Assing Data (this can be a DataTable or you can create each column through Columns Colecction)
gv.DataSource = myData;
gv.DataBind();
// Now that we have our Grid all set up, let's add it to our Place Holder Control
ph.Controls.Add(gv);
Maybe you want to add more controls?
// How about adding a Label as well?
Label lbl = new Label;
lbl.ID = "MyLabelID";
lbl.Text = String.Format("Grid contains {0} row(s).", myData.Length);
ph.Controls.Add(lbl);
// Done!
Hope it helps get you started
Based on your reply that you are using WinForms. First a very simple example, then a little discussion of issues to consider based on "typical" usage scenarios.
Here's a specific example where, in response to a click on a button at run-time, a new DataGridView is created, positioned on the Form, sized, etc. :
// declare a form scoped variable to hold a reference
// to the run-time created DataGridview
private DataGridView RunTimeCreatedDataGridView;
// sample of creating the DataGridView at run-time
// and adding to the Controls collection of a Form
// and positioning and sizing
// fyi: the Form used here is sized 800 by 600
private void button1_Click(object sender, EventArgs e)
{
RunTimeCreatedDataGridView= new DataGridView();
RunTimeCreatedDataGridView.Size = new Size(300, 542);
RunTimeCreatedDataGridView.Location = new Point(10,12);
this.Controls.Add(RunTimeCreatedDataGridView);
}
You can, of course simplify setting Size and Location using the Bounds property, or the method 'SetBounds as in :
RunTimeCreatedDataGridView.SetBounds(10,12,300,542);
You may wish to set other properties that determine Size and Location "automatically" by setting the Dock or Anchor properties.
And you will probably want to "custom configure" the DataGridView's visual appearance in other ways via adding calls to set BackGroundColor, BorderStyle, etc. to the above code.
By this time, I'd hope you are thinking something like: "what about the really important things like configuring the columns, databinding, etc. ?" What about all that wonderful functionality exposed at DesignTime by the "Smart Tag" on the DataGridView's upper right corner, and in the Property Browser window.
Here's where we get general, rather than specific.
If you are "sure" that at some point the run-time user is going to want to create a DataGridView: why not create it in advance: visually style it, create columns, etc., and then hide it when the Form Loads: then show it on demand.
If you absolutely must create a DataGridView from scratch at run-time, but want to avoid a lot of typing : first create the DataGridView at design-time, go into the Designer.cs file and copy out the automatically generated code that is useful for you for visual style, adding and configuring columns: then paste that code in the method or event where you create the DataGridView (yes, you'll need to 'tweak it a bit).
Since, in this case, we know nothing about what you might, or might not, be binding the DataGridView to, we'll just stay "mum" on that one.
In (the odd ? off chance ?) the unlikely case you are creating multiple DataGridViews at run-time, suggest you maintain an internal list of them in a variable like List<DataGridView> and have one variable named currentDataGridView that you can rely on to hold a reference to the currently active (has focus, is visible, etc.) DataGridView.
In every case I recommend using "messing around" with a DataGridView in design-time mode, and then examining the code produced in the Designer.cs file (but never altering it !) to get quick information on how to use various features of the DataGridView. And for major examples of binding a DataGridView to complex DataSources and formatting: do check out CodeProject for relevant articles, tips, etc.
"Harvest" what you need from the automatically generated code in Designer.cs file, and then, when you are ready, delete the DataGridView instance on a Form, and "do your own thing" at run-time.
GridView gv = new GridView();
gv.datasource = dt;
gv.databind();
and then place this gv in panel or div or table coloumn.
You can check this link http://www.ehow.com/how_5212306_create-datagridview-c.html
I am doing a windows mobile application 6.1.
I dragged in a listview and went to columns and added columns to my list view. When I run the listview they do not show up.
I then tried to add them through C# code on page load with the follow code.
ColumnHeader header = new ColumnHeader();
header.Text = "gkgag";
header.Width = 100;
header.TextAlign = HorizontalAlignment.Center;
listView1.Columns.Add(header);
this does not work either. Why don't they show up?
You must use detailed view for column headers to be visible.
listView1.View = View.Details;
If that´s not the problem, column headers might be hidden behind windows systembar.
The problem is as:
I have horizontal asp:RadioButtonList, with 3 buttons witch i add programaticly C# code
The thing is that the third button must be on second line. How can i do it besides creating a new asp:RadioButtonList?
I have tried adding br and \r\n to caption but that dose not help me.
ListItem _item1 = new ListItem("1", "1");
ListItem _item2 = new ListItem("2", "2");
ListItem _item3 = new ListItem("3", "3");
rbl.Items.Add(_item1);
rbl.Items.Add(_item2);
rbl.Items.Add(_item3);
Use the RepeatColumns property ? Set it to 2 and the third one should bump down a line. This creates a column approach. Only downside to it if the labels are long it might not look right.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.repeatcolumns.aspx
If the labels are really long, then I would suggest moving to a repeater or listview and roll your own. You could use a placeholder control in the item template and in the itemdatabound event add the radio buttons based the count or index and use a mod to decide when to place a break element. Definitely a hack though.
You could look into using CssAdapters they offer a lot of flexibility for tweaking control layouts.