WPF datagrid empty row at bottom - wpf

I bind my datagrid using
//fill datagrid
public DataTable GameData
{
get
{
DataSet ds = new DataSet();
FileStream fs = new FileStream(IMDB.WebPage.Class.Config.XMLPath,
FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs, Encoding.Default);
ds.ReadXml(reader);
fs.Close();
DataTable temp = ds.Tables[0];
return ds.Tables[0];
}
}
For some reason I get an empty row at the bottom. And sometimes after clicking on some buttons and checkboxes in the grid, more empty rows are added.
Why is this? And how do I block this?

Sounds like you probably have CanUserAddRows set to true for the DataGrid. Just add
CanUserAddRows="false"
to the XAML.

It also works with the attribute:
IsReadOnly="true"

If your backing collection that implements IEditableCollectionView returns true from its CanAddNew method, the DataGrid will assume that is what you want to do.
There's a good overview here:Overview of the editing features in the WPF DataGrid

If you're creating DataGrid on-the-fly through Source Code...
DataGrid grid = new DataGrid();
grid.CanUserAddRows = false;
//...
grid.AutoGenerateColumns = false;
grid.Margin = new Thickness(10,20,10,10);
grid.VerticalAlignment = VerticalAlignment.Top;
grid.ItemsSource = //... and so on

Though the OP was asking how to REMOVE the empty row, the title isn't specific, and this article appeared in my search while trying to figure out how to ADD the empty row. I found that, for the empty row to appear, it not only needs to have CanUserAddRows="True" but the ItemsSource needs to have a default constructor public MyClass () { }.

Related

WPF edit autogenerated column header text

I'm using a WPF DataGrid to display DataTable's.
I need to be able to edit this bound DataTables (Two-Way Binding).
I'm using the DataGrid as followed:
<DataGrid SelectionUnit="CellOrRowHeader" IsReadOnly="False" AutoGenerateColumns="True" ItemsSource="{Binding Path=SelectedItem.BindableContent, FallbackValue={x:Null}}" />
The Problem I have, the user can't edit the ColumnHeader's like cell content or rows.
The Screenshot below illustrates that porblem. The only thing I can do is sort the columns.
Is there a way to edit the column headers too, for example when the user clicks twice, or presses F2.
Maybe some Style' or a HeaderTemplate will do the job? I have already tried some styles and control templates I've found around the internet, but without any success.
EDIT:
I managed to display the column headers in a TextBox (and not in a TextBlock) within the AutogeneratingTextcolumn event handler:
private void _editor_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
// First: create and add the data template to the parent control
DataTemplate dt = new DataTemplate(typeof(TextBox));
e.Column.HeaderTemplate = dt;
// Second: create and add the text box to the data template
FrameworkElementFactory txtElement =
new FrameworkElementFactory(typeof(TextBox));
dt.VisualTree = txtElement;
// Create binding
Binding bind = new Binding();
bind.Path = new PropertyPath("Text");
bind.Mode = BindingMode.TwoWay;
// Third: set the binding in the text box
txtElement.SetBinding(TextBox.TextProperty, bind);
txtElement.SetValue(TextBox.TextProperty, e.Column.Header);
}
But I couldn't manage to set the binding correctly, if i edit the Text in the TextBoxes, it does not change the text in the Column.Header-Property (which is auto-generated by a binding to a DataTable like explained above).
You forgot to set the source of your binding and you mustn't set the value after the registration of the binding. The correct code would be the following:
private void asdf_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataTemplate dt = new DataTemplate(typeof(TextBox));
e.Column.HeaderTemplate = dt;
FrameworkElementFactory txtElement =
new FrameworkElementFactory(typeof(TextBox));
dt.VisualTree = txtElement;
Binding bind = new Binding();
bind.Path = new PropertyPath("Header");
bind.Mode = BindingMode.TwoWay;
// set source here
bind.Source = e.Column;
txtElement.SetBinding(TextBox.TextProperty, bind);
// You mustn't set the value here, otherwise the binding doesn't work
// txtElement.SetValue(TextBox.TextProperty, e.Column.Header);
}
Additionally you must change the binding property to Header, because you are adding the binding to the text property of the TextBox.

TextBox inside a listview cell

I have a listview that I would like to add a textbox inside each gridview column cell so I can type data into it and then fetch that data.
I'm creating a datatemplate and passing it to a cell template for the GridViewColumn but when I look at the listview I can't add anything to the cell. It doesn't look like the textbox was even created.
GridViewColumn conceptColumn = new GridViewColumn();
conceptColumn.Header = conceptName;
conceptColumn.CellTemplate = this.GetDataTemplate();
this.TestModeler.Columns.Add(conceptColumn);
conceptColumn.DisplayMemberBinding = new Binding(conceptName);
private DataTemplate GetDataTemplate()
{
DataTemplate dt = new DataTemplate(typeof(TextBox));
FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBox));
dt.VisualTree = txtElement;
Binding bind = new Binding();
bind.Path = new PropertyPath("Text");
bind.Mode = BindingMode.TwoWay;
txtElement.SetBinding(TextBox.TextProperty, bind);
txtElement.SetValue(TextBox.TextProperty, "test");
return dt;
}
Please take a look at the ListView Class page at MSDN where you can find a XAML example and plenty of link on how to do various things with a WPF ListView.
Of particular interest to you, please take a look at the How to: Use Templates to Style a ListView That Uses GridView page there which explains what you are trying to do (but in XAML) with examples.
MSDN should always be your first place to look as it is full of information just waiting to be read.

How to force DataGrid to rebuild VisualTree for its columns

I have WPF form with DataGrid. New columns can be added to the datagrid manually by user via button. This is the code to add new column:
private void ColumnAdornerAddButton_MouseDown(object sender, MouseButtonEventArgs e)
{
DataGridTextAdornerColumn column = new DataGridTextAdornerColumn();
column.Header = "New column";
column.HeaderStyle = (Style)FindResource("columnHeader");
column.AdornerTemplate = (DataTemplate)FindResource("columnAdorner");
Binding binding = new Binding("Data");
binding.Mode = BindingMode.TwoWay;
column.Binding = binding;
grid.Columns.Insert(grid.Columns.Count - 1, column);
//Add adorner
DataGridColumnHeader header = GetColumnHeaderFromColumn(column);
AddAdorner(header, column.AdornerTemplate, column.IsReadOnly);
}
private DataGridColumnHeader GetColumnHeaderFromColumn(DataGridColumn column)
{
// dataGrid is the name of your DataGrid. In this case Name="dataGrid"
List<DataGridColumnHeader> columnHeaders = GetVisualChildCollection<DataGridColumnHeader>(grid);
foreach (DataGridColumnHeader columnHeader in columnHeaders)
{
if (columnHeader.Column == column)
{
return columnHeader;
}
}
return null;
}
The problem is that after I have added the column to the grid its header is not yet generated and it is not present in visual tree. Thus I cant get header for the new column and apply adorner to it.
I have tried to call ApplyTemplate recursively on visual tree of the grid without any luck.
Is there any way to force grid to generate DataGridColumnHeader for the new column in code?
Thank you in advance.
Hi after adding columns to datagrid call the method UpdateLayOut() of DataGrid.
datagrid.UpdateLayout();
I hope this will help.
I just want to enhance the solution,
The method
datagrid.Items.Refresh();
will helps to recreate the view (Datagrid).
thus you can see the updated value in datagrid

Can I programmatically add a row to a WPF datagrid?

I just want to add a new row, I have my datasource in objects in which I need to do some processing. i need something like below for wpf datagrid...
DataRow row = dataTable.NewRow();
foreach (NavItem item in record.Items)
{
row[item.FieldNo.ToString()] = item.RecordValue;
}
dataTable.Rows.Add(row);
You should be using an ObservableCollection<NavItem> as the datagrid source. Then simply adding a new element to your collection will add it to the datagrid.
See this MSDN article.
I do not know if it is the right solution, but I came up to something like this, in desperation:
foreach (NavField field in this.Fields)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = field.FieldNo.ToString();
//Some other logic
// Hide non active and hidden fields
if (!field.Active || !field.Show)
column.Visibility = System.Windows.Visibility.Collapsed;
grid.Columns.Add(column);
}
Then I add the datatable as itemssource:
this.dataGridLines.ItemsSource = dataTable.DefaultView;
If I set the datatable directly, it does not care about the columns from the datatable and autogenerate its own columns, don't know why..

DataTemplate in listview

I use a listview to display data like a data matrix (columns and rows). My problem is : my items are typed : MatrixCellVM.
I tried everything I found on the net to apply a DataTemplate on this items but nothing worked.
Here is the latest technique I'm using
foreach (var col in dataMatrix.Columns)
{
//create the data template
DataTemplate cellLayout = new DataTemplate();
cellLayout.DataType = typeof(MatrixCellVM);
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));
spFactory.Name = "myComboFactory";
//set up value textblock
FrameworkElementFactory cellValue = new FrameworkElementFactory(typeof(TextBlock));
cellValue.SetBinding(TextBlock.TextProperty, new Binding("Value"));
cellValue.SetValue(TextBlock.ToolTipProperty, "Value");
spFactory.AppendChild(cellValue);
//set the visual tree of the data template
cellLayout.VisualTree = spFactory;
gridView.Columns.Add(new GridViewColumn{
Header = col.Name,
DisplayMemberBinding = new Binding(string.Format("[{0}]", count)),
CellTemplate = cellLayout
});
count++;
}
One more thing, when I override ToString() in MatrixCellVM, the value is displayed (but with this technique I add a context menu or give any color to my value).
I solved the problem but have another one.
So what I did is pretty simple. DisplayMemberBinding and CellTemplate can't be put together.
So I deleted the whole c# code creating the datatemplate.
The only thing that remains is :
gridView.Columns.Add(
new GridViewColumn
{
Header = col.Name,
CellTemplate = (DataTemplate)listView.Resources["MatrixCellVMTemplate"]
});
Thus, I added in my xaml code defining the datatemplate.
My new problem is that my rows are arrays of MatrixCellVM (as it is a matrix, I don't have properties and I don't know by advance how many columns I will have).
That was the point of the DisplayMemberBinding = new Binding(string.Format("[{0}]",count)) code in my top post.
How could I reproduce this behavior in my datatemplate (what is the binding in my textblock).
If anything is not clear, let me know.
Thanks

Resources