i have 3 columns in a datagridview.
i want to add values.
everything is working fine but when i add values there is a blank row at the beginning.
how can i remove this ?
here is my code

dataGridView2.Rows.Clear()

        Dim oledbcom As New OleDb.OleDbCommand
        oledbcom.CommandText = "Select Code,Weight,Unit from Contents where id = ? and [combination name] = ?"
        oledbcom.Connection = oledbcon

        oledbcom.Parameters.Add("?", OleDb.OleDbType.VarChar)
        oledbcom.Parameters.Add("?", OleDb.OleDbType.VarChar)

        oledbcom.Parameters(0).Value = dataGridView1.Item(0, dataGridView1.CurrentRow.Index).Value
        oledbcom.Parameters(1).Value = lstColour.SelectedItem.ToString

        oledbcon.Open()
        Dim oledbreader As OleDb.OleDbDataReader = oledbcom.ExecuteReader

        While oledbreader.Read
            dataGridView2.Rows.Add()
            dataGridView2.Item(0, dataGridView2.Rows.Count - 1).Value = oledbreader.GetString(0)
            dataGridView2.Item(1, dataGridView2.Rows.Count - 1).Value = oledbreader.GetValue(1)
            dataGridView2.Item(2, dataGridView2.Rows.Count - 1).Value = oledbreader.GetString(2)
        End While
        oledbcon.Close()

Recommended Answers

All 3 Replies

babbu,
Set
dataGridView2.AllowUserToAddRows = False.

Why not DataAdapter?

.....
        Dim oledbcom As New OleDb.OleDbCommand
        oledbcom.CommandText = "Select Code,Weight,Unit from Contents where id = ? and [combination name] = ?"
        oledbcom.Connection = oledbcon

        oledbcom.Parameters.Add("?", OleDb.OleDbType.VarChar)
        oledbcom.Parameters.Add("?", OleDb.OleDbType.VarChar)

        oledbcom.Parameters(0).Value = dataGridView1.Item(0, dataGridView1.CurrentRow.Index).Value
        oledbcom.Parameters(1).Value = lstColour.SelectedItem.ToString
 
        Dim adp As new OleDb.OleDbDataAdapterReader(oledbcom)
        Dim dt as new System.Data.DataTable
        adp.Fill(dt)
        dataGridView2.AllowUserToAddRows = False
        dataGridView2.DataSource=Dt
        .....

thnx a lot
especially for that data adapter thing
i tried using it first but couldnt figure out how to add parameters.
u solved the problem. thnx

Thanks,
Mark this thread as "Solved" if you get solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.