c# - setting the selected row programmatically for a DataGridRow (not by using the index!) -


i have form datagridview on it, gets populated in form's constructor datasource using data adapter (adsdataadapter). shows people's first name, surname , reference number (which primary key of underlying table in database). rows in datagridview ordered surname field (by using order by clause in select statement gets passed data adapter).

after datagridview populated, want able type surname in textbox, , have datagridview navigate first row first letters of surname matches user types.

how go doing navigation?

the "navigate" solution

private void textbox1_textchanged(object sender, eventargs e) {   (int i=0;i<thedatagridview.rowcount;i++)   {     if (thedatagridview.rows[i].cells["surname"].value.tostring().startswith(textbox1.text))     {       thedatagridview.currentcell = thedatagridview.rows[i].cells["surname"];       // optionally       thedatagridview.firstdisplayedscrollingrowindex = thedatagridview.currentcell.rowindex;       break ;     }   } } 

the "filter" solution

first, bind datatable datagridview via bindingsource.

bindingsource thebindingsource = new bindingsource() ; thebindingsource.datasource    = thedatatable ; thedatagridview.datasource     = thebindingsource ; 

then set filter property of bindingsource in textchanged event:

private void textbox1_textchanged(object sender, eventargs e) {   thebindingsource.filter = "surname '"+textbox1.text+"%'";   // if no match : cancel filter   if (thedatagridview.rowcount==0) thebindingsource.filter = "" ;  } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -