Hello,
i need to search a selected date from datetimepicker control in a datagridview control which shows list of work days stored in sql database table.If searched date id found in gridview,then that row should be selected or pointed .Can anyone help me how to do it? thanks in advance
Loading
Vidya YadavPosted Oct 7, 2009, 5:16 AM
Use the given code, it may work.
Firstly set some properties of the datagridview
DataGridView1.ReadOnly = true;
DataGridView1.StandardTab = true;
DataGridView1.SelectionMode = FullRowSelect;
C# code snippet:
private void dtpSearch_ValueChanged(object sender, EventArgs e)
{
int gridIndex = 0;
string strDate = Convert.ToString(dtpSearch.Value.ToShortDateString());
for (int i = 0; i < DataGridView1.Rows.Count - 1; i++)
{
string strGridDate = Convert.ToString(Convert.ToDateTime(DataGridView1[2, i].Value).ToShortDateString());
if (strDate.Equals(strGridDate))
{
gridIndex = i;
break;
}
}
DataGridView1.CurrentCell = DataGridView1[2, gridIndex];
}
In this example 2nd column is the datefield, your's may be other.
Take care that the format of both date must be same For e.g "dd/mm/yy"
Regard's
Vidya
ds psPosted Oct 7, 2009, 2:30 AM
naura paxPosted Oct 6, 2009, 3:05 AM
'dgwdays.Rows[i].Selected = true;' by 'dgwdays.CurrentRow=dgwdays.Rows[i]';
hope it works.
ds psPosted Oct 6, 2009, 1:04 AM
i have to go to that row in the grid when searched date is found , can u tell me how to do so? my code is like below:
private void dtpsearch_ValueChanged(object sender, EventArgs e)
{
// arranging date in array for date parts only to compare
string[] arrspdate = dtpsearch.Text.Split(Convert.ToChar("/"));
string datestr = arrspdate[0] + "/" + arrspdate[1] + "/" + arrspdate[2];
DateTime dt1 = DateTime.Parse(datestr, System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);
for (int i = 0; i < dgwdays.Rows.Count; i++)
{
if (dgwdays.CurrentRow.Cells["WorkDate"].ToString() == datestr )
{
dgwdays.Rows[i].Selected = true;
}
}
naura paxPosted Oct 5, 2009, 7:21 AM
Do you have a DataTable bound to your datagridview, if yes then you can do following.
//dt (datatable)
DataRow[] drArr=dt.Select("DateFieldName='" + dtpDateControl.Value.ToString() + "'");
for (int i=0;i
MessageBox.Show(drArr["DateFieldName"].ToString();
}
But if you don't have Databiding in that case you can directly search through the grid using for loop
foreach(DataGridViewRow dr in grdView)
{
if( dr.Cells["DataFieldName"].Value==dtpContr.Value.ToString())
{
//your code.
}
}