Q: DataGrid and DataTimePicker

S

Soul

***
I am not sure this question belong to *.languages.csharp or
*.framework.windowsdorms.databinding, so I post to both
***

Hi,

Currently I have a DataGrid which bind to a DataSet. One of the column in
the DataSet table consist of DateTime value. I also have two DateTimePicker
(startDate and endDate) on my Windows form.

I currently able to program manually so that when user choose the date in
both startDate and endDate DateTimePicker, the DataGrid will auto select
those rows that fall into the date range, the code of this function look
something like:

// DataSet ds, DataGrid dg, CurrencyManager cm, DateTimePicker startDate and
DateTimePicker endDate declare somewhere else
private void dateTimePicker_ValueChanged(object sender, System.EventArgs e)
{
DataRow dr = null;
int firstSelectedRow = 0;

for ( int i = 0; i < this.da.Tables["tableA"].Rows.Count; i++) //
Unselect all
{
this.dg.UnSelect(i);
}

for ( int i = 0; i < this.ds.Tables["tableA"].Rows.Count; i++)
{
dr = this.ds.Tables["tableA"].Rows;

if ( (DateTime) dr["Date"] >= this.startDate.Value.Date &&
(DateTime) dr["Date"] <= this.endDate.Value.Date )
{
this.dg.Select(i);

if ( firstSelectedRow == 0 )
firstSelectedRow = i;
}
}

cm.Position = firstSelectedRow;
}

My questions are, how do I program so that if user select multiple rows in
the DataGrid, then also both the DateTimePicker will reflect the DateTime
value of the first and last selected rows. I tried using the DataGrid's
CurrentCellChange event and it don't seems to work really well.

In addition, how to prevent infinite loop if I program manually because if
user select multiple rows, then the value of those two DataTimePicker will
change right? Then again, it will reselect rows in the DataGrid...

If there an easy way like binding the DataGrid to those DateTimePicker? :)

Thank you.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

There's no easy way, I think. DataGrid does not expose any events related to
row selection, as far as I know. You can introduce your own ones by
inheriting from the DataGrid and handling its keyboard and mouse input
(clicks on row headers and selection keystrokes like shift-arrowup,
shift-arrowdown etc.)

In addition, DataGrid seems to have a protected method for unselecting all
rows at once - you can also employ it in your inherited control.

As for preventing the infinite loop, you might probably temporarily unwire
the ValueChanged event handler until you have finished processing the
selected rows.
 

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