Adding rows to datagrid view

ecaf

Member
Joined
Dec 1, 2008
Messages
20
Programming Experience
1-3
Hoping someone can help me on this, I have the AllowUserToAddRows properties on the datagridview set to True.
The problem seems to be that on the SQL table the 2nd column in the table is "Allow Null" = False.
If I go to add a new row, you can type in the details in the first column, but when you go to tab to the next column which doesn't allow the nulls, it seems to try and add the row first and then moves to the next column, but then gives an error about not allowing rows in the 2nd column.

I have found a way around this is to go to a new row and type in the 2nd column first, this adds a new row with the other columns are blank in it, then you essentially edit the new row to add the remainder of the column values.

I have tried a function I found on MSDN which uses the DataGridView.Columns.DisplayIndex = 0 / 1 / 2... etc for each of the columns in the table. I thought this way it would use my workaround but force the user to type the 2nd column first by displaying it in DisplayIndex 0.
This function didn't actually rearrange the columns unfortunately, so I couldn't test it. Code is below in case it helps:
VB.NET:
Private Sub AdjustColumnOrder()

        With DataGridView2
            '.Columns("CustomerID").Visible = False
            .Columns("RefCode").DisplayIndex = 0
            .Columns("RefType").DisplayIndex = 1
            .Columns("RefDescription").DisplayIndex = 2
            .Columns("Active").DisplayIndex = 3
        End With

    End Sub

I think it might be best if I could stop the row from being added until the user fills in all of the first before clicking on an ADD / UPDATE button, because there could be a case that a table has more than one field with Allow Nulls set to False.

Does anyone know how to stop it adding the row until a button has been clicked, but still enabling the AllowUserToAddRows = True, so that the user doesn't have to fill out text boxes?
Or if it can't be done that way, do you put in bindingsource.addnew first and then bingdingsource.update? Do you have to bind the text boxes to the datasource?
Thanks.
 
First up, please post your questions in the most appropriate forum for the topic, not just the first one you come to. Anyone who takes the time to read the forum descriptions can find the most appropriate forum so, after 18 posts, we shouldn't have to tell you this. Thread moved.

As for the question, either you are mistaken about what's happening, your own code is causing that problem or there's something seriously wrong with your project because that's not how the DataGridView works. The row is not added to the underlying DataTable until you navigate away from the grid row. I've attached a project to demonstrate this. As you can see from that project, no error message is displayed unless you try to leave the new row without entering a value for the middle column.
 

Attachments

  • DataGridViewNullDemo.zip
    14.3 KB · Views: 26
Thank you for your response jmcilhinney, and I apologise for posting in the wrong section, but I really didn't think it would go in Windows forms as it is a web application. I have been thrown in at the deep end a little here in that I'm not technically a programmer but doing my best to do a job as one, I'm learning a lot of new technology as I go along, and it can be quite daunting to post a question here, and to try and include as much detail as possible, never mind try to choose the correct form to post on.

In relation to your answer, I am not doing the exact same thing as the way you have in the code you provided. I will try and give you as much info. as possible, but it seems to be done differently, this is a project I have taken from someone else to complete, the come from a C++ background and this is in VB.Net. I have made some modifications to include more tables than original.

It is a series of tabs, so when each tab is clicked the datagrid is populated by a different table adapter, the code from the Select Case TabControl1.SelectedIndex is below:
VB.NET:
Case 1
                BindingSource1.DataMember = "tPriority"
                TPriorityTableAdapter1.Fill(MaintDataSet1.tPriority)
And it is something similar for each of the tabs.

Now when I say it writes the row to the database after I tab to the next field that's what it does seem to do. This is how I came to that conclusion: The asterisk symbol * is beside the start of a new row, so I type in the first column and then press tab, straight away, a new line below appears, but the 2nd column in the row I was adding (above the new line) is still highlighted.
I try to continue with the new row I was adding (which is now the 2nd last line). I type a code into the 2nd column, and then press tab to move to the 3rd column. At this stage it highlights the 3rd column, but then pops up another webpage with the error message: "Column 'RefCode' does not allow nulls" - this is because of the SQL constraints I mentioned previously.

Maybe I am mistaken, and the line hasn't been written to the database yet, but it seems to attempt to write it because it gives out about the column not allowing nulls.

Either way, I need to find where in the code it is doing this?
One thing I have just noticed is that it (the datagridview) is a Gizmox.WebGui.Forms.DataGridView control, where as yours is a System.Windows.Forms.DataGridView control. This is probably where the problem is with some auto generated code???


Thanks again for response so far, and I apologise again for disturbance caused by posting in the wrong thread, it was not intentional or laziness.
 
If you say "DataGridView" then everyone is going to assume System.Windows.Forms.DataGridView. There's nothing in your first post to suggest that it's a web app or that you're using a third-party grid. Everything suggests WinForms, including the fact that you're using a System.Windows.Forms.BindingSource. There's no reason to ever use a BindingSource in a web app. As the namespace suggests, it's made for WinForms.

I've moved your thread to the Web Forms forum. I have no idea how your grid works as I've never heard of it, let alone used it. Maybe some other WebForms developer will know more.
 
I only realised that when I compared your code and project with the one I'm working on. I would have said so otherwise.
 
Back
Top