| | |
another one...selecing rows in datagrid and sorting with column headers
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
:-) 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!!
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!
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!
![]() |
Similar Threads
- how to swap rows in a dataGrid (ASP.NET)
- VB6 Database Program (Visual Basic 4 / 5 / 6)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- Adding and deleting a column in JTable (Java)
- need Help in store Datagrid item in an array (VB.NET)
- VC++: setting up columns and rows in a Datagrid (C++)
- DataGrid - Column help needed (Visual Basic 4 / 5 / 6)
- To display an icon in a datagrid (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: Problem with upgrading Frame 1.1
- Next Thread: Problem while Installing .Net Framework SDK 1.1
| Thread Tools | Search this Thread |
.net .net2008 2008 access account advanced application array basic beginner browser button buttons center click code combo cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic excel exists fade filter forms ftp generatetags html images input insert intel internet listview map mobile module monitor msaccess net number objects open panel passingparameters pdf picturebox picturebox2 port position print printing problem regex right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey table temperature textbox timer timespan transparency txttoxmlconverter update user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode xml year





