We help IT Professionals succeed at work.

Datagridview with null row

anusdesai
anusdesai asked
on
780 Views
Last Modified: 2009-07-29
Hi,

I have a datagridview bound to datatable.
I have coded
datagridview.allowusertoaddrows=false

But,still i get a blank row at the end of all rows in datagridview.

My code is:


        Dim arr As New ArrayList

        Dim j As Integer = 0      
   
        Dim strFileName As String
        strFileName = "C:\Files\Pay_Aug.xls"
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        MyConnection = New System.Data.OleDb.OleDbConnection( _
               "provider=Microsoft.Jet.OLEDB.4.0; " & _
               "data source=" + strFileName + "; " & _
               "Extended Properties=""Excel 8.0;""")
        Try

            MyConnection.Open()        

            Dim x As String
            x = Me.cmbSheetName.Text

            Dim Sqlstring As String = "select * from [" & x & "]"

            Dim MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
                 Sqlstring, MyConnection)
            MyCommand.Fill(dt)
           
            DataGridView1.DataSource = dt


        Catch ex As Exception

            MsgBox(ex.ToString)
        Finally
            MyConnection.Close()


        End Try

How can I get rid of the null row....

Regards.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi

set following grid Property

datagridview.ReadOnly=true

thanks,
ahmad

Commented:
Readonly will stop a new record being added, but won't stop the empty row being shown.

If there is a BindingSource involved, set .AllowNew to False in that.

Roger

Author

Commented:
Hi Sancler ,

Can you please give me the syntax....

Regards.

Author

Commented:
Hi again,

I figured it out..its for dataview but,could you give an suggestion for datatable.


Regards.

Commented:
Sorry, I see that there is NOT a BindingScource involved.  I'd missed this line

            DataGridView1.DataSource = dt

in which the binding is direct.

You say

>>
I have coded
datagridview.allowusertoaddrows=false
<<

That should work.  Where did you put that code, and what did it say?  Try putting this

            DataGridView1.AllowUserToAddRows = False

immediately after

            DataGridView1.DataSource = dt

Roger

Author

Commented:

Hi Roger,

Thanks for that..but,unfortunately its not working....
 DataGridView1.DataSource = dt
            DataGridView1.AllowUserToAddRows = False

Can u suggest?


Regards.

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you have a DataTable for a datasource, then you could do something like this:

   dt.DefaultView.AllowNew = False

Bob

Commented:
I've just tested this.  It's an adaptation of something I recently used for something else.

Public Class Form1

    Private dt As New DataTable("TestTable")

    Private Sub fillTable() 'equivalent to using dataadapter.fill
        Dim dc1 As New DataColumn("Hours", GetType(Decimal))
        dt.Columns.Add(dc1)
        Dim dc2 As New DataColumn("Rate", GetType(Decimal))
        dt.Columns.Add(dc2)
        For i As Integer = 1 To 6
            Dim dr As DataRow = dt.NewRow
            dr(0) = i + 0.5
            dr(1) = i * 0.5
            dt.Rows.Add(dr)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        fillTable()
        dgv.DataSource = dt
        dgv.AllowUserToAddRows = False
    End Sub

End Class

It works OK for me.  Just make a new form with a datagridview, called dgv, on it; copy the above code into the code window for it; and run it.  If it works OK for you, then there's something else going on in your code.

Have you customised your datagridview at all - either at design time or anywhere else in your code?

Roger

Author

Commented:
Hi,

It works fine when i create a new application.
But not on my application..

What I am trying to do is get values from a specific column  of datagridview in arraylist and populate it in listbox...
now, when the value of that column gets dbnull my Catch block activates and i get error cannot convert dbnull to string
and in return i dont get array of the required values of the column ....
Can u suggest something?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Roger,

Cool...works gr8!!!!

Cheers.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.