Prevent listview items from showing in a second column - c#

I am working with Winforms using C#.
It is easier to explain my question with a screen shot:
The list view on the left, notice how items15 and later are showing in a second column. I don't want that. I want it to have a vertical scroll bar and items15 to appear under item14, etc..not in a new column. I have set the "view" property on "List" too.
Thanks all.

You need to Set
listview.Scrollable = true;
listview.View = View.Details
listview.HeaderStyle = ColumnHeaderStyle.None;
Add a dummy column, its an important step, cause we changed the View to details:
ColumnHeader header = new ColumnHeader();
header.Text = "MyHeader";
header.Name = "MyColumn1";
listView.Columns.Add(header);

Related

Horizontal view of data in the listview windows form control

I have not been able to find the google solution, I hope here than it can help me quickly.
I want my data to be displayed below each other and scrollable to the right.
I have included these controls so far, a view from the designer attached:
'ListView1 from Designer
'
Me.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None
Me.ListView1.HideSelection = False
Me.ListView1.Location = New System.Drawing.Point(26, 184)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(368, 49)
Me.ListView1.TabIndex = 27
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.ListView1.View = System.Windows.Forms.View.Details
Me.ListView1.Columns.Clear()
Me.ListView1.Columns.Add("").Width = -2
what do i miss in order for my data to appear below each other and scroll to the right(vertical to scroll)?
so I would like the data to be displayed below and not in the next row and then scroll vertically.
like in the picture that data is showing me ..
In the ListView, Set View to Details and set HeaderStyle to None. Note, that you must add a column to the ListView Control. Set the column Width to -1, so that the column spans the whole control.
This is how this would look from code, but you can also do the setup in the form designer.
// Setup listview appearance
listView1.Columns.Clear();
listView1.Columns.Add("");
listView1.View = View.Details;
listView1.HeaderStyle = ColumnHeaderStyle.None;
// Add items
listView1.Items.Add("Aber Aber aber");
listView1.Items.Add("Kunde mit ID 1234 ist nicht in der Datenbank enthalten");
listView1.Items.Add("Stackoverflow rulez");
// Adapt widht of column
listView1.Columns[0].Width = -1;

Listview has no item selected , when we click at the empty area in the listview c#

I have found some solution for this in the link below.
Disallow ListView to have zero selected items
My issue is when user clicks at the empty area in list view. the below line throws null exception.
string x = lvwIngred.SelectedItems[0].Name;
I want to use this below code before executing the above line.
sample-
if (lvwxyz.FocusedItem != null)
{
lvwxyz.FocusedItem.Selected = true;
}
string x = lvwIngred.SelectedItems[0].Name
It works fine if I use the above approach. I have set hideselection = true; multiselect = false;
First Question is - Will it work if hideselection property is set to true for all cases.
2nd Question - What will be the scenario when the lvwxyz.FocusedItem is null, and how can we fix that scenario.
I have also used lvwxyx.Items[0].Selected = true; This also fixes the issue.
Please help.

AllowUserToResizeColumns and AllowUserToResizeRows of the datagridview doesn't work

I have a datagridview that I want to allow user from resizing columns and prevent hom from resizing rows. I am setting the properties in design and also at runtime but it doesn't affect the datagridview.
The opposite happens, the rows can be resized and the columns not.
I tried to reverse the properties values but same result.
private void SetupAlternatesDataGridView()
{
var recordstateColumn = new DataGridViewTextBoxColumn();
recordstateColumn.Name = Glossary.RecordState;
recordstateColumn.HeaderText = Glossary.RecordStateTitle;
var alternateStatusColumn = new DataGridViewTextBoxColumn();
alternateStatusColumn.Name = Glossary.AliasAlternateStatus;
alternateStatusColumn.HeaderText = Glossary.AlternateStatusTitle;
var alternateNumberColumn = new DataGridViewTextBoxColumn();
alternateNumberColumn.Name = Glossary.AliasAlternateNumber;
alternateNumberColumn.HeaderText = Glossary.AlternateNumberTitle;
AlternateDGV.AllowUserToResizeColumns = true;
AlternateDGV.AllowUserToResizeRows = false;
//TODO: For test only, remove it
AlternateDGV.Columns.AddRange(recordstateColumn, alternateStatusColumn, alternateNumberColumn);
AlternateDGV.Rows.Add("added", "A", "POS-100");
}
How to make those properties work?
Just play with all the properties and found that AutoSizeColumnsMode must be set to None in order to AllowUserToResizeColumns and AllowUserToResizeRows to work.
AutoSizeColumnsMode = None
In my case, it didn't work because I had this line:
Grid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
In other words, if you ask one column to fill all available space, the user won't be able to resize it (at least on Windows 10).
But if your entire grid is set to "Fill", like this:
Grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
then resizing will be allowed.

devExpress, how to dock gridView to the panel and remove 'drag column header'?

I cannot dock fill gridView to the panel in DevExpress Visual Studio 2010, I cannot remove "drag column header..." as well, both are visible on the screenshot.
Can somebody help me with those issue?!
part of the code:`EntityConnection entityConn = new EntityConnection(Utility.GetEntityConnection("ViEFConn"));
List<Object> myList = new List<Object>();
//GridControl gridcon = new GridControl();
using (ViEFConn dbF = new ViEFConn(entityConn))
{
// LINQ join query
var query = from f in db.Fields
join t in db.Types
on f.TYPE_ID
equals t.TYPE_ID
where (f.ASS_TYPE == _assType)
select new
{
f.NAME,
f.FIELD ,
t.DATA,
f.TEXT,
};
// add linq query to the list
myList.AddRange(query);
// bind data source of grid control to the list
AssignmentDetailsGridControl.DataSource = myList;
gridView1.OptionsBehavior.Editable = true;
SaveButton.Enabled = true;
SaveCloseButton.Enabled = true;
AssignmentDetailsGridControl.Dock = DockStyle.Fill;
gridView1.OptionsCustomization.AllowColumnMoving = false;
gridView1.OptionsView.ShowGroupPanel = false; `
To dock your grid you need to set
GridControl.Dock = DockStyleFill
Note: it is property of GridControl, not GridView.
It is not quite clear what do you mean by remove "drag column header...".
If you want to disable dragging columns you need to set property
GridView.OptionsCustomization.AllowColumnMoving = false
If you want to disable group panel (marked by arrow on your screenshot) you need to set
GridView.OptionsView.ShowGroupPanel = false
You can hide the Grouppanel with
gridview1.optionsview.showgrouppanel = false;
For remove the right empty area you have to set
dockstyle = fill
or you can place DevExpress LayoutControl at first. Set this to docktype=fill and place the grid on the layout control (thats recommended by DevExpress!).
At least make sure you havent place an emptyspaceitem at the right sight.

Why does the ListView refuse to display its columns, items, and subitems (shows only Group)?

The ListView seems to be as cantankerous as a polecat and as temperamental as a [elided by the P.C. police]
With help from - and others, I was able to get a ListView working just as I wanted
it to
(http://stackoverflow.com/questions/11423537/how-to-add-subitems-to-a-listview).
Now I'm converting that simple demo for use in a real app.
I create the columns in the form's Load event:
listViewGroupsItems.Columns.Add(colHeadName);
listViewGroupsItems.Columns.Add(colHeadRate);
...but they don't display. However, there is a blank line above my first item. So why are no text values being displayed. Are they "there" but invisible? Why would they get wiped out?
Anyway, what I want to see is:
column1Title column2Title
Group1Name
Item subitem
Item subitem
Group2Name
Item subitem
Group1Name
Item subitem
Item subitem
Item subitem
Item subitem
...but what I actually see is just:
[blank line]
Group1Name
...that's it!
The ListView's View property is set to Details; otherwise, all of the properties are the default values.
My listview code is:
private void AddGroupsAndItems() {
Dictionary<string, string> GroupsDict = PlatypusData.GetGroupsForTopLevel(Convert.ToInt32(labelTopLevel.Tag));
int currentGroup = 0;
foreach (KeyValuePair<string, string> entry in GroupsDict) {
string GroupNumber = entry.Key;
string GroupName = entry.Value;
listViewGroupsItems.Groups.Add(new ListViewGroup(GroupName, HorizontalAlignment.Left));
Dictionary<string, string> ItemsDict = PlatypusData.GetItemsForGroup(GroupNumber);
foreach (KeyValuePair<string, string> itemEntry in ItemsDict) {
string itemID = itemEntry.Key;
string itemName = itemEntry.Value;
ListViewItem lvi = new ListViewItem(string.Format("{0} ({1})", itemName, itemID));
lvi.SubItems.Add(PlatypusData.GetDuckbillNameForItemId(itemID));
listViewGroupsItems.Items.Add(lvi);
listViewGroupsItems.Groups[currentGroup].Items.Add(lvi);
}
currentGroup++;
}
}
UPDATE
I changed my code in the form's Load event from:
var colHeadName = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_Billing_Account_Client, Width = 160 };
var colHeadRate = new ColumnHeader { Text = Resources.RateRequestForm_RateRequestForm_Load_RatePlan_ID, Width = 120 };
listViewCustomerBillAccountsClients.Columns.Add(colheadName); //colHeadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate); //colHeadRate);
...to:
ColumnHeader colheadName = new ColumnHeader();
ColumnHeader colheadRate = new ColumnHeader();
listViewCustomerBillAccountsClients.Columns.Add(colheadName);
listViewCustomerBillAccountsClients.Columns.Add(colheadRate);
...and it made no difference at all.
It would seem that the ColumnHeader constructor should be able to take a string of what it should display, but even when I do that:
ColumnHeader colheadName = new ColumnHeader("This");
ColumnHeader colheadRate = new ColumnHeader("That");
...there is still no change (according to Intellisense or whatever it's called, the string arg is an ImageKey, but I thought I'd try just out of thoroughness/frustration.
Late to the party, but I've just spent some time trying to solve a similar problem. The answer I found was that when clearing the list before filling, you must use
listviewGroupItems.Items.Clear();
not
listviewGroupItems.Clear();
The ListView.Clear() method clears everything from the control--including the columns
I had the same issue today. I had coded listView.Clear() as user2867342 mentioned. I needed to change that to listView.Items.Clear(), but that did not make the columns appear. The columns were there, and I could click on them and resize them, but they were completely blank.
I had a ListView set to Details mode. I also had set the OwnerDraw property to true, because I wanted to paint my own progress bar column. MSDN says the following about the OwnerDraw property (emphasis mine):
A ListView control is normally drawn by the operating system. In order
to customize the appearance of ListView items, subitems, and column
headers, set the OwnerDraw property to true and provide a handler for
one or more of the following events: DrawItem, DrawSubItem,
DrawColumnHeader. This is called owner drawing. When the View property
is set to View.Details, all three events occur; otherwise, only the
DrawItem event occurs.
I had to implement the DrawColumnHeader event. In my case, the defualt worked fine, so the method sets the DrawDefault event parameter to true. After implementing this event handler, the column headers appeared correctly:
...Windows.Forms designer code...
listView.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(this.listView_DrawColumnHeader);
...
private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
You're missing column headers in your code. (fixed)
Per the MSDN:
"If your ListView control does not have any column headers specified and you set the View property to View.Details, the ListView control will not display any items. If your ListView control does not have any column headers specified and you set the View property to View.Tile, the ListView control will not display any subitems."
Granted, you'll probably need to make more adjustments than what you see in my SS, but it at least answers your question as to why you are getting blanks.
Edit (Changed lines, although changing them back to your code didn't skew my successful results):
lvi.Group = listViewGroupsItems.Groups[currentGroup];
listViewGroupsItems.Items.Add(lvi);
One more thing to check I just found:
OwnerDraw must be false.
Copy & Paste from previous project and forgot that I used OwnerDraw

Categories