ERROR Unable to Programmatically Add Row to DataGridView

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
Hi
I have a DataGridView Maintenance screen in which I think I was just getting there when I tried to Add Rows of Data. See

VB.NET:
        With QuestionAnswerData.Tables("Questions")
            .Columns.Add("QuestionID", GetType(Integer))
            .Columns.Add("QuestionNo", GetType(String))
            .Columns.Add("Question", GetType(String))
            .Columns.Add("AnswerSection", GetType(String))
            .Columns.Add("AnswerSectionColor", GetType(String))
            .Columns.Add("AnswerImage", GetType(String))
            .Columns.Add("AnswerHyperlink", GetType(String))
            .Columns.Add("AnswerRTFFile", GetType(String))
        End With

My Screen is :

DataGridView_Shot.jpg

I am using the following code to Add Rows of Data.

VB.NET:
    Private Sub btnAddQuestion_Click(sender As Object, e As EventArgs) Handles btnAddQuestion.Click
        If Me.txtBoxQuestionNo.Text = "" Then
            MsgBox("Error, You must Enter a Question No, remaining fields are optional", vbCritical, "Add Question")
        Else
            Me.DataGridView1.Rows.Add(Me.txtBoxQuestionNo.Text, Me.txtBoxQuestion.Text, txtBoxSection.Text, Me.txtBoxSectionColour.Text, Me.txtBoxImage, txtBoxHyperlink)
            'Me.DataGridView1.Refresh()
        End If
    End Sub

It is because I am using : DataGridView1.DataSource = QuestionAnswerData.Tables(0)

I have tried a few ways, DataGridView1.DataSource = QuestionAnswerData.Tables(0)
and Me.DataGridView1.DataSource = Me.BindingSource1

The Error I am getting is that it says I am NOT allowed to Programmatically Add Rows of Data when the data is bound ?

The reason I have to "Add Rows" Programmatically is so that I can have validation and ensure there is at least some data in the 1st key Column.
I may have to restort to using as ListViewBox which has its limitations....

Can anyone offer a way forward.
Thanks Kuldip.
 
Hi,

What did you find when you tried to search the Internet for an answer to your question? I just did a quick search and the very first result, even though it is a reference to C#, would have told you what the issue is.

So, now that you know what the issue is have a go at writing the very easy fix.

Cheers,

Ian
 
Indeed Yes, thank you, sometimes when you in the depths of programming one has a momentary lapse and forget to do this obvious, but found the solution in the end.

VB.NET:
    Private Sub btnAddQuestion_Click(sender As Object, e As EventArgs) Handles btnAddQuestion.Click
        If Me.txtBoxQuestionNo.Text = "" Then
            MsgBox("Error, You must Enter a Question No, remaining fields are optional", vbCritical, "Add Question")
        Else
            'Me.DataGridView1.Rows.Add(Me.txtBoxQuestionNo.Text, Me.txtBoxQuestion.Text, txtBoxSection.Text, Me.txtBoxSectionColour.Text, Me.txtBoxImage, txtBoxHyperlink)
            Dim newQuestionsRow As DataRow = QuestionAnswerData.Tables("Questions").NewRow()

            newQuestionsRow("QuestionNo") = Me.txtBoxQuestionNo.Text
            newQuestionsRow("Question") = Me.txtBoxQuestion.Text
            newQuestionsRow("AnswerSection") = txtBoxSection.Text
            newQuestionsRow("AnswerSectionColor") = Me.txtBoxSectionColour.Text
            newQuestionsRow("AnswerImage") = Me.txtBoxImage.Text
            newQuestionsRow("AnswerHyperlink") = Me.txtBoxHyperlink.Text
            QuestionAnswerData.Tables("Questions").Rows.Add(newQuestionsRow)
            Me.DataGridView1.Refresh()
        End If
    End Sub

Took a little while to work it out in .Net but Working Nicely... Kuldip.
 

Latest posts

Back
Top