private void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
GridColorPickerEditor colorEditor = e.ActiveEditor as GridColorPickerEditor;
if (colorEditor!=null)
{
GridColorPickerElement colorPicker = colorEditor.EditorElement as GridColorPickerElement;
colorPicker.ReadOnly = true;
}
}
Please refer to the attached gif file.
Workaround:
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
if (e.EditorType == typeof(GridColorPickerEditor))
{
e.Editor = new GridColorPickerEditor();
}
}
To reproduce: - Open the Search Row sample in the demo application. - Type some text in the search textbox. - Using the context menu of the search textbox add some unicode control characters.
It would be great if multi-page printing is supported for grids with ColumnGroupsViewDefinition and HtmlViewDefinition.
Currently, when you have an object which has more than one parent, the grid falls in an invalid state and shows the child under all of the parents, however, it does not behave correctly. As this is not supported scenario, the user should be notified via exception.
When you use a custom GroupComparer for the MasterTemplate, grouping is performed successfully for the first time. However, if you remove all groups and perform grouping again for the same columns, the grid behavior is not as expected: you will notice groups mismatching or duplication. Here is a sample code snippet which incorrect behavior is illustrated on the attached gif file:
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("Reference");
dt.Columns.Add("Test Case");
dt.Columns.Add("ChBW");
dt.Columns.Add("Voltage");
dt.Columns.Add("Environmental Conditions");
dt.Columns.Add("RadioAccessTechnology");
dt.Rows.Add("4.2.1", "E-Utran FDD...", "10", "C Nominal-2", "T Nominal - V Nominal", "E-UTRAN Only");
dt.Rows.Add("5.2.1", "E-Utran FDD...", "3", "C Nominal-1", "T Nominal - V Nominal", "E-UTRAN Only");
dt.Rows.Add("14.2.1", "E-Utran FDD...", "1", "C Nominal-3", "T Nominal - V Nominal", "E-UTRAN Only");
dt.Rows.Add("2.2.1", "E-Utran FDD...", "4", "C Nominal-2", "T Nominal - V Nominal", "E-UTRAN Only");
dt.Rows.Add("7.2.1", "E-Utran FDD...", "12", "C Nominal-1", "T Nominal - V Nominal", "E-UTRAN Only");
dt.Rows.Add("2.2.1", "E-Utran FDD...", "2", "C Nominal-3", "T Nominal - V Nominal", "E-UTRAN Only");
this.radGridControlSelection.DataSource = dt;
this.radGridControlSelection.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.radGridControlSelection.MasterTemplate.GroupComparer = new GroupComparer();
}
public class GroupComparer : IComparer<Group<GridViewRowInfo>>
{
public int Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
{
if (x.Level != y.Level)
{
}
DataGroup group = x as DataGroup;
var a = x.Header;
var b = y.Header;
int valueA;
int valueB;
if (group == null)
{
group = y as DataGroup;
}
if (group != null && group.GroupDescriptor != null && group.GroupDescriptor.GroupNames.Count > 0)
{
string propertyName = group.GroupDescriptor.GroupNames.First().PropertyName;
if ((propertyName.ToUpper() == "VOLTAGE"))
{
int indexA = GetIndexContain(a.ToString().Split(new char[] { '-' })[1]);
int indexB = GetIndexContain(b.ToString().Split(new char[] { '-' })[1]);
if (indexA == indexB)
{
return 0;
}
else if (indexA < indexB)
{
return -1;
}
else
{
return 1;
}
}
else if (propertyName.ToUpper() == "RADIOACCESSTECHNOLOGY")
{
return x.Key.ToString().CompareTo(y.Key.ToString());
}
// BAND
else if (propertyName.ToUpper() == "CHBW")
{
Int32.TryParse(a, out valueA);
Int32.TryParse(b, out valueB);
//ASCENDING SELECTED
if (group.GroupDescriptor.GroupNames.First().Direction == ListSortDirection.Ascending)
{
if (valueA > valueB)
{
return 1;
}
else if (valueA < valueB)
{
return -1;
}
else
{
return 0;
}
}
//DESCENDING
else
{
if (valueA > valueB)
{
return -1;
}
else if (valueA < valueB)
{
return 1;
}
else
{
return 0;
}
}
}
}
return x.Key.ToString().CompareTo(y.Key.ToString());
}
private int GetIndexContain(string a)
{
int parsedValue;
if (int.TryParse(a, out parsedValue))
{
return 10 - parsedValue;
}
return -1;
}
}
Workaround:
public class Grid:RadGridView
{
public override string ThemeClassName
{
get
{
return typeof(RadGridView).FullName;
}
}
protected override RadGridViewElement CreateGridViewElement()
{
return new CustomGridViewElement();
}
}
public class CustomGridViewElement : RadGridViewElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadGridViewElement);
}
}
protected override MasterGridViewTemplate CreateTemplate()
{
return new CustomMasterGridViewTemplate();
}
}
public class CustomMasterGridViewTemplate : MasterGridViewTemplate
{
protected override GridViewListSource CreateListSource()
{
return new CustomGridViewListSource(this);
}
}
public class CustomGridViewListSource : GridViewListSource
{
public CustomGridViewListSource(GridViewTemplate template) : base(template)
{
}
protected override RadCollectionView<GridViewRowInfo> CreateDefaultCollectionView()
{
return new CustomGridDataView(this);
}
}
public class CustomGridDataView : GridDataView
{
public CustomGridDataView(GridViewListSource listSource) : base(listSource)
{
}
protected override GroupBuilder<GridViewRowInfo> CreateGroupBuilder()
{
return new CustomGroupBuilder(this.Indexer);
}
}
public class CustomGroupBuilder : GridGroupBuilder
{
public CustomGroupBuilder(Index<GridViewRowInfo> index) : base(index)
{
}
protected override Group<GridViewRowInfo> GetGroup(GroupCollection<GridViewRowInfo> cache,
Group<GridViewRowInfo> newGroup, Group<GridViewRowInfo> parent, object key, int level)
{
GroupDescriptor currentDescriptor = this.CollectionView.GroupDescriptors[level];
DataGroup group = (DataGroup)base.GetGroup(null, newGroup, parent, key, level);
if (group.GroupDescriptor != null && group.GroupDescriptor != currentDescriptor)
{
SetGroupDescriptor(group, null);
IGroupFactory<GridViewRowInfo> groupFactory = this.CollectionView.GroupFactory;
group = (DataGroup)groupFactory.CreateGroup(key, parent);
group.GroupBuilder = this;
}
SetGroupDescriptor(group, currentDescriptor);
return group;
}
private void SetGroupDescriptor(DataGroup dataGroup, GroupDescriptor currentDescriptor)
{
FieldInfo fi = typeof(DataGroup).GetField("groupDescriptor", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(dataGroup, currentDescriptor);
}
}
The conditional formatting collection get deleted after closing the property builder. To reproduce : Add a gridview with a datasource. Open property builder Select a column Advance -> ConditionalFormattingObjectList Add a expression, set a name Press Ok If you click the ConditionalFormattingObjectList, you can see the expression If you click close the Property Builder, the list is not saved when you open it again.
Version 2014.3.1202.40 scrolling worsened
How to reproduce:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Id2", typeof(int));
dataTable.Columns.Add("Id3", typeof(int));
dataTable.Columns.Add("Id4", typeof(int));
for (int i = 0; i < 10000; i++)
{
dataTable.Rows.Add(i, i + 10, i +20, i+30);
}
this.radGridView1.DataSource = dataTable;
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
ExpressionFormattingObject idObj = new ExpressionFormattingObject("1", "Id % 2 = 0", false);
idObj.CellBackColor = Color.SkyBlue;
idObj.CellForeColor = Color.Red;
this.radGridView1.Columns["Id"].ConditionalFormattingObjectList.Add(idObj);
ExpressionFormattingObject idObj2 = new ExpressionFormattingObject("1", "Id2 % 2 = 0", false);
idObj2.CellBackColor = Color.LightGray;
idObj2.CellForeColor = Color.Red;
this.radGridView1.Columns["Id2"].ConditionalFormattingObjectList.Add(idObj2);
ExpressionFormattingObject idObj3 = new ExpressionFormattingObject("1", "Id3 % 2 = 0", false);
idObj3.CellBackColor = Color.LightGreen;
idObj3.CellForeColor = Color.Red;
this.radGridView1.Columns["Id3"].ConditionalFormattingObjectList.Add(idObj3);
ExpressionFormattingObject idObj4 = new ExpressionFormattingObject("1", "Id4 % 2 = 0", false);
idObj4.CellBackColor = Color.LightYellow;
idObj4.CellForeColor = Color.Red;
this.radGridView1.Columns["Id4"].ConditionalFormattingObjectList.Add(idObj4);
}
}
Workaround:
Apply formatting on CellFormatting event
To reproduce:
radGridView1.EnableSorting = true;
radGridView1.AutoSizeRows = true;
radGridView1.ShowGroupPanel = false;
radGridView1.EnableGrouping = false;
radGridView1.MasterTemplate.AllowColumnChooser = false;
radGridView1.MasterTemplate.AllowAddNewRow = false;
radGridView1.MasterTemplate.AllowDeleteRow = false;
radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.MasterTemplate.AllowCellContextMenu = false;
radGridView1.MasterTemplate.AllowColumnHeaderContextMenu = false;
radGridView1.Columns.Add(new GridViewTextBoxColumn("key"));
radGridView1.Columns.Add(new GridViewTextBoxColumn("value"));
radGridView1.Columns.Add(new GridViewCheckBoxColumn { EnableHeaderCheckBox = true, });
var list = new List<KeyValuePair<string, string>>();
list.Add(new KeyValuePair<string, string>("1", "One"));
list.Add(new KeyValuePair<string, string>("2", "Two"));
radGridView1.DataSource = list;
radGridView1.CurrentRow = null;
To reproduce:
BindingList<Item> items = new BindingList<Item>();
public Form1()
{
InitializeComponent();
Item dataItem1 = new Item(1);
dataItem1.Name = "Cat";
dataItem1.GroupName = "Animal";
dataItem1.GroupSortPriority = 2;
items.Add(dataItem1);
Item dataItem2 = new Item(2);
dataItem2.Name = "Kitten";
dataItem2.GroupName = "Animal";
dataItem2.GroupSortPriority = 2;
dataItem2.ParentIndex = 1;
items.Add(dataItem2);
Item dataItem3 = new Item(3);
dataItem3.Name = "Trout";
dataItem3.GroupName = "Fish";
dataItem3.GroupSortPriority = 1;
items.Add(dataItem3);
radGridView1.Relations.AddSelfReference(radGridView1.MasterTemplate, "Index", "ParentIndex");
radGridView1.DataSource = items;
}
public class Item
{
public Item(int index)
{
Index = index;
}
public int Index { get; private set; }
public int ParentIndex { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
public int GroupSortPriority { get; set; }
}
public class GridGroupComparer : IComparer<Group<GridViewRowInfo>>
{
public GridGroupComparer()
{
}
public int Compare(
Group<GridViewRowInfo> x,
Group<GridViewRowInfo> y)
{
if (null == x.Key || null == y.Key)
return 1;
GridViewRowInfo xGridViewRowInfo = x.DefaultIfEmpty(null).First();
GridViewRowInfo yGridViewRowInfo = y.DefaultIfEmpty(null).First();
if (null == xGridViewRowInfo || null == yGridViewRowInfo)
return x.Key.GetHashCode().CompareTo(y.Key.GetHashCode());
Item xGridRowDataItem = xGridViewRowInfo.DataBoundItem as Item;
Item yGridRowDataItem = yGridViewRowInfo.DataBoundItem as Item;
if (null == xGridRowDataItem || null == yGridRowDataItem)
return x.Key.GetHashCode().CompareTo(y.Key.GetHashCode());
if (xGridRowDataItem.GroupSortPriority > yGridRowDataItem.GroupSortPriority)
return 1;
if (xGridRowDataItem.GroupSortPriority < yGridRowDataItem.GroupSortPriority)
return -1;
return 0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
GridGroupComparer groupComparer = new GridGroupComparer();
radGridView1.MasterTemplate.GroupComparer = groupComparer;
GroupDescriptor descriptor = new GroupDescriptor();
descriptor.GroupNames.Add("GroupName", ListSortDirection.Ascending);
radGridView1.GroupDescriptors.Add(descriptor);
}
If you remove the Self-referencing hierarchy, you will notice that the GroupComparer is respected.
To reproduce:
BindingList<Item> items = new BindingList<Item>();
public Form1()
{
InitializeComponent();
Item dataItem1 = new Item(1);
dataItem1.Name = "Cat";
dataItem1.GroupName = "Animal";
dataItem1.GroupSortPriority = 2;
items.Add(dataItem1);
Item dataItem2 = new Item(2);
dataItem2.Name = "Kitten";
dataItem2.GroupName = "Animal";
dataItem2.GroupSortPriority = 2;
dataItem2.ParentIndex = 1;
items.Add(dataItem2);
Item dataItem3 = new Item(3);
dataItem3.Name = "Trout";
dataItem3.GroupName = "Fish";
dataItem3.GroupSortPriority = 1;
items.Add(dataItem3);
radGridView1.Relations.AddSelfReference(radGridView1.MasterTemplate, "Index", "ParentIndex");
radGridView1.DataSource = items;
}
private void ExpandSomeGroups()
{
foreach (DataGroup group in radGridView1.Groups)
{
if (group.GroupRow.HeaderText.Contains("Animal"))
{
if (group.IsExpanded)
{
group.Collapse(false);
}
else
{
group.Expand(true);
}
}
}
}
public class Item
{
public Item(int index)
{
Index = index;
}
public int Index { get; private set; }
public int ParentIndex { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
public int GroupSortPriority { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
GroupDescriptor descriptor = new GroupDescriptor();
descriptor.GroupNames.Add("GroupName", ListSortDirection.Ascending);
radGridView1.GroupDescriptors.Add(descriptor);
ExpandSomeGroups();
}
You will notice that the "Animal" group is not expanded. However, if you remove the self-referencing hierarchy, the "Animal" group will be expanded.
When the RadGridView.EnabledPaging property is set to true, the user should be allowed to specify not only the PageSize,but also the PagesCount for example. Afterwards, it would be convenient to have an event similar to RowSourceNeeded in order to load pages data on demand.
To reproduce: 1. Populate RadGridView with data 2. Export the grid by using the SpreadExport 3. Open the produce Excel file and select some cells to generate a chart in Excel. You will notice that it is not possible to create a complete chart with the selected data. Workaround: use the ExportToExcelML >> http://www.telerik.com/help/winforms/gridview-exporting-data-export-to-excel-via-excelml-format.html
To reproduce:
this.radGridView1.DataSource = this.ordersBindingSource;
GridViewDateTimeColumn col = this.radGridView1.Columns["OrderDate"] as GridViewDateTimeColumn;
col.ExcelExportType = DisplayFormatType.GeneralDate;
col.ExcelExportFormatString = "dd-MMM-yy";
private void radButton1_Click(object sender, EventArgs e)
{
SpreadExport spreadExporter = new SpreadExport(radGridView1);
spreadExporter.RunExport(@"..\..\..\exportedFileQ12015.xlsx");
Process.Start(@"..\..\..\exportedFileQ12015.xlsx");
}
Workaround:
private void radButton1_Click(object sender, EventArgs e)
{
SpreadExport spreadExporter = new SpreadExport(radGridView1);
spreadExporter.CellFormatting += spreadExporter_CellFormatting;
spreadExporter.RunExport(@"..\..\..\exportedFileQ12015.xlsx");
Process.Start(@"..\..\..\exportedFileQ12015.xlsx");
}
private void spreadExporter_CellFormatting(object sender, Telerik.WinControls.UI.Export.SpreadExport.CellFormattingEventArgs e)
{
if (e.GridColumnIndex == 3 && e.GridCellInfo.Value is DateTime)
{
CellValueFormat cvf = new CellValueFormat("dd-MMM-yy");
e.CellSelection.SetFormat(cvf);
}
}
To reproduce:
public Form1()
{
InitializeComponent();
GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn("Select");
checkBoxColumn.EnableHeaderCheckBox = true;
radGridView1.MasterTemplate.Columns.Insert(0, checkBoxColumn);
for (int i = 0; i < 10; i++)
{
this.radGridView1.Rows.Add(false);
}
this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this.radGridView1.MultiSelect = true;
this.radGridView1.HeaderCellToggleStateChanged += radGridView1_HeaderCellToggleStateChanged;
}
private void radGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
{
if (e.State.ToString() == "On")
this.radGridView1.SelectAll();
else
this.radGridView1.ClearSelection();
}
Workaround 1:
private void radGridView1_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)
{
bool selected = e.State.ToString() == "On";
foreach (GridViewRowInfo r in this.radGridView1.Rows)
{
r.IsSelected = selected;
}
}
Workaround 2:
private void radGridView1_MouseUp(object sender, MouseEventArgs e)
{
RadCheckBoxElement el = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckBoxElement;
if (el!=null)
{
GridCheckBoxHeaderCellElement headerCell = el.Parent as GridCheckBoxHeaderCellElement;
bool selected = el.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On;
if (headerCell!=null)
{
if (selected)
{
this.radGridView1.SelectAll();
}
else
{
this.radGridView1.ClearSelection();
}
}
}
}
To reproduce: following the illustrated steps: http://screencast.com/t/D2TCpU2zo Workaround: set up the hierarchy programmatically.
To reproduce:
private void Form1_Load(object sender, EventArgs e)
{
this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
this.radGridView1.DataSource = this.customersBindingSource;
this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
foreach (GridViewColumn col in this.radGridView1.Columns)
{
col.MinWidth = 100;
}
ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
view.ColumnGroups.Add(new GridViewColumnGroup("Details"));
view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Address"));
view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Contact"));
view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["CompanyName"]);
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactName"]);
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactTitle"]);
view.ColumnGroups[1].Groups[0].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Address"]);
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["City"]);
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Country"]);
view.ColumnGroups[1].Groups[1].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Phone"]);
view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Fax"]);
radGridView1.ViewDefinition = view;
}
Please refer to the attached gif file illustrating the behavior when no ColumnGroupsViewDefinition is applied. The horizontal scroll-car should appear as well when the grid's width is less than the total MinWidth of all columns in case of ColumnGroupsViewDefinition.
Workaround: use GridViewAutoSizeColumnsMode.None and handle the RadGridView.SizeChanged event where you can adjust manually the Width of the visible columns on a way to simulate GridViewAutoSizeColumnsMode.Fill. Here is a sample approach:
private void Form1_Load(object sender, EventArgs e)
{
this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
this.radGridView1.DataSource = this.customersBindingSource;
this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.None;
foreach (GridViewColumn col in this.radGridView1.Columns)
{
col.MinWidth = 100;
}
ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
view.ColumnGroups.Add(new GridViewColumnGroup("Details"));
view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Address"));
view.ColumnGroups[1].Groups.Add(new GridViewColumnGroup("Contact"));
view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["CompanyName"]);
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactName"]);
view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["ContactTitle"]);
view.ColumnGroups[1].Groups[0].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Address"]);
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["City"]);
view.ColumnGroups[1].Groups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Country"]);
view.ColumnGroups[1].Groups[1].Rows.Add(new GridViewColumnGroupRow());
view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Phone"]);
view.ColumnGroups[1].Groups[1].Rows[0].Columns.Add(this.radGridView1.Columns["Fax"]);
radGridView1.ViewDefinition = view;
radGridView1.SizeChanged += radGridView1_SizeChanged;
}
private void radGridView1_SizeChanged(object sender, EventArgs e)
{
int totalMinimumWidth = 0;
int columnsCount = ColumnsCountWidth(out totalMinimumWidth);
int delta = this.radGridView1.Width - totalMinimumWidth;
AdjustColumnWidth(delta / columnsCount);
}
private void AdjustColumnWidth(int widthToAdd)
{
ColumnGroupsViewDefinition view = this.radGridView1.ViewDefinition as ColumnGroupsViewDefinition;
if (view == null)
{
return;
}
foreach (GridViewColumnGroup group in view.ColumnGroups)
{
AdjustColumnsInGroup(widthToAdd, group);
}
}
private void AdjustColumnsInGroup(int widthToAdd, GridViewColumnGroup group)
{
if (group.Groups.Count == 0)
{
foreach (GridViewColumnGroupRow row in group.Rows)
{
foreach (GridViewColumn col in row.Columns)
{
col.Width += widthToAdd;
}
}
}
else
{
foreach (GridViewColumnGroup g in group.Groups)
{
AdjustColumnsInGroup(widthToAdd, g);
}
}
}
private int ColumnsCountWidth(out int totalMinWidth)
{
int columnsCount = 0;
totalMinWidth = 0;
ColumnGroupsViewDefinition view = this.radGridView1.ViewDefinition as ColumnGroupsViewDefinition;
if (view == null)
{
return this.radGridView1.Columns.Count;
}
foreach (GridViewColumnGroup group in view.ColumnGroups)
{
columnsCount = IterateGroups(columnsCount, group, ref totalMinWidth);
}
return columnsCount;
}
private static int IterateGroups(int columnsCount, GridViewColumnGroup group, ref int totalMinWidth)
{
int count = columnsCount;
if (group.Groups.Count == 0)
{
foreach (GridViewColumnGroupRow row in group.Rows)
{
count += row.Columns.Count;
foreach (GridViewColumn col in row.Columns)
{
totalMinWidth += col.Width;
}
}
}
else
{
foreach (GridViewColumnGroup g in group.Groups)
{
count = IterateGroups(count, g, ref totalMinWidth);
}
}
return count;
}
Workaround: create a custom GridCheckBoxHeaderCellElement and override the Attach method
public class MyGridCheckBoxHeaderCellElement : GridCheckBoxHeaderCellElement
{
public MyGridCheckBoxHeaderCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
{
}
protected override Type ThemeEffectiveType {
get { return typeof(GridHeaderCellElement); }
}
public override void Attach(GridViewColumn data, object context)
{
if (((GridViewCheckBoxColumn)data).EnableHeaderCheckBox) {
base.Attach(data, context);
} else {
this.CheckBox.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
}
}
}
public Form1()
{
InitializeComponent();
this.radGridView1.CreateCell += radGridView1_CreateCell;
}
private void radGridView1_CreateCell(object sender, Telerik.WinControls.UI.GridViewCreateCellEventArgs e)
{
if (e.CellType == typeof(GridCheckBoxHeaderCellElement)) {
e.CellElement = new MyGridCheckBoxHeaderCellElement(e.Column, e.Row)
}
}
To reproduce:
1. Add a RadTreeView and a RadGridView
2. Use the following code snippet.
3. Select a node from the RadtreeView.
4. Activate the editor for the new row in RadGridView.
5. While the grid editor is active, select a new node in the tree.
public Form1()
{
InitializeComponent();
this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
}
public class ClassA
{
public int Id { get; set; }
public string Name { get; set; }
public string Grade { get; set; }
public ClassA()
{
}
public ClassA(int id, string name, string grade)
{
this.Id = id;
this.Name = name;
this.Grade = grade;
}
}
public class ClassB
{
public int NewID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ClassB()
{
}
public ClassB(int id, string firstName, string lastName)
{
this.NewID = id;
this.FirstName = firstName;
this.LastName = lastName;
}
}
private void radTreeView1_SelectedNodeChanged(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
{
if (radGridView1.DataSource is BindingList<ClassA>)
{
this.radGridView1.Columns.Clear();
this.radGridView1.DataSource = null;
BindingList<ClassB> list = new BindingList<ClassB>() { new ClassB(1, "John", "Wick") };
this.radGridView1.DataSource = list;
}
else
{
this.radGridView1.DataSource = null;
BindingList<ClassA> list = new BindingList<ClassA>() { new ClassA(1,"John", "A+") };
this.radGridView1.Columns.Clear();
this.radGridView1.DataSource = list;
}
}
Workaround: this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = false;
BindingList<Item> items = new BindingList<Item>();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 20; i++)
{
items.Add(new Item(i % 4, "Item" + i, "Type" + i % 2));
}
this.radGridView1.DataSource = items;
this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public Item(int id, string name, string type)
{
this.Id = id;
this.Name = name;
this.Type = type;
}
}
private void radButton1_Click(object sender, EventArgs e)
{
items.Insert(0, new Item(2, "Test", "Type0"));
}
WORKAROUND I:
GridViewSynchronizationService.SuspendEvent(this.radGridView1.MasterTemplate, KnownEvents.CurrentChanged);
Item item = new Item(2, "Test", "Type0");
items.Insert(0, item);
GridViewSynchronizationService.ResumeEvent(this.radGridView1.MasterTemplate, KnownEvents.CurrentChanged);
foreach (GridViewRowInfo row in this.radGridView1.Rows)
{
if (row.DataBoundItem == item)
{
this.radGridView1.CurrentRow = row;
break;
}
}
WORKAROUND II:
this.radGridView1.Rows.CollectionChanged += Rows_CollectionChanged;
this.radGridView1.GroupExpanding+=radGridView1_GroupExpanding;
List<GridViewRowInfo> expandedGroups = new List<GridViewRowInfo>();
private void radGridView1_GroupExpanding(object sender, GroupExpandingEventArgs e)
{
if (!e.IsExpanded && shouldCollapse)
{
expandedGroups.Add(e.DataGroup.GroupRow);
}
}
bool shouldCollapse = false;
private void radButton1_Click(object sender, EventArgs e)
{
shouldCollapse = true;
items.Insert(0, new Item(2, "Test", "Type0"));
}
private void Rows_CollectionChanged(object sender, Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e)
{
if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
{
this.radGridView1.CurrentRow = e.NewItems[0] as GridViewRowInfo;
this.radGridView1.CurrentRow.IsSelected = true;
GridViewRowInfo parent = this.radGridView1.CurrentRow.Parent as GridViewRowInfo;
if (parent != null)
{
parent.IsExpanded = true;
}
if (shouldCollapse)
{
shouldCollapse = false;
foreach (GridViewRowInfo r in expandedGroups)
{
if (!ContainsParentRow(parent, r))
{
r.IsExpanded = false;
}
}
expandedGroups.Clear();
}
}
}
private bool ContainsParentRow(GridViewRowInfo parent, GridViewRowInfo r)
{
GridViewGroupRowInfo group = r as GridViewGroupRowInfo;
if (group != null)
{
foreach (GridViewRowInfo childRow in group.ChildRows)
{
return ContainsParentRow(parent, childRow);
}
}
return parent == r.Parent;
}