Visual Studio 2012 c# ListView issue - kinda strange one - c#

I'll try to explain best I can.
I have a ListView in visual studio c# form, with a function to populate it. When I recently tried to update that ListView by adding a column, it just showed only previous columns populated. Then, for an even greater surprise. When I changed function text in order to populate "old" columns a bit differently just to make sure it works (like, adding string "test string" to a column populating string) nothing happened - it just stayed the old way. After that, I completely erased population function body, leaving nothing inside brackets - and listview was still populated with data, like it called the original version of function, even though I had just erased it completely. Left me puzzled. Failed to google anything similar. More funny stuff - i put a MessageBox into function body just to make sure that "new" function is called at all. And yes, MessageBox is being displayed so it actually accepts that change of function. Code to show it:
private void populateMissionList()
{
this.listView1.Items.Clear();
List<MisijaDTO> misije = MisijaDAO.ocitajSve();
foreach (MisijaDTO mission in misije)
{
string idmisije = "" + mission.Id;
ListViewItem stavka = new ListViewItem(idmisije);
stavka.SubItems.Add(mission.Naziv); // "old" column
stavka.SubItems.Add(mission.Datum + " test string"); //"old" column, but won't show newly added "test string"
if (mission.Uspjesna){
stavka.SubItems.Add("Uspješna"); // "new" column, won't be shown...
MessageBox.Show("282 true"); //... but this text box is shown
}
else {stavka.SubItems.Add("Neuspješna");MessageBox.Show("282 false");}
listView1.Items.Add(stavka);
}
}
My best guess why this happened - I have previously cut that listView from the form, pasted it onto another form (which I used as a backup), and later copied it from backup form and pasted it back onto original form. I suppose ListView somehow got stuck with the old function definition, but I wasn't able to solve it. Tried to: rename ListView, rename population function, clean solution, rebuild solution, close and reopen Visual Studio... nothing helped. It's not database problem either, because it will read new database imports into ListView, but limited to "old" set of columns like it won't recognize ListView that I pasted back. ListView name remaine same after cut/paste/copy/paste, so I guess that's not issue either.
If you need some more information, I'd be glad to provide.

Look in the .designer.cs file where the Column Collection is populated. You probably need to add another column into that....

I owe A BIG apologie to anyone who spent their precious time reading this topic. There isn't any bug or something, but I overlooked some piece of code that was related to my function and that populated list the old way separately. Seems everything works just fine right now.
Good side of this is that I signed up for Stack Overflow finally :)

Related

Adding a new column is deleting all the existing columns of datagrid view

I am using a datagridview in which I have added columns in designmode and it is not databound.This grid was working perfectly fine for a long time.Now I want to insert a new column to the datagrid view,and I am not able to add the column.If I am adding it is deleting all the existing columns of datagridview.I am not even able to edit the existing columns as the same thing is happening(Deletion of all the columns of datagrid view).I have googled so much and couldn't find a possible solution.If anyone has experienced the same issue kindly provide a solution.
note:Am not adding any rows or columns programatically.Its a windows application using c# .net vs2010.
thanks
If you experience problems with the winforms designer, try deleting and newly adding your existing control. Make sure, that you do not change any code in the designer code file unless you really know what you are doing.
There was a change in visual studio, when form become split into 2 files: form.cs and form.designer.cs. I still have plenty of single-file forms myself but they are working without issues.
My guess you have done something to the form. To example, new constructor with parameter. Or manually edited InitializeComponent method (never do it!). Try to reverse that, if you can, for a time while using designer.
More clean would be to simple write down all things you have to do to DataGridView, by inspecting InitializeComponent method code, then simply add fresh DataGridView and do everything again in designer.
Even better solution would be to re-design form from scratch. Refactoring few years old code is good idea, especially if you have issue like this one.
From the forums I have read, this seems to be an existing bug in Visual Studio. I recommend that you manually write out the code in your "Form.Designer.cs". You will have to enter your respective code appropriately and then your column will automatically appear in your DataGridView without the "Column is not a valid identifier" issue that appears when using the Graphical User Interface.
Form.Designer.cs
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.YourColumnName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.YourColumnName
});
//
// YourColumnName
//
this.YourColumnName.HeaderText = "Your Column Name";
this.YourColumnName.Name = "YourColumnName";
this.YourColumnName.ReadOnly = true;
//
}
#endregion
private System.Windows.Forms.DataGridViewTextBoxColumn YourColumnName;

ListView.Refresh 'clears' the listview

My code is looping through operations and is adding feedback to a listview control. Previously, my code has been working OK, but something has changed today...
The listview control starts off like this:
...but after I issue a call to Update() or Refresh(), rather than show the items that I have added, it renders like this:
At this point, the control is still enabled and visible. In fact the only interaction my code has with it is to add new items and Update().
To add items, I'm using the following:
lvwDrawings.Items.Add(new ListViewItem(new string[]
{
drawing.PartNo,
drawing.Revision.ToString(),
drawing.Issue,
drawing.DrgTypeText,
errorStatus !=null ? errorStatus : drawing.Status,
drawing.Filepath
}));
In case I'd introduced some problem here, I tested with the simpler lvwDrawings.Items.Add("test");, but the result was the same. If I do a QuickWatch, the control correctly tells me that it contains x items...
OK, I've found the problem - for some unknown reason, where I'd previously had lvwDrawings.Items.Clear() to clear the list between different runs, I somehow ended up with lvwDrawings.Clear(). This clears not only the items in the listview, but also the columns.
It's curious on two counts: I would have though that when I tried to add a ListViewItem with specific columns that it would have objected when there weren't any columns, and also, what on earth did I think I was doing when I made the change(??!).
Moderately interesting diversion:
I discovered the problem by creating a second listview below the first, and working through until I hit the problem. As part of that process, I added columns in the design, one of which I called Path, to which the designer didn't object.
However, in the code, references to methods of the static class Path (e.g. Path.GetDirectoryName()) resulted in an error - 'ColumnHeader Path does not have a method 'xxx'' or similar. Clearly, it assumed that when I referred to Path, I was referring to the column within the listview.
Furthermore, when I went to rename the column (to 'FullPath'), it renamed all references to Path in the code, eg. FullPath.GetDirectoryName()....

Adding items to RibbonDropDown at runtime

So I have a dropdown menu in a ribbon with contents that can be changed while it is being used. Outlook is also happy to let me 'add' or 'insert' items into it, as long as I do not add more than 1 item.
If I try to, I'll be told that the index is out of bounds rather than expanding the upper bounds for me.
I find that if I insert it into the collection in the designer portion of the code, it will work fine, but designer code is only run once, unless I Dispose the ribbon and re-create it.
Any ideas regarding how I can get this working
Try this. This should work for you.
RibbonDropDownItem item
= Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
item.Label = "First Name";
this.cbRecent.Items.Add(item);
Try the following directly inside the Ribbon Class:
RibbonDropDownItem item = this.Factory.CreateRibbonDropDownItem();
item.Label = "Text";
combo.Items.Add(item);
jeds, your approach doesn't work with "new". You have to use the "Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()". Otherwise, you are right and your approach works great with a RibbonGallery.
That approach also works great with a DropDown. I'm still often conflicted about which one to use...
However, other than those 2 objects (Dropdown and RibbonGallery), I believe drventure is correct. You simply have to stub out the objects ahead of time and use them as needed.
You can also use the XML Ribbon, but that creates an even bigger set of headaches (at least for my use cases).
Try using a Ribbon Gallery. I have been able to modify them during run-time with as little as
foreach (string s in list)
{
RibbonDropDownItem item = new RibbonDropDownItem();
item.Label = s;
rGallery.Items.Add(item);
}
where rGallery is a RibbonGallery.
Generally speaking, VSTO wants you to completely describe the UI elements you need one time, the very first time you're asked for them (via GetCustomUI).
I've run into similar probs before with vsto and about the only reasonable way around it I've found was to prepopulate (via the designer) all the elements you might need (so let's say 10 items in your drop down list).
Then, programmatically HIDE or SHOW those items and update their captions and other properties as necessary while your addin runs.
That way, you never have to dynamically add or remove anything.

Argument exception occurring on form.ShowDialog() when I change the sortorder of the form's data grid

Ok, I'm utterly confused by this situation, so bear with me.
In my application, if you click on a button I have an editor form open
dgEditor = new fmDataGridFieldEditor();
dgEditor.ShowDialog();
This works fine, and my form shows up and operates correctly. The form has a data grid in it with some specified fields. Now, if I then change data in one of the columns of the datagrid (a column that is just meant for numbers) and then change the sorting order by clicking on the column header, my form crashes. with an ArgumentException error that says "Object must be of type Int32" on the dgEditor.ShowDialog(); line.
I don't understand what is going on or even how to start debugging this. This doesn't happen when I modify existing rows, or if the rows I enter are already sorted (e.g. 0,1,2 is fine but 0,1, 0 causes the crash).
Furthermore, I have visual studio 2010 setup to break on all exceptions, not just unhandled ones, but I'm getting an exception in the same place.
Finally, I tied the data grid's ColumnSortModeChanged event to show a message box, but even when the sorts don't crash the form, the message box doesn't show.
I'm at a loss on how to proceed with this.
The debugger shows the last line of code that you wrote. Which is the ShowDialog() call. If you look at Debug + Windows + Call stack then you see the methods in the .NET framework that are involved. Scroll the window up if necessary to see them all. DataGridView has a lot of built-in functionality, the source code isn't readily available although you can get it from the Reference Source. Not that this will help much, there's rather a lot of it.
Clearly there's some invalid data in one or more rows. Looks like a leading space, only guessing here without sitting in front of your machine. Implement the CellValidating event so the user cannot enter an improperly formatted number.
I just had this happen to me in VB. What I discovered was that when I copied the values from a textbox into the grid I didn't do a cast to int. Being VB I assumed it would cast implicitly, but the cell value is, I guess, an object, so it happily took a string. Everything looked right and worked right, until I happened to sort on that column. Maybe this will help someone else.
ShowDialog will throw an error if you are trying to create a PictureBox dynamically on the TableLayoutPanel. It does not allow two PictureBox elements to add a similar index on the Table or you could have a error if your are using MemorySTream and not closing it properly.

Code thinks Datagrid footer textbox is empty

I am working on an .net (C#) web application.
Recently a defect came my way that stated that when two users were logged into the application at the same time they both could not update values without one refreshing the page. When I looked into the issue I discovered that the author of the code has used static datasets. I changed the datasets to not be static and everything works great.
However, This issue spans many pages in the application and I must fix it everywhere. On some of these pages the application uses datasets to bind data to datagrids. The datagrids are populated with the information in the dataset and the footer contains some textboxes and an add button to add extra rows. Here is where the problem starts:
When the page was using static datasets and the user attempted to add a row through the interface everything worked fine. However, when I changed it to use datasets that were not static (they are loaded every time the page loads) and the user attempts to add a row, the code thinks that the textbox is empty (discovered when debugging even though I can see the text that I entered) and empty field validation fails and a message is displayed.
Can someone please tell me why on Earth this is happening? Why does it see the text when the dataset is static (the dataset NEVER populates the foot row) and not see the text when it is not static? Some insight would be awesome!
Thanks in advance!
Turns out there was an issue with how the grid was binding.
The binding was occurring when the datasets were being instantiated. This means that every time the page loads the binding was occurring again and causing the text to be blank. This is weird considering the item command looked like it was executing before the page load, but the datagrid didnt like it anyway. I moved where the binding was occurring so that it only happens once when the page loads (and of course it happens when something is added to the grid).
This seemed to do the trick!

Categories