Grid not displaying its rows - winforms

I'm using Infragistics.Win.UltraWinGrid.UltraGrid version Infragistics4.Win.UltraWinGrid.v13.1
and I have some problem getting this code running properly.
// creating the dataSource object which is a wrapper for a System.Data.DataTable
dataSource.FillTheValues();
Infragistics.Win.UltraWinGrid.UltraGrid grid = new Infragistics.Win.UltraWinGrid.UltraGrid();
grid.DataSource = dataSource.Table; //dataSource.Table is a System.Data.DataTable
grid.DataBind();
grid.PerformLayout();
At this point something very strange happens (checked in debug mode):
dataSource.Table.Rows.Count is 1203
grid.Rows.Count is 1203
but in the actual grid in my form nothing is displayed and the grid appears totally empty.
The Columns in the grid perfectly macth the one in the dataSource.Table, so the binding is fine.
Additionally, I've tried to invoke grid.Update, grid.Refresh, grid.Invalidate but no result.
Do you have any idea ?

In your code above, you declare and initialize a new instance of the UltraGrid and then you fill this new instance with the datasource. This grid instance is not the same grid that you have created and put on the form surface with the designer. It is just an object filled with data but never displayed.
You could resolve this problem using the variable reference to the grid designed with the form Designer (If you haven't changed the name property in the designer, it should be named ultraGrid1)
dataSource.FillTheValues();
ultraGrid1.DataSource = dataSource.Table;

Related

Datagridview rowcount showing 0 even when there is a valid datasource

I have a dynamically created DataGridView that has a valid DataSource with one row bound to it. However, it is returning me 0 when I am doing a rowcount on the DataGridView.
dgResult.DataSource = resultDt; // a datatable containing one row
flowLayoutPanel.Controls.Add(dgResult);
int rows = dgResult.Rows.Count; // returning 0 always!
Can someone please tell me where I may be going wrong here?
I found the issue. I was displaying the grid in a tabbed page that was not selected. Unless the grid is visible, it does not raise the rowadded event (which is weird!) durnig databinding. I selected the tab page before doing the databind, and the rowcount worked.
Use this code instead:
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = resultDt;
dgResult.DataSource = bindingSource;
flowLayoutPanel.Controls.Add(dgResult);
var c = dgResult.Rows.Count;
The binding source is what's responsible for syncing your data with the control. You want to use it, rather than trying to assign the table directly to the control.

DataGridView auto size columns without showing the grid

I need to get DataGridView column sizes without showing the the grid (for printing). I'm using data binding through DataSource (using BindingSource doesn't seem to work too).
For whatever reason I get wrong column sizes. If I show the grid in a form first, everything is fine. If I try to get column sizes after setting no DataSource and executing AutoResizeColumns(), columns are too wide (didn't autosize).
How can I force grid to do it's job without showing it?
Fixed. Use CreateControl() to initialize control, binding, handle, etc.

WPF -- Displaying Image in RadGridView dynamically and through code

I have a radgridview which is bound to an object that I created, foo.
Object foo has a property 'status' that is populated from an enum.
I have many foo objects stored in a collection, which I use to bind to my radgridview.
Upon binding I get records and the grid displays fine.
I'd like to not display the 'status' enum value from my foo object within the grid. Instead, I'd like to use separate images that will depict the status more clearly (and in less space). Example, if foo's 'status' enum value == Open, I will display an open door and value == Closed would be a closed door. So the same column could have different images, but from the same property bound by foo's status enum. Hopefully that's clear enough...
I have tried overriding the content value with a bitmapimage within the RowLoaded event of the grid. I have also tried setting the data type of the grid to image, butmapimage, and image source to no avail...
Seems like this should be much easier than it is... If the image came straight from the database, it wouldn't be a problem; there's plenty of examples out there. However, doing it dynamically AND through code remain a mystery.
Thanks in advance for any assitance!
Apparently all I needed was to post the question...
I fell victim to trying way too many things at once and a soultion that should have worked perfectly didn't because I was trying a style on a cell at the same time.
In the example provided above, within the grid's RowLoaded event, one could do the following:
Get the enum value from the cell content
Create an Image control ( System.Windows.Controls.Image img = new Image(); )
Assign the image's source depending upon the enum value ( open.png / closed.png )
Set the cell's content = to the image control you just created
When the grid displays, you should see your image in the grid cell...
You also need to ensure the column's datatype is set to:
typeof( System.Windows.Controls.Image )

Update datagrid view with updated data

I've a datagridview. I read an xml file and bind the data to the gridview. I change the xml and save it in another form. So i reread the xml file and i bind it to the gridview. The datatable is getting the updated values. But the grid is not getting updated till i close and open the application again. How to do it?
Thank you.
Regards,
Raghavendra
#1
Is there a Databind() or Rebind() for DataGridView? I set the datasource like this -
dvMoviesList.DataSource = dtMovies;
If i add any new row to the dtMovies table and set the datasource again, it is getting reflected. But if i edit the values of any of the existing rows and re assign the datasource it is not getting reflected till i close and open the application again. Any ideas?
Thank you.
I think you need to place a BindingSource in between the DataGridView and DataTable.
DataGridView _dgv = new DataGridView();
BindingSource _bs = new BindingSource();
DataTable _dt = new DataTable();
.
.
.
_dgv.DataSource = _bs;
_bs.DataSource = _dt;
Now whenever _dt gets updated, the BindingSource should take care of refreshing the DataGridView and you shouldn't have to worry about resetting any DataSource property. The DataSource properties can be set in the InitializeComponent() method (the designer), the Form's constructor, or Form.Load event handler.
I am sure what I am about to suggest is not the right way but I ran into the same issue and being an ASP.NET developer mostly and not having done much work in winforms the only solution I found was to set the datasource to NULL then rebind the data.
Example:
this.dataGridView.DataSource = null;
this.dataGridView.DataSource = myDataSource;
Going to verify the solution recommened by Nasser as the solution I have just does not seem right even though it works.
All you need is to Databind your datasource just like this
ArrayList list = new ArrayList();
list = YourXMLContent.GetItems();
GridView1.datasource = list; // you can use you DataTable instead of list
GridView1.update();
Gridview1.invalidate();
its all.
Have you tried this?
dvMoviesList.Refresh();
FYI: I've noticed a difference between setting the DataSource to a DataSet vs. a DataTable. Setting it to a DataSet does not cause the grid to refresh, even when it contains only one table. However, setting it to the DataTable inside the DataSet causes the grid to refresh correctly.
By the way, I was setting the DataSource property in an intermediate BindingSource object that was associated with the DataGridView, not in the DataGridView itself. Not sure if this matters.
This may only be tangentially related to the question, since the DataSource in the question was XML, not DataSet. However, others with my issue will probably bump into this topic (as I did), so I posted this anyway.

problem with two controls having the same datasource

I am building a winforms application, i have two comboboxes which have the same datasource, the datasource is a DataTable. Now, when I select a value in one comboBox, the value of another comboBox is changed too. Is there a way make it change without affecting the other?
In that type of scenario, you can create two different binding sources, one bound to each of your combo boxes. If you set the DataSource property of each of the binding data sources to your DataTable, then your combo boxes will work independently, while still showing the same data.
The initialisation would be something like:
// Initialization of the binding sources(assuming dataTable is a populated DataTable)
bindingSource.DataSource = dataTable;
bindingSource2.DataSource = dataTable;
The WinForms binding system is detecting that both comboboxes are hooked up to the same DataSource and is (helpfully) syncing changes across the two.
To avoid this you need to ensure each combobox has a distinct DataSource.
One way is to use the appropriate non-visual component from the Toolbox (BindingSource).
Another, if you're setting up your bindings with code, is to use BindingList. Note that there is one trap with BindingList - it can act as a wrapper:
[The] BindingList constructor creates a WRAPPER collection around the original list. It doesn't create a new list containing the same elements. (I've never seen this documented, but have verified with Reflector).
-- http://www.nichesoftware.co.nz/blog/200809/databinding-lists
Instead of:
editDebitAccount.DataSource = accountsList;
editCreditAccount.DataSource = accountsList;
use this:
editDebitAccount.DataSource = new BindingList(accountsList);
editCreditAccount.DataSource = new BindingList(accountsList);

Resources