Click here to Skip to main content
15,923,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to VB.net, mostly VB6 in the past, so be gentle (trying to change).

I have a data grid view that has a databindings to a data base.

Some of the data needs to be displayed with a combo box for editing.

i.e. First 5 Columns are TextBox Columns, 6th Column is a ComboBox Column etc

So in my wisdom (not much obviously, as nothing really worked as planned) I design the layout of the DataGridView programmatically to have this set out on Load (and I get the 6 Columns as planned with the sixth being a ComboBox) and then I bind data to it.

But....

The data gets appended at the end of the 6 columns with 6 new text box columns using the data from the databinding. Hence I now have 12 columns....

What is the procedure to DataBinding the data but deciding how the interface should look....

I.e. Use the first 6 Columns I have design in the DataGridView and append the data.....

I really appriceate your help, and thank any one in advance....


Stephan
Posted
Comments
rbnsubedi 3-Jul-11 11:55am    
what you have tried or plz provide some snippet of code

hey! be more specific while asking question, I am not able to understand what are you trying so please give more information. if possibly embed some code in your question whatever you can use given code to bind datagridview to your table.
Try
VB
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=yourdatabase;Integrated Security=True"
        Dim sql As String = "SELECT * FROM tablename"
        Dim connection As New SqlConnection(connectionString)
        Dim dataadapter As New SqlDataAdapter(sql, connection)
        Dim ds As New DataSet()
        connection.Open()
        dataadapter.Fill(ds, "tablename")
        connection.Close()
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "tablename"
    End Sub
End Class
 
Share this answer
 
So imagine the above code. (because that's what I am doing to bind the data).

But now imagine that the query returns 6 columns of data. Using the aboce code will add six columns of TextBoxColumns.

But I would no like to change the 5th Column to a ComboBoxColumn.

Do I do this before binding the data or after binding the data?

So far I have added the 6 columns (including the ComboBoxColumn) by programmmtically adding each column on load. Then bind the data, but after binding the data I have 12 columns. (Its like it appends the 6 new column from the binding).

Hope that makes sense.

Stephan
 
Share this answer
 
Comments
Alan N 3-Jul-11 20:52pm    
Well as you haven't shown your code and I can't be sure that I'd imagine what you'd imagine but the most likely reason for six extra columns is that the AutoGenerateColumns property of the grid is set to true. Don't go looking for it in the VS designer properties window as it's one of the specials that doesn't show up there!

Alan.
StephanGumpert 3-Jul-11 21:17pm    
Alan you hit the nail right on the head. Spot on.
I do however have another question.

My extra column dont appear which is good.
The grid now expands to the number of rows that are requred but no data appears in the rows, the rows are blank.

Code Used to add the data as follows:
(Is this correct?)

BindingsSourceObj.DataSource = PartDataSet
BindingsSourceObj.DataMember = PartDataSet.Tables(0).Table

MyDataGridView.DataSource = BindingsSourceObj


As I said If the AutoGenerateColumns = True the Data comes in, but when the AutoGenerateColumns = False rows are there but content is blank.

Stephan
StephanGumpert 3-Jul-11 21:37pm    
Seams I solved it by my self.... too quick to ask the question.

I simply need to add the DataPropertyName to each column and bingo all is good.

Stephan

Many Thanks guys.

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