finding the underlying datarow/cell of a bound TextBox

G

Geir Sanne

hi!!!!

ive bound a textbox to a dataset.

how can i get the current row that the textbox is displaying ?

i need to be able to call the DataRow.SetColumnError() method.

i dont have access to the dataset/datatable so i need to be able to get that
from the textbox or some other way.

regards
geir sanne
 
C

Cor Ligthert

Geir,

That is great, how you did this?
ive bound a textbox to a dataset.
i dont have access to the dataset/datatable

You have to use the currencymanager.position however without access to the
datable, that will not go.

Cor
 
G

Geir Sanne

hi!!!

ive managed to get both dataset and table into my class so now i do :

CurrencyManager cm = (CurrencyManager)txtBox.BindingContext[ds,dataMember];

DataRow row = ds.Tables[dataMember].Rows[cm.Position];

row.SetColumnError(ds.Tables[dataMember].Columns["columnName"], ex.Message);



but for some reason the row.SetColumn....method doesnt work.

I get : Additional information: object reference not set to an instance of
an object.

row, ex.Message and ds.Tables[dataMember].Columns["columnName"] returns a
legal value.



Any idea what could be wrong ?

-Geir
 
G

Geir Sanne

errorString = "Replace this text.";
Row.SetColumnError(1, errorString);
Assuming 1 is the indexnumber of your column "columnName"


this is exactly what im trying to do.

the prob is that it crashes with "Additional information: object
reference not set to an instance of
an object."

and when row, columnindex (1) and errorstring are not null i dont see
what the problem is

any ideas ?

-Geir
 
C

Cor Ligthert

Geir,

I tried this, however did not get an error

\\\
using System;
using System.Data;

namespace MyTest
{
public class Class1
{
[STAThread]
static void Main()
{
DataTable dt = new DataTable();
dt.Columns.Add("mycolumn");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "mytext";
DataRow dr = dt.Rows[0];
dr.SetColumnError(0, "mytext");
Console.WriteLine(dr.GetColumnError(0));
}
}
}
///
I hope this helps?

Cor
 
G

Geir Sanne

this is the exception i get when i call row.setcolumnerror();

- e {"Object reference not set to an instance of an object." }
System.Exception
+ [System.NullReferenceException] {"Object reference not set to an instance
of an object." } System.NullReferenceException
System.Object {System.NullReferenceException} System.Object
_className null string
_COMPlusExceptionCode -532459699 int
_exceptionMethod <undefined value> System.Reflection.MethodBase
_exceptionMethodString null string
_helpURL null string
_HResult -2147467261 int
_innerException { } System.Exception
_message "Object reference not set to an instance of an object." string
_remoteStackIndex 0 int
_remoteStackTraceString null string
_source null string
+ _stackTrace {System.Array} System.Object
_stackTraceString null string
_xcode -1073741819 int
_xptrs 1234904 int
HelpLink null string
HResult -2147467261 int
InnerException { } System.Exception
Message "Object reference not set to an instance of an object." string
Source "System.Data" string
StackTrace " at System.Data.DataTable.RecordChanged(Int32 record)\r\n
at System.Data.DataRow.RowErrorChanged()\r\n at
System.Data.DataRow.SetColumnError(DataColumn column, String error)\r\n at
System.Data.DataRow.SetColumnError(Int32 columnIndex, String error)\r\n at
Systek.ControlsLibrary.FieldValidator.ValidateTextBox(TextBox txtBox) in
d:\\petroonlinesource\\petroonline\\systek\\controlslibrary\\fieldvalidator.
cs:line 130" string
+ TargetSite {System.Reflection.RuntimeMethodInfo}
System.Reflection.MethodBase


-Geir
 
G

Geir Sanne

i found the problem.

if i bind the column to a textbox i get an exception when i do
row.setcolumnerror().

if i dont bind the column to the textbox it works fine.

must be a bug in .net ?

-Geir
 
C

Cor Ligthert

Geir,

Do you know how somebody can help you when you do not want to show anything
of your code. You did nowhere tell in this thread that you had binded the
column to the textbox.

Although I have asked different times to show or tell something more.

Cor
 
G

Geir Sanne

here is some sample code.

i bind textbox1 to a column which has the text "bjarne"
when i type "arne" in the textbox its supposed to be an error.

what happens when i type "arne" is that
row.setcolumnerror(inedx,"somerrror"); is called.
the problem is i guess that before this method is done .net rollbacks the
change so the textbox displays "bjarne" and that triggers the
textbox.ontextchanged that does the row.setcolumnerror(index,null);
so the columnerror is removed before row.setcolumnerror(inedx,"somerrror");
method is done.

so i guess if i can make .net leave the "arne" in the textbox instead of
rollingback to "bjarne" it will probably work


-Geir



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid1;
private DataSet ds = new DataSet();

public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.ultraGrid1 = new Infragistics.Win.UltraWinGrid.UltraGrid();

((System.ComponentModel.ISupportInitialize)(this.ultraGrid1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.CausesValidation = false;
this.textBox1.Location = new System.Drawing.Point(88, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//
// ultraGrid1
//
this.ultraGrid1.Location = new System.Drawing.Point(48, 160);
this.ultraGrid1.Name = "ultraGrid1";
this.ultraGrid1.Size = new System.Drawing.Size(440, 208);
this.ultraGrid1.TabIndex = 1;
this.ultraGrid1.Text = "ultraGrid1";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(624, 413);
this.Controls.Add(this.ultraGrid1);
this.Controls.Add(this.textBox1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
((System.ComponentModel.ISupportInitialize)(this.ultraGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion


private void textBox1_TextChanged(object sender, System.EventArgs e) {
if (textBox1.Text == "arne") {
CurrencyManager cm =
(CurrencyManager)textBox1.BindingContext[ds,"test"];
DataRow row = ds.Tables["test"].Rows[cm.Position];

int index = ds.Tables["test"].Columns.IndexOf("name2");

textBox1.BackColor = Color.Red;

textBox1.TextChanged -= new
System.EventHandler(this.textBox1_TextChanged);
row.SetColumnError(index,"somerror");
textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
} else {
CurrencyManager cm =
(CurrencyManager)textBox1.BindingContext[ds,"test"];
DataRow row = ds.Tables["test"].Rows[cm.Position];

int index = ds.Tables["test"].Columns.IndexOf("name2");

textBox1.BackColor = Color.White;

if (!row.GetColumnError(index).Equals(""))
row.SetColumnError(index,null);
}
}

private void Form3_Load(object sender, System.EventArgs e) {
DataTable t = ds.Tables.Add("test");
t.Columns.Add("id",typeof(decimal));
t.Columns.Add("name",typeof(string));
t.Columns.Add("name2",typeof(string));

DataRow r1 = t.NewRow();
r1[0] = 1;
r1[1] = "geir";
r1[2] = "bjarne";

t.Rows.Add(r1);

textBox1.DataBindings.Add("Text", ds, "test.name2");

ultraGrid1.SetDataBinding(ds,"test");
}
}
}
 
C

Cor Ligthert

Geir,

Why do you use such a in my opinion difficult method and not the validating
event of the textbox for that?

(Than you can even use the errorprovider)

Cor

Geir Sanne said:
here is some sample code.

i bind textbox1 to a column which has the text "bjarne"
when i type "arne" in the textbox its supposed to be an error.

what happens when i type "arne" is that
row.setcolumnerror(inedx,"somerrror"); is called.
the problem is i guess that before this method is done .net rollbacks the
change so the textbox displays "bjarne" and that triggers the
textbox.ontextchanged that does the row.setcolumnerror(index,null);
so the columnerror is removed before
row.setcolumnerror(inedx,"somerrror");
method is done.

so i guess if i can make .net leave the "arne" in the textbox instead of
rollingback to "bjarne" it will probably work


-Geir



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid1;
private DataSet ds = new DataSet();

public Form3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.ultraGrid1 = new Infragistics.Win.UltraWinGrid.UltraGrid();

((System.ComponentModel.ISupportInitialize)(this.ultraGrid1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.CausesValidation = false;
this.textBox1.Location = new System.Drawing.Point(88, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//
// ultraGrid1
//
this.ultraGrid1.Location = new System.Drawing.Point(48, 160);
this.ultraGrid1.Name = "ultraGrid1";
this.ultraGrid1.Size = new System.Drawing.Size(440, 208);
this.ultraGrid1.TabIndex = 1;
this.ultraGrid1.Text = "ultraGrid1";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(624, 413);
this.Controls.Add(this.ultraGrid1);
this.Controls.Add(this.textBox1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
((System.ComponentModel.ISupportInitialize)(this.ultraGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion


private void textBox1_TextChanged(object sender, System.EventArgs e) {
if (textBox1.Text == "arne") {
CurrencyManager cm =
(CurrencyManager)textBox1.BindingContext[ds,"test"];
DataRow row = ds.Tables["test"].Rows[cm.Position];

int index = ds.Tables["test"].Columns.IndexOf("name2");

textBox1.BackColor = Color.Red;

textBox1.TextChanged -= new
System.EventHandler(this.textBox1_TextChanged);
row.SetColumnError(index,"somerror");
textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
} else {
CurrencyManager cm =
(CurrencyManager)textBox1.BindingContext[ds,"test"];
DataRow row = ds.Tables["test"].Rows[cm.Position];

int index = ds.Tables["test"].Columns.IndexOf("name2");

textBox1.BackColor = Color.White;

if (!row.GetColumnError(index).Equals(""))
row.SetColumnError(index,null);
}
}

private void Form3_Load(object sender, System.EventArgs e) {
DataTable t = ds.Tables.Add("test");
t.Columns.Add("id",typeof(decimal));
t.Columns.Add("name",typeof(string));
t.Columns.Add("name2",typeof(string));

DataRow r1 = t.NewRow();
r1[0] = 1;
r1[1] = "geir";
r1[2] = "bjarne";

t.Rows.Add(r1);

textBox1.DataBindings.Add("Text", ds, "test.name2");

ultraGrid1.SetDataBinding(ds,"test");
}
}
}
 
G

Geir Sanne

I dont think validating for errorprovider works as i want too, and we
use the our own FieldValidator class for grids and we want it to work
for all controls.

When the user clicks on a row in the grid, the details are displayed in
some textboxes. i want the user to be able to type something wrong
without having to change it right away. he can then look at other rows
and when he clicks save he will be told to fix the errors first.
errors are mark with an error sign in the grid, and the details in the
textboxes, dropdowns are marked be red backcolor.

is this possible with errorprovider or validating event ?
 
C

Cor Ligthert

Geir,

Than check before your update, why take those difficult steps to do it in
your update?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top