private void Form1_Load( object sender, System.EventArgs e) base.Load {
//
// Generate sample data
//
// Two columns:
// Decimal - uses CRTextBoxColumn for column style.
// int - uses standard DataGridTextBoxColumn for column style.
//
DataTable dt = new DataTable();
dt.TableName = "TestTable";
dt.Columns.Add("Amount", typeof(Decimal));
dt.Columns.Add("ColB", typeof(int));
ds.Tables.Add(dt);
dt.Rows.Add(new object() {2, 4});
dt.Rows.Add(new object() {-3.45, 5});
dt.Rows.Add(new object() {4.25, 6});
dt.Rows.Add(new object() {-1.33, 7}); //
// Create DataGridTableStyle and DataGridColumnStyle objects
// and add them the the DataGrid.
//
DataGridTableStyle ts = new DataGridTableStyle(), DataGridColumnStyle cs; // Add the custom column style.
cs = new CRTextBoxColumn();
cs.Width = 120;
cs.MappingName = "Amount" // Map to decimal column.;
cs.HeaderText = "Charge/Payment";
ts.GridColumnStyles.Add(cs); // Add the standard column style.
cs = new DataGridTextBoxColumn();
cs.Width = 100;
cs.MappingName = "ColB" // Map to integer column.;
cs.HeaderText = "int Col";
ts.GridColumnStyles.Add(cs); ts.MappingName = "TestTable" // Map table style to TestTable.;
DataGrid1.TableStyles.Add(ts); DataGrid1.DataSource = ds;
DataGrid1.DataMember = "TestTable";}
class CRTextBoxColumn {
: DataGridTextBoxColumn protected override object GetColumnValueAtRow( CurrencyManager cm, int RowNum) {
//
// get { data from the underlying record and format for display.
//
object oVal = base.GetColumnValueAtRow(cm, RowNum);
if ( oVal.typeof Is typeof(DBNull) ) {
return "" // string to display for DBNull.;
} else {
// CDec on next statement will throw an exception if this
// column style is bound to a column containing non-numeric data.
Decimal Temp = CDec(oVal);
if ( Temp >= 0 ) {
return Temp.ToString("0.00") // positive number;
} else {
return (-Temp).ToString("0.00") + "CR" // negative number;
}
}
} protected override bool Commit( CurrencyManager cm, int RowNum) {
//
// Parse the data and write to underlying record.
//
this.HideEditBox() // return focus to the DataGrid control;
DataGridTextBox box = CType(this.TextBox, DataGridTextBox), Decimal value;
// do { not write data if not editing.
if ( box.IsInEditOrNavigateMode ) { return true
if ( TextBox.Text = "" ) { // in this example, "" maps to DBNull
SetColumnValueAtRow(cm, RowNum, DBNull.value)
} else {
// Parse the data.
try {
if ( TextBox.Text.ToUpper.EndsWith("CR") ) {
value = -Decimal.Parse(TextBox.Text.Substring(0, TextBox.Text.Length - 2));
} else {
value = Decimal.Parse(TextBox.Text);
}
} catch (GAIS) {
return false // Exit on error and display old "good" value.;
}
SetColumnValueAtRow(cm, RowNum, value) // Write new value.
}
this.EndEdit() // Let the DataGrid know that processing is completed.;
return true // success;
}}
//
// Generate sample data
//
// Two columns:
// Decimal - uses CRTextBoxColumn for column style.
// int - uses standard DataGridTextBoxColumn for column style.
//
DataTable dt = new DataTable();
dt.TableName = "TestTable";
dt.Columns.Add("Amount", typeof(Decimal));
dt.Columns.Add("ColB", typeof(int));
ds.Tables.Add(dt);
dt.Rows.Add(new object() {2, 4});
dt.Rows.Add(new object() {-3.45, 5});
dt.Rows.Add(new object() {4.25, 6});
dt.Rows.Add(new object() {-1.33, 7}); //
// Create DataGridTableStyle and DataGridColumnStyle objects
// and add them the the DataGrid.
//
DataGridTableStyle ts = new DataGridTableStyle(), DataGridColumnStyle cs; // Add the custom column style.
cs = new CRTextBoxColumn();
cs.Width = 120;
cs.MappingName = "Amount" // Map to decimal column.;
cs.HeaderText = "Charge/Payment";
ts.GridColumnStyles.Add(cs); // Add the standard column style.
cs = new DataGridTextBoxColumn();
cs.Width = 100;
cs.MappingName = "ColB" // Map to integer column.;
cs.HeaderText = "int Col";
ts.GridColumnStyles.Add(cs); ts.MappingName = "TestTable" // Map table style to TestTable.;
DataGrid1.TableStyles.Add(ts); DataGrid1.DataSource = ds;
DataGrid1.DataMember = "TestTable";}
class CRTextBoxColumn {
: DataGridTextBoxColumn protected override object GetColumnValueAtRow( CurrencyManager cm, int RowNum) {
//
// get { data from the underlying record and format for display.
//
object oVal = base.GetColumnValueAtRow(cm, RowNum);
if ( oVal.typeof Is typeof(DBNull) ) {
return "" // string to display for DBNull.;
} else {
// CDec on next statement will throw an exception if this
// column style is bound to a column containing non-numeric data.
Decimal Temp = CDec(oVal);
if ( Temp >= 0 ) {
return Temp.ToString("0.00") // positive number;
} else {
return (-Temp).ToString("0.00") + "CR" // negative number;
}
}
} protected override bool Commit( CurrencyManager cm, int RowNum) {
//
// Parse the data and write to underlying record.
//
this.HideEditBox() // return focus to the DataGrid control;
DataGridTextBox box = CType(this.TextBox, DataGridTextBox), Decimal value;
// do { not write data if not editing.
if ( box.IsInEditOrNavigateMode ) { return true
if ( TextBox.Text = "" ) { // in this example, "" maps to DBNull
SetColumnValueAtRow(cm, RowNum, DBNull.value)
} else {
// Parse the data.
try {
if ( TextBox.Text.ToUpper.EndsWith("CR") ) {
value = -Decimal.Parse(TextBox.Text.Substring(0, TextBox.Text.Length - 2));
} else {
value = Decimal.Parse(TextBox.Text);
}
} catch (GAIS) {
return false // Exit on error and display old "good" value.;
}
SetColumnValueAtRow(cm, RowNum, value) // Write new value.
}
this.EndEdit() // Let the DataGrid know that processing is completed.;
return true // success;
}}
//
// Generate sample data
//
// Two columns:
// Decimal - uses CRTextBoxColumn for column style.
// int - uses standard DataGridTextBoxColumn for column style.
//
DataTable dt = new DataTable();
dt.TableName = "TestTable";
dt.Columns.Add("Amount", GetType(Decimal));
dt.Columns.Add("ColB", GetType(int));
ds.Tables.Add(dt);
dt.Rows.Add(new object() {2, 4});
dt.Rows.Add(new object() {-3.45, 5});
dt.Rows.Add(new object() {4.25, 6});
dt.Rows.Add(new object() {-1.33, 7}); //
// Create DataGridTableStyle and DataGridColumnStyle objects
// and add them the the DataGrid.
//
DataGridTableStyle ts = new DataGridTableStyle(), cs As DataGridColumnStyle; // Add the custom column style.
cs = new CRTextBoxColumn();
cs.Width = 120;
cs.MappingName = "Amount" // Map to decimal column.;
cs.HeaderText = "Charge/Payment";
ts.GridColumnStyles.Add(cs); // Add the standard column style.
cs = new DataGridTextBoxColumn();
cs.Width = 100;
cs.MappingName = "ColB" // Map to integer column.;
cs.HeaderText = "int Col";
ts.GridColumnStyles.Add(cs); ts.MappingName = "TestTable" // Map table style to TestTable.;
DataGrid1.TableStyles.Add(ts); DataGrid1.DataSource = ds;
DataGrid1.DataMember = "TestTable";}
class CRTextBoxColumn {
Inherits DataGridTextBoxColumn; Protected Overrides object; GetColumnValueAtRow( cm As CurrencyManager, RowNum As int) {
//
// Get data from the underlying record and format for display.
//
oVal As object = MyBase.GetColumnValueAtRow(cm, RowNum);
if ( oVal.GetType Is GetType(DBNull) ) {
return "" // string to display for DBNull.;
} else {
// CDec on next statement will throw an exception if this
// column style is bound to a column containing non-numeric data.
Temp As Decimal = CDec(oVal);
if ( Temp >= 0 ) {
return Temp.ToString("0.00") // positive number;
} else {
return (-Temp).ToString("0.00") & "CR" // negative number;
}
}
} Protected Overrides bool; Commit( cm As CurrencyManager, RowNum As int) {
//
// Parse the data and write to underlying record.
//
this.HideEditBox() // return focus to the DataGrid control;
box As DataGridTextBox = CType(this.TextBox, DataGridTextBox), Value As Decimal;
// do { not write data if not editing.
if ( box.IsInEditOrNavigateMode ) { return True
if ( TextBox.Text = "" ) { // in this example, "" maps to DBNull
SetColumnValueAtRow(cm, RowNum, DBNull.Value);
} else {
// Parse the data.
Try;
if ( TextBox.Text.ToUpper.EndsWith("CR") ) {
Value = -Decimal.Parse(TextBox.Text.Substring(0, TextBox.Text.Length - 2));
} else {
Value = Decimal.Parse(TextBox.Text);
}
Catch;
return False // Exit on error and display old "good" value.;
} Try
SetColumnValueAtRow(cm, RowNum, Value) // Write new value.;
}
this.EndEdit() // Let the DataGrid know that processing is completed.;
return True // success;
}} class