Windows Develop Bookmark and Share   
 index > Windows Forms General > how to move to the first editable cell or the first cell in datagridview
 

how to move to the first editable cell or the first cell in datagridview

Hi All,

I inheritedan existing code forDataGridView, which has a nice cell_validating(). The problem is that if user use a mouse to click on the last row, or press enter key, or arrow key to go down to the last row - which is a new empty row - and enter some data in the middle cell, the validation take place and complaining that the first cell cannot have a blank value.

At that point, user cannot even go to that blank cell to enter some data, because the cell_validating() will be called again.

My question is, how do I move the focus to that first, blank cell to let the user enter data?

It seems to me that cell_validating() should not be a place to validate all cell. Should I move all validating to row_validating() instead?

Thanks


David N.
David N_2  Friday, January 23, 2009 6:29 AM

Hi David,

Each time when a DataGridViewCell loses focus, the CellValidating event is raised. Each time when a DataGridViewRow loses focus, the RowValidating event is raised. In your case, since you'd like to validate the data in the first cell only when the user finish editing the whole row,you should handle the RowValidating event of the DataGridView to do that.

The following is a sample:

void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (!this.dataGridView1.Rows[e.RowIndex].IsNewRow
&& this.dataGridView1.Rows[e.RowIndex].Cells[0].Value == DBNull.Value)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
this.dataGridView1.Rows[e.RowIndex].ErrorText = "input data here";
e.Cancel = true;
}
else
{
this.dataGridView1.Rows[e.RowIndex].ErrorText = "";
}
}

Please try my suggestion to see if it works.

Sincerely,

Linda Liu

Linda Liu  Thursday, January 29, 2009 6:55 AM

Hi

It is all the bussiness logic applied in your control from which you have inherited to use it in your application.If you dont want the feature just overide the particular one that First cell be blank and so move to the next selected cell...and so on.

I think it I am with you in understanding what you have asked for.Please let me know if any issue.

Thankx.


Please remember to click 'Mark as Answer' on the post that helped you. Unmark if it provides no help
Sreenath G V  Friday, January 23, 2009 9:36 AM

Thanks for replying.

But I think your answer is not what I am looking for. Of course, I can override any function if I wish. However, in the real business world, you cannot, should not, and don't want to, rewrite any thing from scratch.

The problem here is neither I am not fully understand the MS datagridview throughly, or there is a big problem in the DataGridView object. Until I am 100% for sure, I tend to blame myself first.

The other problem is that, you cannot tell the users to always use the tab key when there is still the enter key and the mouse. In a perfect world, users just his that tab key, enter data, and then his the tab key. But we are not living in a perfect world. Users will use the mouse to click on an empty cell of the next empty row. And users will press the enter key, and users will do any thing they wish too. We cannot blame them.

Back to my question. This have driven me nut. I only looking for a controllable way to move a currentcell to the first cell. I cannot do it in the CellValidating(), I cannot do it in the RowValidating(). So, which datagridview method is best method to trap for setting the current cell to the first cell.

Thanks


David N.
David N_2  Sunday, January 25, 2009 4:45 AM

Hi David,

Each time when a DataGridViewCell loses focus, the CellValidating event is raised. Each time when a DataGridViewRow loses focus, the RowValidating event is raised. In your case, since you'd like to validate the data in the first cell only when the user finish editing the whole row,you should handle the RowValidating event of the DataGridView to do that.

The following is a sample:

void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (!this.dataGridView1.Rows[e.RowIndex].IsNewRow
&& this.dataGridView1.Rows[e.RowIndex].Cells[0].Value == DBNull.Value)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
this.dataGridView1.Rows[e.RowIndex].ErrorText = "input data here";
e.Cancel = true;
}
else
{
this.dataGridView1.Rows[e.RowIndex].ErrorText = "";
}
}

Please try my suggestion to see if it works.

Sincerely,

Linda Liu

Linda Liu  Thursday, January 29, 2009 6:55 AM

You can use google to search for other answers

Custom Search

More Threads

• How to get Continuous Smooth Progress Bar in Windows Application
• Does AllowScriptAccess stops loading?
• Error as "Falied to import the ActiveX control. Please ensure it is properly registered."
• UserControl with other controls inside (URGENT)
• datagridview current cell
• Capture Webcam
• x marks the spot
• Datagridview Text File, Icon
• WebBrowser Control and Printing
• Register 3rd party dll files during software installation.