c# DataSet To DataGrid - c#

I would like to convert my dataset which contains one table into a datagrid in order to get the width of each column to add many groups title with the correct width just above.
I've tried " mydatagrid.ItemsSource = mydataset.Table[0].defaultview;" and it works properly
except this instruction doesn't fill any columns in my datagrid so i can't get any width of any columns.
If anyone have an idea, thanks a lot.

Have you set AutoGenerateColumns = true?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx
Your question sounds a bit strange. You DO know, that DataGrid is a web control, yes?

First of all, there are two DataGrid controls: one in System.Windows.Forms namespace for Windows Forms and the other in System.Web.UI.WebControls for Web.
In either case, DataGrid is a control that shows a data from a data source in a grid. In order to show the data, you have to bind it to a control.
This is quote from DataGrid article:
"To display a table in the System.Windows.Forms.DataGrid at run time, use the SetDataBinding method to set the DataSource and DataMember properties to a valid data source."
dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");

So i'm back with a solution.
My columns were empty cause my code was define before the event "Loaded" happen so now everything is perfect.
Thanks for precision about the datagrid.

Related

Bound DataGridView with combo box [duplicate]

I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?
The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.
As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.
You can pre-create your columns in the designer. If the name of the column matches the name of the property the column will end up bound to, the databinding will take care of the DGV population for you as before.

Define the column control of a database-driven DataGridView

I use a DataGridView to view and edit data in my PostgreSQL database, which works fine. I want to make it a little more user-friendly by choosing the right Control to input data. Specifically, I want to create a ComboBox in a column to let the user search and select a value fast.
To do so, I think I need the EditingControlShowing event to fill the combobox. However, the column I get is of type DataGridViewTextBoxColumn, so the corresponding e.Control is a TextBox instead of a ComboBox.
I never initialise those columns, because they come from views in the database. How can I cast/initialise the column to DataGridViewComboBoxColumn ?
This is how I populate my DataGridView:
dgView.DataSource = getView();
getView() returns a DataTable as can be gotten from NpgsqlDataAdaper.
Since you want to use something other than a DataGridViewTextBoxColumn you need to set AutoGenerateColumns to false and actually define the columns yourself. When defining them, choose a DataGridViewComboBox column for the appropriate fields.
This can be done in code, or in the designer, and based off your question I think you should be able to simply do it through the designer.
You should do it in 2 steps.
1. First of all ( as mentioned by Michael) you should set AutoGenerateColumns to false and then create the required columns using the datagrid view columns option (in properties)
2. Then you should loop through your records and assign appropriate values to appropriate columns.
In case you have hundreds of records that you are showing in grid view, than you should consider using server side paging.

How to show the Small Pointer in WPF DataGrid as in WinForms DataGridView

I am working on WPF and DataGrids, I am really new on this. And I need the WPF DataGrid to show in its row headers the small pointer as in the WindowsForms DataGridView does (it looks like a small triangle pointing to right).
I have tried with this property:
dataGrid1.HeadersVisibility = DataGridHeadersVisibility.All;
But doing as above the dataGrid got some very small headers which seems to be just to select the entire row.
I have done some research about it but I have found nothing.
Does anyone know if this is even possible?
Thank you in advance.
2 things --
1) specify a SortMemberPath -- this will get you the default style 2) style the grid's header template
After some research I have found two links which allow me to do what I was trying to accomplish:
WPF DataGrid Row indicators
or this one:
Changing the style of the selected row of the DataGrid control

Change WPF DataGrid Cell Font based on comparison of cell values

I am new to using the WPF DataGrid and I'm kind of lost. What I have is a DataTable with numeric values to which I bind the DataGrid via a DataView. What I want to do is compare the values of each column, and make the cell that has the bigger value per column, bold. (e.g. Who wins in each category)
There's no ObservableCollection, no fancy stuff. I'm just adding data to a DataTable the "manual" way, getting the values cell-by-cell from an SQLite database DataTable response. Even the columns are created programatically, and not via XAML. I have almost no experience in XAML, so don't assume I've worked with Triggers or anything.
How would I go about doing something like this?
Start from small examples. You can create some test data, with which you can try data binding technique. Try to style some parts of DataGrid: change colors of Foreground, add some Border with BorderBrush. Explore WPF yourself - it is hard only for the first view.
Here is some how to:
Use converters in WPF
Use binding
Use styles and templates for DataGrid elements
Use MVVM

When i change the datasource on My datagrid the columns dont autofill unless i order a column

I have a DataGrid on a Win Form that display perfectly when I initially set the datasource. The AutoSizeColumnsMode is "DisplayCells". When I change the datasource during runtime the rows do not autosize unless I re-order a column.
Does anyone know how to fix this? Also note that the DataGrid is on a different Tab than the button that calls the update.
The only code that I ever use to populate the grid is:
dgUnPrinted.DataSource = TableName;
dgUnPrinted.Refresh();
try http://www.codeproject.com/KB/miscctrl/AutoResizeDataGrid.aspx
or
http://www.hanselman.com/blog/HowDoIAutomaticallySizeAutosizeColumnsInAWinFormsDataGrid.aspx
or use DataGridView then you can play with gdv.Columns[0].AutoSizeMode property.
Did you try the AutoResizeColumns method?
http://msdn.microsoft.com/en-us/library/ms158595%28v=VS.90%29.aspx
After some thinking I decided to sort a column through code. This filled out all the cells. I am not sure why this is needed or why it works when everything else does not work but at least i have a fix.
dgUnPrinted.Sort(dgUnPrinted.Columns[0], ListSortDirection.Ascending);

Categories