Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > programmatically find a row in a DataGridView and select it
 

programmatically find a row in a DataGridView and select it

Hi There,

I've got a DataView populated with data from a Table. The Table contains a PrimaryKey. With this DataView I populate a DataGridView and show all Columns except the PrimaryKey Column. What I now want is to find a row due to a given PrimaryKey Value.

How can I do this without to iterate thorugh all rows and check the PrimaryKey from each DataRowView???

Thanks in advance...
Greet Markus

Markus Fritz  Monday, January 02, 2006 3:47 PM

whew!!! my lip was curling into a sneer as I smelt the old vbsux way of doing things. . . vbsux did alot of people a disservice. I can't stand vbsux.

take a look at the BindingSource class. you should setup a binding source to intermediate between your data and the grid. set the grids datasource to the BindingSource.

Use BindingSource.Find to find the item. Use BindingSource.Position to go to it.

Blair Allen Stark  Monday, January 02, 2006 4:30 PM
 Markus Fritz wrote:

With this DataView I populate a DataGridView

you populate??? do you load the grid by iterating through your table and then copying the values to the grid???

Blair Allen Stark  Monday, January 02, 2006 4:12 PM
No I do not load the grid by iterating through my table. I set the DataView as DataSource of my DataGridView!!!!!
Markus Fritz  Monday, January 02, 2006 4:14 PM

whew!!! my lip was curling into a sneer as I smelt the old vbsux way of doing things. . . vbsux did alot of people a disservice. I can't stand vbsux.

take a look at the BindingSource class. you should setup a binding source to intermediate between your data and the grid. set the grids datasource to the BindingSource.

Use BindingSource.Find to find the item. Use BindingSource.Position to go to it.

Blair Allen Stark  Monday, January 02, 2006 4:30 PM
thank you!! but the Position Property does not contain the found position but the find method does return the position of the found row.
Markus Fritz  Tuesday, January 03, 2006 7:37 AM

not at a place to check it, but I think its -

myBindingSource.Position = myBindingSource.Find(....)

Blair Allen Stark  Tuesday, January 03, 2006 8:29 AM

WOW

So Simple - I have spent a few days on this same problem!

Thanks

JSMARTIN  Wednesday, November 08, 2006 5:05 AM

This is exactly what I need to do!

Here's what I need a bit of help with - I'm just working with a simple test project here. I popped a datagridview on the form, and it pops up this window inviting me to get the data from the database and bind it to the datagridview.

The result is that my form load has one line of code:

Me.ADVClientsTableAdapter.Fill(Me.ADV4SQLDataSet.ADVClients)

What I need to know is, where do I put the code to do what you said:

"setup a binding source to intermediate between your data and the grid"

and "set the grids datasource to the BindingSource"?

Then I can do the myBindingSource.Position = myBindingSource.Find(....) to find the data in any column I want, I suppose, right?

By the way, I need to search on partial entry, for example, the user types "Bo" and I need to find the first instance of a first name that starts with "Bo"... not sure if my need fits this case, since the OP was re searching for the PK.

Thanks very much!

BobishKindaGuy  Friday, November 24, 2006 12:43 AM

Okay, found the answer to the above.

Ijust dropped a bindingsource on my form, and bound it to the adapter, and bound the datagridview to the bindingsource instead of to the adapter.

Then I wrote this little sub tofind a person's last name in the datagridview.

Problem is, it only succeeds with the find when the user has typed the whole last name exactly.

What I would prefer is a partial match so if the person starts typing a name, it correspondingly moves the current row dynamically as the person types. Can this be done using the bindingsource "Find" command?

Here's the code that works if the user types the complete last name:

Private Sub DataGridView1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress

Static KeysPressed As String

Static LastKeyPress As Date

'if more than 2 seconds has passed, clear the string

If DateDiff(DateInterval.Second, LastKeyPress, Now) >= 2 Then

KeysPressed = CStr(e.KeyChar)

Else

KeysPressed += CStr(e.KeyChar)

End If

LastKeyPress = Now

'to see the typed string: Label1.Text = KeysPressed

Dim dg As DataGridView = CType(sender, DataGridView)

Dim DS As DataSet = CType(BindingSource1.DataSource, DataSet)

Dim FieldNameToSearch As String = "StLastName"

BindingSource1.Position = BindingSource1.Find(DS.Tables(0).Columns(FieldNameToSearch).ToString, KeysPressed)

End Sub

BobishKindaGuy  Friday, November 24, 2006 2:46 AM

just curious, why not use a textbox like textboxSearchand its textchanged event instead of the inclusive anything typed on form from the forms.keyPressEvent?

Also why not use

Private Sub textboxSearch_TestChanged(object sender, EventArgs e)

if (textboxSearch.Length > 2) then

BindingSource1.Position = BindingSource1.Find(FieldNameToSearch , textboxSearch)

end If

End Sub

No static field needed whatsoever

fs - new to w7  Sunday, December 02, 2007 12:16 AM

Good question-

It seems more intuitive to me for the user to click somewhere on the datagrid and start typing. So when a window opens, I suppose it would be good to set the focus to the most obvious datagrid, so the user doesn't even have to tab or click at all.

If it were a textbox, the user would have to explicity click the textbox (or tab into it) and start typing. The textbox is always "there" and there is a question in the user's mind why it is there. This becomes a "training issue", which is what I call it when you have to explain why a feature is on a window.

So I prefer the former, although the latter is simpler.

I also put a label in the header of the datagrid to feed back the characters typed. With a transparent background and no border, the letters "appear" in the top right corner of the datagrid's text header area when the user types.

I think the DataGridView control that ships with VB2005 has a built-in "select-as-you-type" feature, but I'm not sure how it works, i.e. which column it decides to search in. But the DataGrid does not seem to.

Anyway,I've always liked that "auto-select" feature of some software that I've used. You seea list, you just start typing, and the list moves to the appropriate row.

BobishKindaGuy  Monday, December 03, 2007 2:57 AM

thank yiou. Now I understand your design choices. I guess you must either be using the datagrid in read only mode or have some other GUI for use to start editing.

I am still sort of a newbie to visual studio .net and still tend to think of thetraditional style of auto focus on a critera control before displaying.because of the large amount of records available. I believe they now call it design for scalability for large number of user shared database application

I do like your technique forintrincially small number of record set.

I certainly don't know a way of just retrieving first 10 or whatever in a datagred/datagridview. I do know how to the filter to select a narrow range of records

So far I prefer Datagridview over DataGrid

BTW, off topic: started using c# 2008 as it seems to be easier with datagridview and also free form databinding for non text data

fs - new to w7  Monday, December 03, 2007 4:54 PM

You can use google to search for other answers

Custom Search

More Threads

• Trying to make a simple database application: Updating a ListBox with data related to another
• Fixed Length Text File Editor using DataGridView in C#
• "Update Affected 0 of 1 rows" having deleted a parent row
• Insert data from Data grid view to database through the use of Datatable
• Suppress update to datagridview
• DatagridView Error!
• Datagrid.Cell Backcolor
• DataGridViewcombobox in n-tier app
• check if data in a dataset's column alrady exists
• Specifying Columns in Code