PDA

View Full Version : جستجو در DataGrid



mohsen_f_b
چهارشنبه 04 شهریور 1388, 00:26 صبح
سلام
طاعات و عباداتون قبول در گاه حق.
یه سوال داشتم.
من یه DataGrid دارم تو برنامم می خوام یه TextBox بزارم و هرچی توش نوشتم Enter زدم کل DataGrid رو بگرده و نشون بده.
یعنی هرچی تو TextBox نوشتم تو تمام فیلدهام بگرده و با زدن Enter دونه دونه اونارو پیمایش کنه.

باتشکر
شب خوش

Royce_Da
چهارشنبه 04 شهریور 1388, 02:12 صبح
سلام



DataView dv = newDataView();
dv.Table = dt;
dv.Sort = "ID";
int i = dv.Find("10");
MessageBox.Show(i.ToString());
dataGridView1.Rows[i].Selected = true;



اول اطلاعات datagrid یا datatable رو میریزی توی یه dataview بعد dataview ات رو با اون فیلدی که میخای پیداش کنی sort میکنی . بعد با متد find اونو پیدا میکنی . این متد شماره index محلی که اون رکورد وجود داره رو برمیگردونه . بعد تو میتونی با اون هر کاری بخای بکنی.


امیدوارم گرفته باشی چی میگم:لبخند: :چشمک:

shahab_ss
یک شنبه 26 مهر 1388, 17:01 عصر
private void SearchGrid()
{
//maxSearches = the # of cells in the grid
int maxSearches = dataGridView1.Rows.Count * dataGridView1.Columns.Count + 1;
int idx = 1;
bool isFound = false;
string searchValue = txtSearch.Text.ToUpper();

if (Convert.ToBoolean(txtSearch.Text.Length))
{
// If the item is not found and you haven't looked at every cell, keep searching
while ((!isFound) & (idx < maxSearches))
{
// Only search visible cells
if (dataGridView1.Columns[_Column].Visible)
{
// Do all comparing in UpperCase so it is case insensitive
if (dataGridView1[_Column, _Row].Value.ToString().ToUpper().Contains(searchValue))
{
// If found position on the item
dataGridView1.FirstDisplayedScrollingRowIndex = _Row;
dataGridView1[_Column, _Row].Selected = true;
isFound = true;
MessageBox.Show( "Search phrase found!");
}
}

// Increment the column.
_Column++;

// If it exceeds the column count
if (_Column == dataGridView1.Columns.Count)
{
_Column = 0; //Go to 0 column
_Row++; //Go to the next row

// If it exceeds the row count
if (_Row == dataGridView1.Rows.Count)
{
_Row = 0; //Start over at the top
}
}

idx++;
}

// If isFound = false then the phrase has not been found in the grid
if (! isFound)
{
MessageBox.Show("Search phrase not found!");
}
}
}این کد دقیقاً همین کار رو انجام میده

shirvancity1
یک شنبه 26 مهر 1388, 17:48 عصر
دوست عزیز یک این برنامه همین کار رو می کنه امیدوارم بدردت بخوره