Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a value in a dataGrid 1

Status
Not open for further replies.

kmfna

MIS
Sep 26, 2003
306
US
Hello all,

I am working on a project that has a datagrid bound to a dataset (well, a table of the dataset). This has worked nicely to this point, however, I need to be able to allow a user to put a straight set of numbers (without slashes) in a date field. I have the function to change the numbers (ex: 01012001) to a readable format (ex: 01/01/2001), but the datagrid is not allowing me to make the change before it reads the numbers as an invalid date and resets it to its default. I have tried dataset rowchanging, rowchanged, columnchanging, columnchanging events as well as datagrid currentcellchanged...and can not catch the value before the grid automatically rolls the change back to default. Any ideas?? I've been working on this for a bit and cannot seem to get it to work. If you need any more info on it, let me know and I'll attempt to clarify.

Thanks in advance,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
The only way that I was able to handle this problem was to inherit the DataGridTextBoxColumn class and create my own custom DataGridColumnStyle. I overrode the Commit method and created an event handler called Parse.

If you don't want to get that involved, what you could do is put the specific code to Parse the text from the TextBox directly in the Commit method rather than creating an event handler.

Something that I didn't try that you might be able to get to work is to attach to one of the event handlers of the TextBox. This would avoid having to inherit the class (though I'm not sure what event you would attach to or what you would do about it). The TextBox used by the DataGridTextBoxColumn class is publicly exposed via the TextBox property.
 
Hmm....that is a good idea, any chance I could get you to drop a simple example of how you implemented this?? I GREATLY appreaciate the help, I'm being constantly hounded about this.

Thanks again,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
This is not working code as I had to hack a lot out but this should give you a rough idea:

Code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Control {
public class DataGridTextBoxColumn : System.Windows.Forms.DataGridTextBoxColumn {
	//This event provides the ability to parse the text from the text box
	//The implementation is made to match the Binding class
	public event ConvertEventHandler Parse;


	protected virtual void OnParse(ConvertEventArgs evt) {
		if (this.Parse != null) this.Parse(this, evt);
	}
	protected override bool Commit(CurrencyManager mgr, int intRow) {
					//Give the parsing event code first priority so that it can override any of
					//the other implementations here
					ConvertEventArgs evt = new ConvertEventArgs(
						this.TextBox.Text, this.PropertyDescriptor.PropertyType);

					this.OnParse(evt);

this.TextBox.Text = evt.Value.ToString();

return base.Commit();

	}

Basically, you grab the text from the TextBox, pass it off to the parse event, then the client code reformats it to a datetime, and you put the reformatted text back into the textbox. The regular code for the DataGridTextBoxColumn class then does its thing using the corrected text.
 
Thanks a lot for that, though I already had my overloaded function, though it was cool of you to show that, the question is, how do you make the grid's column be the new overloaded text box??

thanks again, and I'll be leaving you a star for your greatness,

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
You'll have to go into the form designer's code and manually change the variable declaration from:

System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;

to:

Control.DataGridTextBoxColumn dataGridTextBoxColumn1;

If you choose to include the inherited class within your root namespace, you won't need to include the namespace at all:

DataGridTextBoxColumn dataGridTextBoxColumn1;

Once you do this, you will get a compiler error where the class is instantiated:

this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();

changes to:

this.dataGridTextBoxColumn1 = new Control.DataGridTextBoxColumn();

Thank you for the feedback Kevin!
 
dalchri,

First off, quite welcome for the feedback, you earned it...now, I tried to find the code that you mentioned, but mine does not add the columns like that. My columns are, I believe, created with the databinding to the datatable. I tried to add the column as a column style, but it doesn't call the Commit override. Also, a point that I forgot until I started checking on all of this, the datagrid is a custom grid, derived from the datagrid base class....would this make a difference??

Thanks again for all of your help.
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
That never occurred to me. With my approach, you would need to create the DataGridTableStyle and DataGridColumnStyles in the designer and not have the DataGrid create them dynamically at runtime.

If you really need them to be dynamic, you can probably do this yourself in code as I don't think the MS code is terribly sophisticated.

Code:
DataTable tbl;
DataGrid grd
DataGridTableStyle tblStyle;

foreach (DataColumn col in tbl) {
      DataGridTextBoxColumn colStyle;

      colStyle.MappingName = col.Name;
      colStyle.Caption = col.Name;
      tblStyle.GridColumnStyles.Add(colStyle);
}
grd.TableStyles.Add(tblStyle);

Or something close to that...
 
Hmm...I'll check on that a little more. I actually did try to add it as a style, but it didn't work. One time it took away all of my columns entirely (Crazy, huh?) and another time it left the columns, but never called the overloaded commit...but I'll check a little more, maybe I can get it to work. If not, maybe we can come up with another option.

Thanks again,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top