Example T474976
Visible to All Users

How To: Utilize the UnboundSource component in code

Files to look at:

This example illustrates how to bind the DataGrid control to data by using the UnboundSource component in code. Values manually entered at runtime are saved to the Differences dictionary on the ValuePushed event. If for a required Data Grid cell, a dictionary entry exists, it will be loaded on the ValueNeeded event. Otherwise, the auto-generated value will be inserted.

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

UnboundDS-Code/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; using DevExpress.Data; namespace UnboundDS_Code { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); gridView1.OptionsBehavior.AutoPopulateColumns = false; CreateDS(); CreateGridColumns(); gridControl1.UseEmbeddedNavigator = true; } public void CreateDS() { UnboundSource unboundDS = new UnboundSource(); UnboundSourceProperty unboundSourceProperty1 = new UnboundSourceProperty() { DisplayName = "ID", Name = "Int", PropertyType = typeof(int) }; UnboundSourceProperty unboundSourceProperty2 = new UnboundSourceProperty() { DisplayName = "Day of Week", Name = "String", PropertyType = typeof(string) }; unboundDS.Properties.AddRange(new DevExpress.Data.UnboundSourceProperty[] { unboundSourceProperty1, unboundSourceProperty2}); unboundDS.ValueNeeded += unboundDS_ValueNeeded; unboundDS.ValuePushed += unboundDS_ValuePushed; unboundDS.SetRowCount(100000); gridControl1.DataSource = unboundDS; } void unboundDS_ValuePushed(object sender, UnboundSourceValuePushedEventArgs e) { var defaultValue = GetDefaultData(e.RowIndex, e.PropertyName); var cellKey = new CellKey(e.RowIndex, e.PropertyName); if(object.Equals(defaultValue, e.Value)) this.Differences.Remove(cellKey); else this.Differences[cellKey] = e.Value; } void unboundDS_ValueNeeded(object sender, UnboundSourceValueNeededEventArgs e) { if(this.Differences.Count > 0) { object rv; if(this.Differences.TryGetValue(new CellKey(e.RowIndex, e.PropertyName), out rv)) { e.Value = rv; return; } } e.Value = GetDefaultData(e.RowIndex, e.PropertyName); } public void CreateGridColumns() { gridView1.Columns.AddVisible("Int"); gridView1.Columns.AddVisible("String"); } private struct CellKey : IEquatable<CellKey> { int rowIndex; string propertyName; public int RowIndex { get { return rowIndex; } } public string PropertyName { get { return propertyName; } } public CellKey(int rowIndex, string propertyName) { this.rowIndex = rowIndex; this.propertyName = propertyName; } public bool Equals(CellKey other) { return this.RowIndex == other.RowIndex && this.PropertyName == other.PropertyName; } public override int GetHashCode() { return unchecked(RowIndex * 257 + (string.IsNullOrEmpty(this.PropertyName) ? 0 : this.PropertyName[0])); } public override bool Equals(object obj) { if(obj is CellKey) return Equals((CellKey)obj); else return false; } } readonly string[] DefaultStringData = CultureInfo.CurrentCulture.DateTimeFormat.DayNames; readonly Dictionary<CellKey, object> Differences = new Dictionary<CellKey, object>(); object GetDefaultData(int rowIndex, string propertyName) { switch(propertyName) { case "String": return DefaultStringData[rowIndex % DefaultStringData.Length]; case "Int": return rowIndex + 1; default: return null; } } } }
UnboundDS-Code/Program.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace UnboundDS_Code { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.