:-) Here's another one that's bugging me.....again in VB.NET 2005 and windows forms.

After selecting an account from a dropdownlist, I am populating a datagrid with items that correspond to that account. Really, I just wanted to display a summary of the items in the datagrid, and then pass the internal key from whatever record that is selected. From whatever was selected I would populate textboxes and allow the user to edit parts of the data. I've used datagrids to edit things before, and didn't have much luck with reliably controlling how the user edited the data. I tried a selectbox (I think?) but I liked how the datagrid gave me column headings, plus allowed you to sort on the fly by clicking on the column headings. Anyway....

I've got a datatable being filled from SQL, and am binding that to a datagrid. I'm filling it with my primary key, and then making invisible that column so the user doesn't see it. I've got it set up so clicking the in the datagrid selects the whole row, the user can't edit rows, etc., and that all works. I'm getting the primary key with this:

myDataGrid.SelectedRows.Item(0).Cells.Item("ID1").Value.ToString

and this seems to work just fine. The problem is, after I've selected a row, if I try to click on a column heading to resort the grid, the program crashes. I'm guessing I need to get the selected row/key differently, or reset something when I click on a column header, but I wanted to know why it was happening and what I should do to fix it. I guess I could just disable sorting on column headers (can I do this?), but it would be neat to be able to keep that in. Maybe I'm just selecting the row wrong, or getting the info from the selected row wrong? Any help would be greatly appreciated. Thanks!!

Just wanted to post an update:

I still don't really know why this was happening, but to "fix" it, I ended up doing a "Try...Catch...End Try", and having nothing if it caught an error. That was the problem - when I'd click a heading, it would reset (maybe?) the grid or something for an instant, and then report an error. If I suppressed the error, I'm assuming it's still doing it, but after the "error" it then resorts just fine, and works how I wanted it to. Strange.

On a somewhat related note....is there a way to select a row based on a value in a DataGrid? I'm selecting a row in the datagrid, loading that data into the form (via textboxes), and then updating the database with the new data. I then want to rebind the datagrid, and have it retain the original selected row that I was "editing". I don't see something like "FindByValue" or whatever.

Heh, I know that I'm not using the datagrid for what its really supposed to be for, but my users don't like editing within the datagrid itself. So it's mostly for display purposes only! :-) Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.