Hi ravikumar-1532,
First, you can use BindingSource.Filter property to filter the rows with dateTimePicker's value.
Then you can use DataGridView.SelectedRows property to get the collection of rows selected.
Meanwhile, you need to run these in DataGridView.SelectionChanged event which occurs when the current selection changes.
Here is my test code you can refer to.(In my test, I calculate the sum of column Field2)
public Form1()
{
InitializeComponent();
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void button1_Click(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = string.Format("Field2 >= #{0:MM/dd/yyyy}# And Field2 <= #{1:MM/dd/yyyy}#", dateTimePicker1.Value, dateTimePicker2.Value);
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
int total = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Sum(row => (int)row.Cells[1].Value);
label1.Text = total.ToString();
}
Test result:
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.