swapping of columns in a data grid view - c#

i have a c# windows form application, in all tabs inside it, i have a data grid view that display data of a punch machine(ID,IP,Port,Type,Description,Location)
there's no errors, but the problem is sometimes the ID is shown instead of the Description Field,knowing that the ID is hidden in the grid..
So what's the possible problem?
the grid view is filled from a data set through a binding source.
all the grid has the same binding source, all are the same,
and i load the data on the form load.

I have seen that before. I don't know what causes it. It seems to happen only if you hide the first column. I don't know if this is the right way to fix it but I moved the ID column away from the first column. In my case, I put it at the end of the dataGridView and I didn't see this "bug" again.
Hope it helps.

the solution was in a combo box that are binded to the same binding source of the grid..i was filled the "Selected Value" field which is wrong..so simply remove the selected value from the combo box

Related

How do I connect a binding to a (Winform) ComboBox control to get/set the selection in the control from the binding source?

I have plumbed the deeps of my google-fu to find an answer.
First, I am not simply asking how to bind a combobox to a datasource. It's a bit more than that unless I'm having a serious understanding gap.
On my Winform, I have a DataGridview on the left and on the right I have a panel with values from the selected row on the left. One of those controls on the right is the ComboBox I'm having trouble with. I have my bindingsource and dataview set up and the other controls on the right are working splendidly, except the ComboBox control.
The user, interacting with this ComboBox, should see values such as "Item ABC" and "Item EFG" and the value related to them might be 1234 and 5678. If this was a fully unbound control I'd put an object array of items in. Once I get it working, I'd load that from a different source.
But when I try to DataBindings.Add("??", dataview, "dataviewfield", ...) I can't get the proper value for "??". Runtime debug shows that "SelectedItem.Value" would be the right option, but I get "not found" type exception when I use that. I've tried "SelectedValue" as well, but that didn't work (debug show's it's null & throws no nulls allowed exception).
How can I get that value placed directly into the DataView via the Binding?
Setting the .DataSource simply loses the items and doesn't help at all.
How does one do this? Short of making the ComboBox unbound totally, setting the selectedindex directly and capturing the value when the selected index changes changes - Just seems so clunky to have to do that.
-old programmer
Further Notes: I edited to clarify the placement of the ComboBox.
I have made progress (naturally, only after asking a question does a new avenue pop into my head). I got to thinking I might need a custom binding adapter so started googling that. I found some samples doing what I want.
The foremost problem was I was not using assigning a datasource on the ComboBox, I was simply adding items. When I created a two column dataset and a few rows (could have been anything I suppose) and set that and the two field names as the ComboBox's displaymember and valuemember did the SelectedValue start showing a value (instead of null all the time).
I think that was the problem. The remaining issue is getting the left hand side to re-display/refresh after the change.
When loading the datasource for the DataGridView and the bindingsource, try making them share the same list:
BindingSource1.Datasource = dataset
DataGridView.Datasource = BindingSource1
This should mean that any changes to the data in the bindingsource will also update the DataGridView.
To edit the selected object then just handle the SelectionChanged event and set the BindingSource1.Position to the index of the selected object in the Datasource.

How does one get rid of this design-time row in the DataGridView row at run-time?

I'm using a DataGridView in a WinForm for the very first time, and have run into an odd problem.
I'm trying to populate the DataGridView control from the database. I'm not binding it to a dataset or anything, but retrieving a set of rows from a Sqlite query. I am then populating the rows programmatically from the data retrieval result. The grid is set up like this (as it appears in the designer):
What happens at runtime is fine, except for what appears to be a "phantom" row, which shows up in the grid as it appears in the designer, as the last row (blank or unpopulated), when I finish loading the grid. I've tried clearing the rows:
DataGridView.Rows.Clear();
But this doesn't clear anything. Trying to remove or make this row invisible fails with an exception because it's apparently uncommitted.
How do I get rid of the thing?
After entering the question in its entirety, I tried one last time to see if there was something that stood out. So I examined the properties of the DataGridView in close detail and wondered about some of the properties whose function I wasn't sure about.
The answer turned out to be the property AllowUserToAddRows. The "uncommitted row", as the exception designated it, is the place where the user would add the information for a new row that is to be inserted! Since this implementation of the grid is intended to be for editing of existing rows only, setting this property to False removed the apparently "bogus" row, and solved my problem.
And incidentally removed the row from the designer.

How to change the value of a Text Box depending on the currently selected row within a Data Grid?

Hi I'm hoping someone can help,
I have a Data Grid that has X amount of rows and I have a Text Box at the bottom of my form called 'Notes', I want the user to be able to select a row then enter a note within the Text Box and store this so when the user selects a different row the Text Box will be blank available for notes to be added for the new row. Then if the user selects a row that they have added notes to the Text Box will contain those notes.
I hope that makes sense, any ideas on how to approach this would be really appreciated.
Thanks,
Ryan
Thanks to GuidoG I have solved this, just in case anyone else has the same issue what I did was add a data binding to the grid then set the parameters. "Text" is just the property name, MyDataTable is the data source for my grid and "Notes" is the column name.
MyGrid.DataBindings.Add(new Binding("Text", MyDataTable, "Notes"));

listview box not displaying data in details view

I googled this question in many forms but couldn't find an answer applicable to me also..
I have a list view box in windows forms application and in that I add some items in runtime like the code I shown below:
lvinfo.Items.Add("Script executed successfully in " + dbname + " database");
The same code I am using several times for updates of execution and this works fine when I give the view as list or something else...
And also I am not clearing these items at any point of time in my code..When I googled I could find the answers related to clearing items..
My problem is when I set view property as Details, the text/content in the list view box is not displayed..or it is hidden..
EDIT:
I tried
columnHeader1.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
Any response would be really appreciated..
I guess it is because you haven't add any columns to your list view. In details view you should add at least one column to display your item.Try this before adding your items:
listView1.Columns.Add("myColumnHeader");
Have you created column headers for your ListView as the documentation states:
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.
and
Although columns are only displayed in the details view, subitems
without column headers will not display in either the details view or
the tile view.
Theres also an example further down the page for setting up column headers

c# datagridview help

I have a form showing a gridview. The data is bound dynamically, depending on several options that the user clicks on. The populating data works fine (bound from dynamically created datasets). The problem lies, when I want to switch between displaying data whereby the tables/datasets contain different layouts. The gridview keeps showing the previous layout.
How would I unbind all data to the gridview, so that I can then display the brand new table?
Thanks.
Set datasource = null ?

Categories

Resources