Style Data in a gridview which dynamically generates the columns - wpf

I have built a dynamic gridview using the following code
grdVariants.Columns.Clear();
int i = 0;
foreach (DataColumn column in options.Columns)
{
grdVariants.Columns.Add(new GridViewColumn
{
Header = column.ColumnName,
DisplayMemberBinding = new Binding(string.Format("[{0}]", i++))
});
}
This will dynamically generate my columns at runtime, I then bind the data using
lstVariantsGrid.DataContext = options;
lstVariantsGrid.Items.Refresh();
This all works great and shows the data in the correct columns etc, the only issue i have is that I can't style the rows like I would in xaml as it is all an unknown quantity until runtime. Can anyone offer some advice on how I might go about doing this?
One of the biggest problems I have is that one of the columns needs to display the image rather than just the path which it currently shows, as well as fiddling with fonts and colors etc.
Thanks for your time.

Use a datatemplate which you can create in xaml and load in code behind and then set to the CellTemplate property of the GridViewColumn.

Related

Binding on a row of a list

I'm searching a way for making a binding of a datagrid colomn on a row of a list
looking for something like that in pseudo code:
DataGridTextColumn colBuild = new DataGridTextColumn
{
Header = "Custom Column Name",
Binding = new Binding("myList.elementAt(0)")
};
I known this can looks strange but I'm looking for adding a variable number of custom columns on a datagrid in addition to fixed ones. And I don't know how to get data filled in those columns.
Can someone help me?

CheckBox in Header (Fixed) row of C1.Win.C1FlexGrid grid

My question is "Is there a way to use a CheckBox inside a fixed row cell in a C1.Win.C1FlexGrid?".
I have a C1FlexGrid with two Fixed rows. (It is important to mention here that I am using the C1.Win.C1FlexGrid grid and not the WPF, or SilverLight version)
The first fixed row I have is used for the headers as usual. the second one though is customized to perform some other tasks all is working fine except for one task I can't get done. I need to use a CheckBox inside one cell of the cells of the second Fixed row (just like any Boolean cell in the grid's normal rows) because I want to use this CheckBox to Check/Uncheck all check boxes in the same column.
Of course setting the column datatype to bool won't do the job for fixed rows. Setting the editor of the cell to a CheckBox won't do also as the editor won't be visible at all times but only when cell is selected. Also, based on my research, there is a CellFactory property that some threads are discussing which can be used to do this job, but CellFactory is not implemented in C1.Win.C1FlexGrid class but only in WPF, SilverLight and Phone versions of the grid.
Any ideas on how to do this?
Create a new CellStyle with a Boolean DataType and set it to any Cell that you need. Here’s the code to implement it, assuming the cell is in Row 1 and Column 1:
//Implement 2 fixed rows
c1FlexGrid1.Rows.Fixed = 2;
//create and set a new style to the reqd. cell
var cs = c1FlexGrid1.Styles.Add("Boolean");
//set DataType
cs.DataType = typeof(Boolean);
//Set any alignment
cs.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.CenterCenter;
c1FlexGrid1.SetCellStyle(1, 1, cs);
Thanks,
Richa

WPF: xceed datagrid, how to adjust column width at runtime?

I'm trying to adjust the xceed datagrid at runtime and cannot find a setting for this, anyone have any experience with it?
I am using Xceed's DataGridControl and its exactly as Matt sketched it. If your control is named dgCtrl, the code behind would look like this:
dgCtrl.Columns[0].Width = 100; // Sets the column's width to 100px.
You can also use the column names (FieldName property) to access the Columns:
dgCtrl.Columns["Address"].Width = 100;
Xceed's grid offers various options to adjust the width of the columns automatically at run-time. Refer to their online documentation for more info.
I haven't used the xceed datagrid, but does it not have a columns property like the normal one?
dataGrid.Columns[2]...
foreach (Xceed.Grid.Column column in grdInterstitialView.Columns)
{
column.Width = (int)(column.GetFittedWidth() * 1.1M);
}

WPF Datagrid read a cell value

I am trying to find out how to read the value of my WPF datagrid cells.
something along the lines of
String myString = myDataGrid.Cells[1][2].ToString();
the datagrid has been created in XAML and i have populated the datagrid with row data by using
reportGrid.Items.Add(new cbResultRow() { ... });
now I want to go back and read cell values in my datagrid.
I have seen some examples of reading data from a selected row or cell, by I don't have any selection (the user doesnt interact with the datagrid).
i have also seen code like
foreach(DataGridRow myrow in myDataGrid.Rows)
however the compiler says Rows is not a member of datagrid.
I have searched for several hours to try to find out how to do what I would have thought was a very simple thing!
please help,
Thanks,
will.
The WPF datagrid was built to bind to something like a DataTable. The majority of the time, you will modify the DataTable, and the Rows/Columns within the DataTable that is bound to the DataGrid.
The DataGrid itself is the Visual Element for the DataTable. Here is a pretty good tutorial on how to do this. To modify data within a loop would look something like this.
foreach(DataRow row in myTable.Rows)
{
row["ColumnTitle"] = 1;
}
This would simply make all the values in Column "ColumnTitle" equal to 1. To access a single cell it would look something like this.
myTable.Rows[0][0] = 1;
This would set the first cell in your DataTable to 1.
This might help someone else.
foreach (DataRowView row in dgLista.SelectedItems)
{
string text = row.Row.ItemArray[index].ToString();
}
Good luck!
Here's a summary of the solution.
Winform
Type: System.windows.Forms.DataGridView
// C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//"Column1" = column name in DataGridView
string text = row.Cells["Column1"].value.ToString();
}
WPF equivalent
Type: DataGrid
// C#
foreach (DataRowView row in dataGrid.Items)
{
string text = row.Row.ItemArray[index].ToString();
}

Why wont my column sort in a winforms .NET datagrid?

I have a WinForms .NET datagrid whose data source is a List<cLineItem> called lines. cLineItem is very simple class with properties like units (int), description (string) and unit amount (float).
In code, i populate the list of lines and then set the data source:
dataGridView1.DataSource = lines;
This correctly populates the grid, however, even though each of the columns in the grid are set to Sortable, when you click a column header, it doesnt sort the rows.
Sorting in DataGridView doesn't work by default, unless your source explicitly supports sorting. You need to wrap your data source in a SortableBindingList. You can use the files PropertyComparer.cs and SortableBindingList.cs from this zip file and use it like this:
dataGridView1.DataSource = new SortableBindingList<cLineItem>(lines);

Resources