problem with DataGridView control  
Author Message
SHISHIR WAHI





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

Is there a way to restrict the number of rows a user can add in a datagridview control

e.g when the user had entered 2 records in the grid, a message should popup saying "You can't add more than 2 records".

Please help.




Windows Forms21  
 
 
DMan1





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

You need to declare your dataset outside of the procedure so that both procedures can use the same instance of your datset

 
 
Badri Narayanan





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

Check the RowState property of GridViewRow to if it was Inserted. Iterate the GridView rows and check the RowState of each row. Maintain a counter (and increment) when the row has Inserted state. When counter has reached 2 then you can disable adding more rows.

HTH


 
 
mastan





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

Than Q for Giving Solution, i have changes the Procedure like this.

Public conn As New OleDbConnection("provider= Microsoft.Jet.OleDb.4.0; data source=ss.mdb")

Dim myada As New OleDbDataAdapter("SELECT * FROM CUSTOMER", conn)

Dim myds As New DataSet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

myada.Fill(myds, "Customer")

DataGridView1.DataSource = myds.Tables("Customer")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

myada.Update(myds, "Customer")

MsgBox("Updated...")

End Sub

Now i am getting error like "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."

Please Help.........



 
 
DMan1





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

You need to set the command and command text specifically of the adapter...

myada.UpdateCommand.CommandText = "UPDATE Customer SET TheName = 'AName'"

Is a simple update command...however more realistically you will need to use parameters and the following syntax...

"UPDATE Customer SET Name, Field2, Field3 VALUES ( , , )"

myada.UpdateCommand.Parameters.Add("CustomerName")...etc...



 
 
mastan





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

Than Q

Let us assume Customer Table Contain CustomerID,CustomerName fields.Please Provide Code Snippet for update in Datagridview , iam new to vb.net

Please Help.... i have been trying since 4 hours....



 
 
ReneeC





PostPosted: Windows Forms Data Controls and Databinding, problem with DataGridView control Top

 

Here's a favorite trick of mine. I used this routine a lot in ADO work.

 

Public Function PersistTable(ByVal Table As DataTable, Optional ByVal NewRow As DataRow = Nothing) As Boolean

        ' adodb.PersistTable - called by any routine needing to make permanent changes in a table.

        ' Usually this would be midlevel procedures in IOSUBS

        ' PersistTable has an optional argument for appending a new row on a table.

        ' con is a connection instantiated elsewhere. And so is the DataAdapter"

       

        Adapter.SelectCommand = New OleDbCommand("Select * from [" & Table.TableName & "]", con)

        Dim builder As New OleDbCommandBuilder(Adapter)

        builder.QuotePrefix = "["

        builder.QuoteSuffix = "]"

 

        If NewRow IsNot Nothing Then Table.Rows.Add(NewRow)

 

        Try

            Adapter.Update(Table)

            Return True

        Catch e As Exception

            'Throw e

            MsgBox("Error persisting Table: " + Table.TableName + vbCrLf + "Exception was: " + e.Message, _

                    MsgBoxStyle.Information, "ADONET.PersistTable")

            Return False

        End Try

    End Function

 

Now then, this routine greatly simples the syntax to updating a table"

 

'update the rows....

 

PersistTable(Table) ' and there you have it. Don't leave home without it.