Hey everyone. I know this is probably a stupid question but it's been bugging me for several days now and I could really use some help. OK. I have a DataGridView on a windows form listing student names, majors, etc. If the user double-clicks on that datagrid, a separate form opens up with the fields for the user to be able to edit that record. Here is the code I use to fill the datagridview (on the main form):
Public Sub GetStudentList() Dim builder As New SqlConnectionStringBuilder()builder.ConnectionString = strDBConn
dsStudents.Clear()
Using connection As New SqlConnection(builder.ConnectionString) Dim command As New SqlCommand("admin_sp_GetStudentList", connection)command.CommandType = CommandType.StoredProcedure
Tryconnection.Open()
Using drStudents As SqlDataReader = command.ExecuteReader()dsStudents.Load(drStudents, LoadOption.OverwriteChanges,
New String() {"Students"}) End UsingFormatStudents()
Catch ex As ExceptionMessageBox.Show(ex.Message)
Finally If connection.State <> ConnectionState.Closed Thenconnection.Close()
End If End TryEnd Using
End Sub
On the second form, I have the following stuff:
Private
callingForm As frmMainPublic
Sub New(ByVal newCallingForm As frmMain) MyBase.New()InitializeComponent()
callingForm = newCallingForm
End Sub Private Sub frmStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadAcceptButton = btnAddEdit
CancelButton = btnCancel
FillStudentControls()
End SubPrivate Sub FillStudentControls()
Cursor = System.Windows.Forms.Cursors.WaitCursor
Dim dv As DataView = frmMain.dsStudents.Tables(0).DefaultViewdv.RowFilter =
"studID = " & intStudIDtxtStudFName.Text = dv(0)(
"studFName").ToStringtxtStudLName.Text = dv(0)(
"studLName").ToStringtxtEmail.Text = dv(0)(
"studEmail").ToString' - more fields (there are 10 textboxes on this form plus 2 comboboxes)
txtStudFName.Focus()
dv.RowFilter =
"studID > 0"Cursor = System.Windows.Forms.Cursors.Default
End SubI'm not sure if using a dataview is the best approach but it's the only one I know of. Unfortunately what is happening is this. Because it (the dataview) is using the same dataset as the datagridview, when the new form is loaded, the datagridview on the main form is getting redrawn (and have the first row selected). That is why I added that second to last line in the "FillStudentControl" procedure (to make the datagridview still show all the Students and not just the one that is selected .
How would I go about NOT having the datagridview on the main form redraw itself when the other form pops up Should I do it differently (not using the dataview on the second form) If so, how do I go about getting the fields from the dataset that I need
Sorry if this is all newbie 101 questions but that is kind of what I am. So any and all help would be greatly appreciated. Oh, and for the record, this is not a school project or anything. I am building this little program just to teach myself VB/SQL Server/ etc. I don't know if that matters but another post I made on a different board was discarded because the moderator thought I was asking for help with my homework :).

Stop DataGridView from re-drawing itself?
kothari_ronak
That's exactly what was happening. I added:
If
dgvStudents.ColumnCount > 0 ThendgvStudents.Columns.Clear()
End Ifand it working perfectly. As I said, I don't know if this is the best approach/method for handling this but it is at least working now :) If there is a better way to do this I am all ears. I'm trying to do everything at runtime so dragging/dropping is not something I'd want to do unless necessary. But if anyone has any other suggestions for a listing (datagridview) on one form and details on the other, please don't hesitate to post.
Thanks for the suggestions on what could be causing the problem. Once I read your post it made sense (but that kind of sense that I wouldn't have thought of on my own because I was locked up in what I thought was causing the problem and not looking at the whole picture).
So thank you for that.
You guys rock (all-gender inclusive, of course)
Rakesh_24
pieter.vp
Well, my FormatStudents() procedure adds the columns required after the dataset is filled from the stored proc. So I'm clearing the dataset but never clearing the actual columns from the datagridview. So maybe, after filling the dataset (the second time around), it is creating 2 columns for each field (one original column and one "new" added column) So I need to do a column count check and see if there are any, then delete them I'll give it a shot.
Thank you for the suggestion. I've tried counting rows in the dataset but I never thought about the fact that I was "adding" the columns all over again. I thought it was a problem with the dataset just appending itself and not actually clearing.
Thank you again for that. I'll see if it works and let everyone know :)
Gabe19
Sorry. After a little more testing I found there is another problem. Once I close the second form (the details form), I go back to the original form (with the datagridview). The datagridview is "redrawing" itself (sideways). So, instead of having, for example, 5 columns and 50 rows, it'll have 10 columns and 50 rows. It is basically making a duplicate of itself right next to itself. I've got the datagridview formatted so that the last column fits in perfectly so initially I missed what it was doing. But then I noticed the horizontal scroll bar and then I saw the error.
On the second form, the user either "updates" the record or "cancels". If they hit update, a stored procedure is run to update the database. If they cancel, it simply closes the form. This is the code that is used to open (and then redraws the datagridview) the second form:
Private
Sub ShowStudentDetails() Dim fStudent As New frmStudentDetails(Me)Cursor = System.Windows.Forms.Cursors.WaitCursor
fStudent.ShowDialog()
Cursor = System.Windows.Forms.Cursors.Default
fStudent =
Nothing GetStudentList() Me.BringToFront() End SubI didn't want to have to re-fill the datagrid if there was a way around that (well, a simple way that a newbie like me would be able to understand). But the only thing I could think of was to recall the procedure that queries the d/b, fills the dataset, and then formats the datagridview. That's also why I put the "dsStudents.clear" at the beginning of the GetStudentList procedure (to make sure it used the most recent information from the database).
Any idea's on what would cause the double-datagridview problem and how I would go about fixing it I've been working on it since Friday and have spent way too much money on Excedrin :)
Thanks in advance for any and all help that you can give. It is mucho appreciated.
Cyber
That was it. Thank you very much for that. Now it is working perfectly.
Thanks again! :)
softwater