Center datagrid combobox text - c#

How can I center the text of ComboBox columns in a DataGridView?
The DataGrid is dynamically updated with columns of different types:
foreach (Fields field in fields)
{
if (field.group.Count > 1)
{
DataGridComboBoxColumn column = new DataGridComboBoxColumn();
column.Header = field.name;
column.ItemsSource = field.group;
column.SelectedValueBinding = new Binding(field.name)
{
};
dgwDataMain.Columns.Add(column);
}
else
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = field.name;
column.Binding = new Binding(field.name)
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
NotifyOnSourceUpdated = true,
NotifyOnTargetUpdated = true
};
dgwDataMain.Columns.Add(column);
}
}

Solution for WinForm DataGridView:
In your code where you creating a ComboBox columns add next row:
column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Related

How to get value of a programmatically written combobox in a datagrid in wpf?

To follow my previous post here => Binding SelectedItem of ComboBox in DataGrid with different type
I have now a datagrid containing 2 columns, one with a text, the other with a combobox (in a datatemplate, written thru the C# code, not the Xaml).
After having done some choice on the combobox, I now would like to parse the result but the value of the cell containing my combobox stay empty :
foreach(DataRowView row in Datagrid1.Items)
{
var firstColumNresult = row.Row.ItemArray[0];// Return correctly a string
var myrow = row.Row.ItemArray[1];// always empty...
}
The result is that I cant get the values of my (previously generated) combobox.
I suppose one binding must missed somewhere...
This is the combobox creation code :
DataTable tableForDG = new DataTable();
tableForDG.Columns.Add(new DataColumn { ColumnName = "Name", Caption = "Name" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "Attachment", Caption = "Attachment" }); // this column will be replaced
tableForDG.Columns.Add(new DataColumn { ColumnName = "AttachmentValue", Caption = "AttachmentValue" });
tableForDG.Columns.Add(new DataColumn { ColumnName = "DisplayCombo", Caption = "DisplayCombo", DataType=bool });
// Populate dataview
DataView myDataview = new DataView(tableForDG);
foreach (var value in listResults)// a list of string
{
DataRowView drv = myDataview.AddNew();
drv["Name"] = value.Name;
drv["Attachment"] = value.Name;// this column will be replaced...
drv["DisplayCombo"] = true;// but it can be false on my code...
}
var DG = myDataview;//
Datagrid1.ItemsSource = DG;
Datagrid1.AutoGenerateColumns = true;
Datagrid1.Items.Refresh();
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Header = "Attachment";
var newCombobox = new FrameworkElementFactory(typeof(ComboBox));
newCombobox.SetValue(ComboBox.NameProperty, "myCBB");
Binding enableBinding = new Binding();
newCombobox.SetValue(ComboBox.IsEnabledProperty, new Binding("DisplayCombo"));
newCombobox.SetValue(ComboBox.SelectedValueProperty, new Binding("AttachmentValue"));
List<string> listUnitAlreadyAttached = new List<string>();
// fill the list...
enableBinding.Source = listUnitAlreadyAttached;
newCombobox.SetBinding(ComboBox.ItemsSourceProperty, enableBinding);
var dataTplT = new DataTemplate();
dataTplT.VisualTree = newCombobox;
dgTemplateColumn.CellTemplate = dataTplT;
Datagrid1.Columns[1] = dgTemplateColumn;
Any idea/advice ?
You should explicitely specify the binding mode and update trigger of your binding. Also use SetBinding instead of SetValue:
var valueBinding = new Binding("AttachmentValue")
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
newCombobox.SetBinding(ComboBox.SelectedValueProperty, valueBinding);
This should enable you to get the selected value into your row data. It might not update in the displayed datagrid value for the AttachmentValue column.

DataGridView bound to BindingList shows too many columns

I've got a class with two public properties, Name and Text. I want to bind a DataGridView to a list of these objects, so I loaded them into a BindingList<>. I only want to show the Name property in the DataGridView, but I'm getting both columns. What am I doing wrong?
private void fileOpenTextBox1_FileSelected(object sender, string e)
{
m_definitions = new BindingList<TagSimDefinition>(TagSimDefinition.Read(e));
dgvTagNames.AutoGenerateColumns = false;
dgvTagNames.Columns.Clear();
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colTagName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name = "colTagName",
HeaderText = "Tag Name",
DataPropertyName = "Name"
};
dgvTagNames.Columns.Add(colTagName);
dgvTagNames.DataSource = m_definitions;
}
#Robert you are setting,
dgvTagNames.DataSource = m_definitions;
for that it is showing both columns. If you want to get one column use it like this,
m_definitions = new BindingList<TagSimDefinition>(TagSimDefinition.Read(e));
dgvTagNames.AutoGenerateColumns = false;
dgvTagNames.Columns.Clear();
dgvTagNames.ColumnCount = 1;
dgvTagNames.Columns[0].Name = "colTagName";
dgvTagNames.Columns[0].DataPropertyName = "colTagName";
dgvTagNames.DataSource = m_definitions;
Another solution:
m_definitions = new BindingList<TagSimDefinition>(TagSimDefinition.Read(e));
dgvTagNames.Columns.Clear();
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colTagName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name = "colTagName",
HeaderText = "Tag Name",
DataPropertyName = "Name"
};
dgvTagNames.Columns.Add(colTagName);
dgvRegion.Rows.Clear();
int index = 0;
foreach (var dparam in m_definitions)
{
dgvTagNames.Rows.Add();
dgvTagNames["colTagName", index].Value = dparam.<Property1>;
dgvTagNames.Rows[index].Tag = dparam;
index++;
}
Everyone here is making things wayyyyyyy to complicated. If you have columns you don't want displayed you could just set the Column.Visible = false after the setting the datasource.
dgvTagNames.Columns[index].Visible = false;

WPF DataGridComboBoxColumn does not display text if it is not in ItemsSource

In my C# WPF application I programmatically add a ComboBoxColumn to a DataGrid:
public static DataGridComboBoxColumn getCboCol(string colName, Binding textBinding)
{
List<string> statusItemsList = new StatusStrList();
DataGridComboBoxColumn cboColumn = new DataGridComboBoxColumn();
cboColumn.Header = colName;
cboColumn.SelectedItemBinding = textBinding;
cboColumn.ItemsSource = statusItemsList;
return cboColumn;
}
If an item in the containing DataGrid contains text, which my StatusStrList doesn't contain, it won't be displayed.
Example: If my StatusStrList contains A, B, C and a DataGrid's item has X, the X won't be displayed as text in the ComboBox.
How can I fix this?
Thanks in advance,
Christian
DataGridComboBoxColumn isn't dynamic enough to do something like this but you can use DataGridTemplateColumn. Code below should achieve the functionality you need. It works by using a CellTemplate containing a TextBlock which easily displays an item that wouldn't be in the ItemsSource of the ComboBox. Going into edit mode will bring up the ComboBox that contains all of the items of the list.
DataGridTemplateColumn cboColumn = new DataGridTemplateColumn();
cboColumn.Header = colName;
//DataTemplate for CellTemplate
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory txtBlkFactory = new FrameworkElementFactory(typeof(TextBlock));
txtBlkFactory.SetValue(TextBlock.TextProperty, textBinding);
cellTemplate.VisualTree = txtBlkFactory;
cboColumn.CellTemplate = cellTemplate;
//DataTemplate for CellEditingTemplate
DataTemplate editTemplate = new DataTemplate();
FrameworkElementFactory cboFactory = new FrameworkElementFactory(typeof(ComboBox));
cboFactory.SetValue(ComboBox.TextProperty, textBinding);
cboFactory.SetValue(ComboBox.ItemsSourceProperty, statusItemsList);
cboFactory.SetValue(ComboBox.IsEditableProperty, true);
MouseEventHandler handler = new MouseEventHandler(delegate(object sender, MouseEventArgs args)
{
ComboBox cboBox = (ComboBox)sender;
cboBox.IsDropDownOpen = true;
});
cboFactory.AddHandler(ComboBox.MouseEnterEvent, handler);
editTemplate.VisualTree = cboFactory;
cboColumn.CellEditingTemplate = editTemplate;

How to set a DataGridView column to a DataGridViewComboBoxColumn?

Here is my code:
DataSet data = new DataSet();
data.ReadXml("data.xml");
DataGridView grid = new DataGridView();
var genreCboBoxItems = data.Tables[0].AsEnumerable().Select(genre => genre.Field<string>("genre")).Distinct().ToArray();
// TODO: Make is so the 'genre' column in grid is a combo box?
grid.DataSource = data.Tables[0];
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
*edit: genreCboBoxItems
Try this: (not tested)
var column = new DataGridViewComboBoxColumn();
column.DataSource = data.Tables[0].AsEnumerable().
Select(genre => new { genre = genre.Field<string>("genre") }).Distinct();
column.DataPropertyName = "genre";
column.DisplayMember = "genre";
column.ValueMember = "genre";
grid.DataSource = data.Tables[0];
// Instead of the below line, You could use grid.Columns["genre"].Visible = false;
grid.Columns.Remove("genre");
grid.Columns.Add(column);
This might help you cast DataGridViewColumn to DataGridViewComboBox.
First create DataGridViewComboBoxColumn using designer with proper name. Then say you have a list of String list and other string values to bind to that datagridview then use this code:
Below code will bind a list to two DataGridViewTextBoxCell and a DataGridViewComboBoxCell. Note AllCriterias is a list with two string values and a list of string. DGVEligibilityCriteria is the grid name.
for (int i = 0; i < AllCriterias.Count; i++)
{
DataGridViewTextBoxCell Cmb1 = (DataGridViewTextBoxCell)DGVEligibilityCriteria.Rows[i].Cells[0];
Cmb1.Value = AllCriterias[i].Name;
DataGridViewTextBoxCell Cmb2 = (DataGridViewTextBoxCell)DGVEligibilityCriteria.Rows[i].Cells[1];
Cmb2.Value = AllCriterias[i].Type;
DataGridViewComboBoxCell Cmb = (DataGridViewComboBoxCell)DGVEligibilityCriteria.Rows[i].Cells[2];
foreach (var filtervalue in AllCriterias[i].FilterValues)
{
Cmb.Items.Add(filtervalue);
}
}
Need to display the fist index as default by setting selectindex property.
Use this code : Here "filterValues" is the name of the DataGridViewComboBoxCell which u created in the datagridview designer.
foreach (DataGridViewRow row in DGVEligibilityCriteria.Rows)
{
row.Cells["filterValues"].Value = (row.Cells["filterValues"] as DataGridViewComboBoxCell).Items[0];
}

Create DataGridTemplateColumn Through C# Code

I have a dynamic Datagrid that I have created. I am creating each column for it through code behind. I am having troubles on a column that I want to be displayed at a textblock when not editing, but as a combobox while editing. I have an ObservableCollection of Transactions. Each Transaction has a type called "Account". Here is what I have so far:
private DataGridTemplateColumn GetAccountColumn()
{
// Create The Column
DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
accountColumn.Header = "Account";
Binding bind = new Binding("Account");
bind.Mode = BindingMode.TwoWay;
// Create the TextBlock
FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
textFactory.SetBinding(TextBlock.TextProperty, bind);
DataTemplate textTemplate = new DataTemplate();
textTemplate.VisualTree = textFactory;
// Create the ComboBox
bind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
comboFactory.SetValue(ComboBox.DataContextProperty, this.Transactions);
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
comboFactory.SetBinding(ComboBox.ItemsSourceProperty, bind);
DataTemplate comboTemplate = new DataTemplate();
comboTemplate.VisualTree = comboFactory;
// Set the Templates to the Column
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
return accountColumn;
}
The value displays in the TextBlock. However, in the combobox, I am only getting one character to display per item. For example, here is the textblock:
But when I click to edit and go into the combobox, here is what is shown:
Can someone help me out so that the items in the Combobox are displayed properly? Also, when I select something from the Combobox, the textblock isn't updated with the item I selected.
UPDATED:
Here is my column as of now. The items in the ComboBox are being displayed properly. The issue now is that when a new item is selected, the text in the TextBlock isn't updated with the new item.
private DataGridTemplateColumn GetAccountColumn()
{
// Create The Column
DataGridTemplateColumn accountColumn = new DataGridTemplateColumn();
accountColumn.Header = "Account";
Binding bind = new Binding("Account");
bind.Mode = BindingMode.OneWay;
// Create the TextBlock
FrameworkElementFactory textFactory = new FrameworkElementFactory(typeof(TextBlock));
textFactory.SetBinding(TextBlock.TextProperty, bind);
DataTemplate textTemplate = new DataTemplate();
textTemplate.VisualTree = textFactory;
// Create the ComboBox
Binding comboBind = new Binding("Account");
comboBind.Mode = BindingMode.OneWay;
FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox));
comboFactory.SetValue(ComboBox.IsTextSearchEnabledProperty, true);
comboFactory.SetValue(ComboBox.ItemsSourceProperty, this.Accounts);
comboFactory.SetBinding(ComboBox.SelectedItemProperty, comboBind);
DataTemplate comboTemplate = new DataTemplate();
comboTemplate.VisualTree = comboFactory;
// Set the Templates to the Column
accountColumn.CellTemplate = textTemplate;
accountColumn.CellEditingTemplate = comboTemplate;
return accountColumn;
}
The "Accounts" property is declared like this in my MainWindow class:
public ObservableCollection<string> Accounts { get; set; }
public MainWindow()
{
this.Types = new ObservableCollection<string>();
this.Parents = new ObservableCollection<string>();
this.Transactions = new ObservableCollection<Transaction>();
this.Accounts = new ObservableCollection<string>();
OpenDatabase();
InitializeComponent();
}
Here is my Transaction Class:
public class Transaction
{
private string date;
private string number;
private string account;
public string Date
{
get { return date; }
set { date = value; }
}
public string Number
{
get { return number; }
set { number = value; }
}
public string Account
{
get { return account; }
set { account = value; }
}
}
You bind the ItemsSource to the selected value, a string, aka char array, so every character is used as an item, the ItemsSource binding presumably should target some other collection from which the value can be chosen.
Dim newBind As Binding = New Binding("LinktoCommonOutputBus")
newBind.Mode = BindingMode.OneWay
factory1.SetValue(ComboBox.ItemsSourceProperty, dictionary)
factory1.SetValue(ComboBox.NameProperty, name)
factory1.SetValue(ComboBox.SelectedValuePathProperty, "Key")
factory1.SetValue(ComboBox.DisplayMemberPathProperty, "Value")
factory1.SetBinding(ComboBox.SelectedValueProperty, newBind)
By creating Binding you can set SelectedValue in a datagrid for WPF.

Categories

Resources