Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Columncount property cannot be set on a data-bound datagridview control. In VB.NET

What I have tried:

con = New SqlConnection(conn)
       ds = New DataSet()
       da = New SqlDataAdapter("select Cid,Name,Gender,dob,RationNo,PanId,voteId,Address,Phone,EmailId,NomineeStatus,photo from CustomerReg where statuss=1 ", con)
       If ds.Tables(0).Rows.Count > 0 Then

           DataGridView1.AutoGenerateColumns = False
           DataGridView1.ColumnCount = 12
           DataGridView1.Columns(0).Name = "cid"
           DataGridView1.Columns(0).HeaderText = "Customer ID"
           DataGridView1.Columns(0).DataPropertyName = "Cid"

           DataGridView1.Columns(1).Name = "Name"
           DataGridView1.Columns(1).HeaderText = "Customer Name"
           DataGridView1.Columns(1).DataPropertyName = "Name"

           DataGridView1.Columns(2).Name = "Gender"
           DataGridView1.Columns(2).HeaderText = "Gender"
           DataGridView1.Columns(2).DataPropertyName = "Gender"

           DataGridView1.Columns(3).Name = "DOB"
           DataGridView1.Columns(3).HeaderText = "DOB"
           DataGridView1.Columns(3).DataPropertyName = "DOB"

           DataGridView1.Columns(4).Name = "RationNO"
           DataGridView1.Columns(4).HeaderText = "Ration Card No"
           DataGridView1.Columns(4).DataPropertyName = "RationNO"

           DataGridView1.Columns(5).Name = "PanId"
           DataGridView1.Columns(5).HeaderText = "Pancard ID"
           DataGridView1.Columns(5).DataPropertyName = "PanId"

           DataGridView1.Columns(6).Name = "voteid"
           DataGridView1.Columns(6).HeaderText = "Voters ID"
           DataGridView1.Columns(6).DataPropertyName = "voteid"

           DataGridView1.Columns(7).Name = "Address"
           DataGridView1.Columns(7).HeaderText = "Address"
           DataGridView1.Columns(7).DataPropertyName = "Address"

           DataGridView1.Columns(8).Name = "Phone"
           DataGridView1.Columns(8).HeaderText = "Phone"
           DataGridView1.Columns(8).DataPropertyName = "Phone"

           DataGridView1.Columns(9).Name = "EmailId"
           DataGridView1.Columns(9).HeaderText = "Email ID"
           DataGridView1.Columns(9).DataPropertyName = "EmailId"

           DataGridView1.Columns(10).Name = "NomineeStatus"
           DataGridView1.Columns(10).HeaderText = "Nominee Status"
           DataGridView1.Columns(10).DataPropertyName = "NomineeStatus"

           DataGridView1.Columns(11).Name = "Photo"
           DataGridView1.Columns(11).HeaderText = "Photo"
           DataGridView1.Columns(11).DataPropertyName = "Photo"

           DataGridView1.DataSource = ds.Tables(0)

       End If
   End Sub

   Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
       ' Dim sts As String
       txtID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString()
       txtName.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString()
       sex = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString()
       If sex = "M" Then
           rbtmale.Checked = True
           rbtfmale.Checked = True

       End If
       DateTimePicker1.Value = DataGridView1.Rows(e.RowIndex).Cells(3).Value.ToString()
       txtrcno.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value.ToString()
       txtpcno.Text = DataGridView1.Rows(e.RowIndex).Cells(5).Value.ToString()
       txtvid.Text = DataGridView1.Rows(e.RowIndex).Cells(6).Value.ToString()
       txtaddress.Text = DataGridView1.Rows(e.RowIndex).Cells(7).Value.ToString()
       txtphone.Text = DataGridView1.Rows(e.RowIndex).Cells(8).Value.ToString()
       txtemail.Text = DataGridView1.Rows(e.RowIndex).Cells(9).Value.ToString()
       sts = DataGridView1.Rows(e.RowIndex).Cells(10).Value.ToString()
       If sts = "Y" Then
           CheckBox1.Checked = True
           CheckBox1.Checked = False

       End If
       fileName = DataGridView1.Rows(e.RowIndex).Cells(11).Value.ToString()
       PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
       PictureBox1.ClientSize = New Size(177, 197)
       PictureBox1.Image = Image.FromFile(fileName)
Updated 12-Jun-20 21:48pm
Maciej Los 13-Jun-20 2:51am    
Not a question at all!

1 solution

The error is pretty explicit:
Columncount property cannot be set on a data-bound datagridview control
Ant what it means is: if your DGV has a DataSource, you cannot control the number of columns because the source requires a specific number.
So if you want to do "special things" with a databound DGV, you need to remove any existing binding first, by setting the DataSource property to Nothing.
DataGridView1.DataSource = Nothing
DataGridView1.AutoGenerateColumns = False
DataGridView1.ColumnCount = 12
DataGridView1.Columns(11).DataPropertyName = "Photo"

DataGridView1.DataSource = ds.Tables(0)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900