DataGrid Cell Edit
-
Hi guys and gals, I have a windows form with a DateTimePicker and a DataGrid. What I would like to do is double-click a cell in the DataGrid and have that cell update to show the date that is selected by the DateTimePicker. Any suggestions as to how I could do this? TIA Steve Web design and hosting http://www.kayess.com.au
-
Hi guys and gals, I have a windows form with a DateTimePicker and a DataGrid. What I would like to do is double-click a cell in the DataGrid and have that cell update to show the date that is selected by the DateTimePicker. Any suggestions as to how I could do this? TIA Steve Web design and hosting http://www.kayess.com.au
-
Thanks Polis, but that's not what the boss wants I'm afraid. Web design and hosting http://www.kayess.com.au
-
Thanks Polis, but that's not what the boss wants I'm afraid. Web design and hosting http://www.kayess.com.au
In that case, here's what I think. 1. First of all, in order for you to be able to handle the cell you click you have to get the current row by writing something like:
//Get current row
CurrencyManagerxCM
= (CurrencyManager)this.dataGrid2.BindingContext[this.dataGrid2.DataSource, this.dataGrid2.DataMember];
DataRowViewxDRV
= (DataRowView)xCM.Current;
DataRowcurrRow
= xDRV.Row;2. Then you move on by specifying that you wish to respond only to mouse-clicks that fall between a cell's range:
System.Drawing.Point
point
= new Point(e.X, e.Y);
DataGrid.HitTestInfoinfo
= this.dataGrid2.HitTest(point);
if(info.Type == DataGrid.HitTestType.Cell)
{
// Do your thing... e.g.
currRow[myDataTable.Columns["Date"]] = this.dateTimePicker1.Value.ToShortDateString();
}A piece of note here. In order to be able to get the your new point from e.X and e.Y, you will have to write the code in a relevant event (such as your dataGrid's mouseUp event) Something important: The actual change will not take part on the dataGrid itself but on the DataRow of the table that the dataGrid is bound to. By changing the column of your bound table, the value on the dataGrid's cell will automatically change Hope this pleases the boss ;) Regards, Polis Can you practice what you teach?
-
In that case, here's what I think. 1. First of all, in order for you to be able to handle the cell you click you have to get the current row by writing something like:
//Get current row
CurrencyManagerxCM
= (CurrencyManager)this.dataGrid2.BindingContext[this.dataGrid2.DataSource, this.dataGrid2.DataMember];
DataRowViewxDRV
= (DataRowView)xCM.Current;
DataRowcurrRow
= xDRV.Row;2. Then you move on by specifying that you wish to respond only to mouse-clicks that fall between a cell's range:
System.Drawing.Point
point
= new Point(e.X, e.Y);
DataGrid.HitTestInfoinfo
= this.dataGrid2.HitTest(point);
if(info.Type == DataGrid.HitTestType.Cell)
{
// Do your thing... e.g.
currRow[myDataTable.Columns["Date"]] = this.dateTimePicker1.Value.ToShortDateString();
}A piece of note here. In order to be able to get the your new point from e.X and e.Y, you will have to write the code in a relevant event (such as your dataGrid's mouseUp event) Something important: The actual change will not take part on the dataGrid itself but on the DataRow of the table that the dataGrid is bound to. By changing the column of your bound table, the value on the dataGrid's cell will automatically change Hope this pleases the boss ;) Regards, Polis Can you practice what you teach?
Thank you Polis, you are my hero :-D now if I do a
if(e.Clicks == 2)
in the MouseUp Event that will handle the double-click. Cheers Steve Web design and hosting http://www.kayess.com.au -
Thank you Polis, you are my hero :-D now if I do a
if(e.Clicks == 2)
in the MouseUp Event that will handle the double-click. Cheers Steve Web design and hosting http://www.kayess.com.auGlad I could help Steve Regards, Polis Can you practice what you teach?