By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,760 Members | 1,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,760 IT Pros & Developers. It's quick & easy.

Q: DataGrid and DataTimePicker

P: n/a
***
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[i];

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.

--
Soul


Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Soul" <no@spam.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
***
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[i];

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.

--
Soul


Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.