I am using WindowsFormHost so as to be able to use a gridview in my WPF application
(started with this ,How to instantiate a Datagridview(in code behind)).
And proceeded as
public WindowsFormsHost HOST = new WindowsFormsHost();
my gridview as
System.Windows.Forms.DataGridView gridview = new System.Windows.Forms.DataGridView();
(do not forget the references, follow here- http://www.c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/)
I fill it with a datatable as
table = new System.Data.DataTable();
table.Columns.Add("Object ID");
foreach (string column in columns)
{
table.Columns.Add(column);
}
foreach (string row in rows)
{
drow = table.NewRow();
tit = row.Substring(0, row.IndexOf('$'));
drow[0] = tit.IndexOf('&') > -1 ? tit.Substring(tit.IndexOf('&') + 1) : tit;
table.Rows.Add(drow);
}
//making the native control known to the WPF application
HOST.Child = gridview;
//Displaying the column headers of the listbox(assigned above).
gridview.DataSource = table.DefaultView;
However when I add the gridview to my WPF window as
this.Children.Add(gridview); //error at this line
I get an error saying
cannot convert from 'System.Windows.Forms.DataGridView' to 'System.Windows.UIElement'
Why so ?
I mean what may I do to rectify this ?
DataGridView is a WinForms control. So you can't directly add that to WPF Control's child. Instead add the WindowsFormsHost instance as follows:
RootGrid.Children.Add(HOST);
Is this question and current answer serious??? Why on earth would you want to import the WindowsForms dll and add a CPU intensive WindowsFormsHost element to your WPF application just so that you can use a DataGridView control? You'll find that it's much better just using the equivalent WPF control: DataGrid
You can find out all about the DataGrid class by viewing the DataGrid class page on MSDN. This page has a detailed description and code examples to help you to use the control. Furthermore, the WPF DataGrid Practical Examples page on CodeProject has detailed examples on how to do just about anything that one might want to do with the DataGrid control.
Guys I am posting what did the trick for me for future visitors.
Instead of adding the gridview I added the HOST to the window and it worked.
this.Children.Add(HOST);
SOLVED HERE :Using windows forms controls on a WPF page
Related
I'm trying to export a DataTable to Excel using a programmatically created DevExpress GridControl. The code I have exports the GridView columns, but without any populated data rows. Here is the sample code (tblErrors is a DataTable parameter passed to the method):
GridControl control = new GridControl();
GridView view = new GridView();
control.ViewCollection.Add(view);
control.MainView = view;
foreach (DataColumn tCol in tblErrors.Columns)
{
GridColumn gCol = new GridColumn();
gCol.Name = "col" + tCol.ColumnName;
gCol.FieldName = tCol.ColumnName;
gCol.UnboundType = DevExpress.Data.UnboundColumnType.Bound;
view.Columns.Add(gCol);
gCol.Visible = true;
}
control.DataSource = tblErrors;
control.Visible = true;
view.ExportToXlsx(String.Format(#"{0}\{1} Error Report.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), DateTime.Today.ToString("yyyy-MM-dd")));
Any ideas as to what I'm missing?
At the time you're calling view.ExportToXlsx this view (and the whole GridControl) is not finally initialized and loaded the data.
You can check it very simply - just set breakpoint on view.ExportToXlsx call and you can see that in this moment view.RowCount == 0, so nothing to export yet.
In fact, this "final initialization" will occur when control became visible. But in your code you're not adding control to the Controls collection of Form or UserControl, so in fact it will never be visible, and has no rows.
If it is intended behaviour and you want just to export data from your dataTable to Excel using GridControl as some "adaptor" and without showing this GridControl on form - you can make some trick like
control.DataSource = tblErrors;
Controls.Add(control);
control.ForceInitialize();
view.ExportToXlsx(....
Control.Remove(control);
Note it may cause some "blinking" of control if your data amount in tblErrors is large because control is actually being added to form, then force initialized, exported data and then control being removed from form.
As for me - it is not the best approach to export data from dataTable to Excel. Consider using instead some libraries for direct writing to Excel files, fro example, NPOI.
UPDATE
After some thoughts - you can prevent "blinking" I've mentioned just by setting control.Visible = false; before adding control to form. But this approach using GridControl is still not the best from my point of view.
I am developing a windows application in C#,I have a DataGridView,currently showing values of a datasource. I want to add a column in which every row will have a table.Now Is there any way to do that dynamically? because the table structure will vary in each row.
thanks in advance.
Edit :- I mean, I want to insert a table in a cell, dynamically.
As u can see Entity Details column showing this when I tried to add TableLayoutPanel,in each row dynamically.
1). Try out MSDN's Mark Ridout's TreeGridView and read his article on using it.
Also see if this CodeProject DataGridView with Hierarchical Data Binding that uses Mark Ridout's TreeGridView is useful.
2). If free controls dont work out have a look at 3rd parties (I am not affiliated with these co's):
Devexpress XtraGrid
Telerik Gridview
Infragistics Grid
VIBlend DataGridView for WinForms
Janus Grid
xceed's Grid
3). I'm pretty sure adding a TablePanelLayout into a Grids cell is not what you want, here is the code so you can see how dodgy it is for yourself:
DataTable dt = new DataTable();
dt.Columns.Add("name");
for (int j = 0; j < 10; j++)
{
dt.Rows.Add("");
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 200;
//add tableLayoutPanel1 into the control collection of the DataGridView
this.dataGridView1.Controls.Add(tableLayoutPanel1);
//resize the row
this.dataGridView1.Rows[1].Height = 100;
//set its location and size to fit the cell
tableLayoutPanel1.Location = this.dataGridView1.GetCellDisplayRectangle(0,1, true).Location;
tableLayoutPanel1.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
I don't think this is (easily) possible with the controls provided out-of-the-box by Microsoft.
If you have the budget, perhaps check out Telerik's controls.
http://www.telerik.com/products/winforms/gridview.aspx
They have a gridview control as part of their .NET WinForm controls that looks like it will do what you want it to.
Otherwise, is it possible to change your architecture and do an ASP.NET web-based approach to solving this problem?
Hope that helps. Good luck.
There is this article : How to: Host Controls in Windows Forms DataGridView Cells
at MSDN
The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways.
If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing.
To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell.
You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface.
So i would recommend , first Make your Table as independent control with properties or method
then use that control to be displayed for each row - when you are doing databind ,let each row set data for your TableControl.
An alternative approach could be this (assuming you are not editing in grid) - you can create a single row like usercontrol ( create a usercontrol where all textbox or label are laid around like datagridrow style them so that it looks like a single row) and then you add them in a panel with scrollbar .
Adding a column with a table within it in a DataGridView is not possible according to me..
Best alternate to your question is to create a dynamic table by self and repeat rows to show your data and within that row you can off course put a cell with a table within it.
You can add a table to the cells control collection using the RowDatabound Event. Something like this example(sorry - different to what you are doing but maybe similar to what you want, also in VB rather than C#)
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim i As Integer
Dim myTable As Table
Dim myRow As TableRow
Dim mycell As TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
myTable = New Table
myRow = New TableRow
For i = 1 To 3
mycell = New TableCell
mycell.Text = i.ToString
mycell.BorderStyle = BorderStyle.Solid
mycell.BorderWidth = 0.2
mycell.Width = 15
If i = 1 Then mycell.BackColor = Drawing.Color.Beige
If i = 2 Then mycell.BackColor = Drawing.Color.Yellow
If i = 3 Then mycell.BackColor = Drawing.Color.Green
myRow.Cells.Add(mycell)
Next
myTable.Rows.Add(myRow)
e.Row.Cells(12).Controls.Add(myTable)
End If
End Sub
i have a gridview in C# in which i hd kept 2 controls in an itemtemplate. now based on certain condition i hv to show only one.
please help me
GridView.Columns[indexOfColumnToHide].Visible = false;
I want to clear my gridview. I have 2 GridViews and Has Select Button On It. on Selecting this button that item goes into the second gridview. now the question is how should i clear the second grid view. I am trying the clear method but clear method is not found in my visual studio..
dataGridView1.DataSource = null;
or
dataGridView1.Rows.Clear();
gridview.DataSource = null;
//rebind to gridview
gridview.DataBind();
Bind the Gridview to an empty list.
Binding it to 'null' like Patrick Kafka mentioned should work - unless you have some column requirements (I'm mentioning it because I have a tendancy to plug in javascript into my gridviews and unless you specify those columns in the markup, they won't be generated and it will cause errors in the js. (This is also relevant to those getting errors after doing Columns.Clear )
In a case like that (as well as in all other cases), you can simply bind the gridview to a new instance (or empty instance) of your datasource. (Below example for a gridview bound to a datatable - it could be bound to new List<T>() as well).
grdiview1.DataSource = new DataTable();
grdiview1.DataBind();
dataGridView1.Columns.Clear(); //this clears the entire Gridview
Simply add the following c# code to clear the GridView:-
gridView.Rows.Clear();
I am doing a windows mobile application 6.1.
I dragged in a listview and went to columns and added columns to my list view. When I run the listview they do not show up.
I then tried to add them through C# code on page load with the follow code.
ColumnHeader header = new ColumnHeader();
header.Text = "gkgag";
header.Width = 100;
header.TextAlign = HorizontalAlignment.Center;
listView1.Columns.Add(header);
this does not work either. Why don't they show up?
You must use detailed view for column headers to be visible.
listView1.View = View.Details;
If that´s not the problem, column headers might be hidden behind windows systembar.