how to bind specific sql colums in datagrid in vb.net?












0















i am working on important project where i want to show specific columns from sql database into datagridview when i search by itemcode in textbox. i have written a code but it is showing error. can somebody help me?



my code is here



Public Class Form8

Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
adapter = New SqlDataAdapter("select * from stockdata", constr)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
bs.DataSource = dt
DataGridView1.DataSource = bs


dt.Clear()
adapter.SelectCommand.CommandText = "select * from customerdata where itemcode = @itemcode"

adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)




'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
DataGridView1.DataSource = dt




End Sub









share|improve this question

























  • You need to include the error you are receiving.

    – Marie
    Nov 21 '18 at 18:16











  • System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

    – pankaj babbar
    Nov 21 '18 at 18:23











  • if i remove DataGridView1.ColumnCount = 3 then it add only blank row

    – pankaj babbar
    Nov 21 '18 at 18:25











  • You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

    – Marie
    Nov 21 '18 at 18:27


















0















i am working on important project where i want to show specific columns from sql database into datagridview when i search by itemcode in textbox. i have written a code but it is showing error. can somebody help me?



my code is here



Public Class Form8

Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
adapter = New SqlDataAdapter("select * from stockdata", constr)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
bs.DataSource = dt
DataGridView1.DataSource = bs


dt.Clear()
adapter.SelectCommand.CommandText = "select * from customerdata where itemcode = @itemcode"

adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)




'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
DataGridView1.DataSource = dt




End Sub









share|improve this question

























  • You need to include the error you are receiving.

    – Marie
    Nov 21 '18 at 18:16











  • System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

    – pankaj babbar
    Nov 21 '18 at 18:23











  • if i remove DataGridView1.ColumnCount = 3 then it add only blank row

    – pankaj babbar
    Nov 21 '18 at 18:25











  • You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

    – Marie
    Nov 21 '18 at 18:27
















0












0








0








i am working on important project where i want to show specific columns from sql database into datagridview when i search by itemcode in textbox. i have written a code but it is showing error. can somebody help me?



my code is here



Public Class Form8

Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
adapter = New SqlDataAdapter("select * from stockdata", constr)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
bs.DataSource = dt
DataGridView1.DataSource = bs


dt.Clear()
adapter.SelectCommand.CommandText = "select * from customerdata where itemcode = @itemcode"

adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)




'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
DataGridView1.DataSource = dt




End Sub









share|improve this question
















i am working on important project where i want to show specific columns from sql database into datagridview when i search by itemcode in textbox. i have written a code but it is showing error. can somebody help me?



my code is here



Public Class Form8

Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
adapter = New SqlDataAdapter("select * from stockdata", constr)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
bs.DataSource = dt
DataGridView1.DataSource = bs


dt.Clear()
adapter.SelectCommand.CommandText = "select * from customerdata where itemcode = @itemcode"

adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)




'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
DataGridView1.DataSource = dt




End Sub






vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:12









Mary

3,1622718




3,1622718










asked Nov 21 '18 at 17:58









pankaj babbarpankaj babbar

157




157













  • You need to include the error you are receiving.

    – Marie
    Nov 21 '18 at 18:16











  • System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

    – pankaj babbar
    Nov 21 '18 at 18:23











  • if i remove DataGridView1.ColumnCount = 3 then it add only blank row

    – pankaj babbar
    Nov 21 '18 at 18:25











  • You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

    – Marie
    Nov 21 '18 at 18:27





















  • You need to include the error you are receiving.

    – Marie
    Nov 21 '18 at 18:16











  • System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

    – pankaj babbar
    Nov 21 '18 at 18:23











  • if i remove DataGridView1.ColumnCount = 3 then it add only blank row

    – pankaj babbar
    Nov 21 '18 at 18:25











  • You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

    – Marie
    Nov 21 '18 at 18:27



















You need to include the error you are receiving.

– Marie
Nov 21 '18 at 18:16





You need to include the error you are receiving.

– Marie
Nov 21 '18 at 18:16













System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

– pankaj babbar
Nov 21 '18 at 18:23





System.InvalidOperationException: 'ColumnCount property cannot be set on a data-bound DataGridView control.'

– pankaj babbar
Nov 21 '18 at 18:23













if i remove DataGridView1.ColumnCount = 3 then it add only blank row

– pankaj babbar
Nov 21 '18 at 18:25





if i remove DataGridView1.ColumnCount = 3 then it add only blank row

– pankaj babbar
Nov 21 '18 at 18:25













You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

– Marie
Nov 21 '18 at 18:27







You should edit your question to include the error. It makes it easier for future people having issues. I am not a Forms dev so I cant be sure but it looks like you are clearing your datatable then setting a new command but you never execute the new command. Maybe you need to execute it and refil your dt? You are creating an adapter, executing a command, and immediately clearing the result too. You probably should eliminate the bits you dont need to make it easier to debug.

– Marie
Nov 21 '18 at 18:27














1 Answer
1






active

oldest

votes


















0















  1. The constructor of a DataAdapter takes a string and a connection as arguments. `New SqlDataAdapter(string, SqlConnection). You have passed in a string (constr) not a connection.

  2. Only retrieve the data you need, not all the fields with "*".

  3. Set up your DataGridView before you bind it.

  4. Not sure what itemcode is doing in a customerdata table. Seems like a database design issue.

  5. It seems you are setting the DataSource property twice.


I am not good at DataGridView (where is @Plutonix when I need him) so this could need some tweaking.



Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim cn As New SqlConnection(constr)
'Get the real column names from your database for StockId, ItemCode and Item
adapter = New SqlDataAdapter("Select StockId, ItemCode, Item From stockdata Where itemcode = @itemcode;", cn)
adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
'DataGridView1.DataSource = dt
bs.DataSource = dt
DataGridView1.DataSource = bs
End Sub





share|improve this answer
























  • your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

    – pankaj babbar
    Nov 21 '18 at 18:56











  • @pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

    – Mary
    Nov 21 '18 at 19:01











  • ok thanks for helping me.

    – pankaj babbar
    Nov 21 '18 at 19:04











  • Why the downvote? I like to know where my errors are.

    – Mary
    Nov 21 '18 at 22:45











  • Oh sorry i have upvoted. It is by mistake

    – pankaj babbar
    Nov 22 '18 at 4:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418035%2fhow-to-bind-specific-sql-colums-in-datagrid-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















  1. The constructor of a DataAdapter takes a string and a connection as arguments. `New SqlDataAdapter(string, SqlConnection). You have passed in a string (constr) not a connection.

  2. Only retrieve the data you need, not all the fields with "*".

  3. Set up your DataGridView before you bind it.

  4. Not sure what itemcode is doing in a customerdata table. Seems like a database design issue.

  5. It seems you are setting the DataSource property twice.


I am not good at DataGridView (where is @Plutonix when I need him) so this could need some tweaking.



Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim cn As New SqlConnection(constr)
'Get the real column names from your database for StockId, ItemCode and Item
adapter = New SqlDataAdapter("Select StockId, ItemCode, Item From stockdata Where itemcode = @itemcode;", cn)
adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
'DataGridView1.DataSource = dt
bs.DataSource = dt
DataGridView1.DataSource = bs
End Sub





share|improve this answer
























  • your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

    – pankaj babbar
    Nov 21 '18 at 18:56











  • @pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

    – Mary
    Nov 21 '18 at 19:01











  • ok thanks for helping me.

    – pankaj babbar
    Nov 21 '18 at 19:04











  • Why the downvote? I like to know where my errors are.

    – Mary
    Nov 21 '18 at 22:45











  • Oh sorry i have upvoted. It is by mistake

    – pankaj babbar
    Nov 22 '18 at 4:14
















0















  1. The constructor of a DataAdapter takes a string and a connection as arguments. `New SqlDataAdapter(string, SqlConnection). You have passed in a string (constr) not a connection.

  2. Only retrieve the data you need, not all the fields with "*".

  3. Set up your DataGridView before you bind it.

  4. Not sure what itemcode is doing in a customerdata table. Seems like a database design issue.

  5. It seems you are setting the DataSource property twice.


I am not good at DataGridView (where is @Plutonix when I need him) so this could need some tweaking.



Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim cn As New SqlConnection(constr)
'Get the real column names from your database for StockId, ItemCode and Item
adapter = New SqlDataAdapter("Select StockId, ItemCode, Item From stockdata Where itemcode = @itemcode;", cn)
adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
'DataGridView1.DataSource = dt
bs.DataSource = dt
DataGridView1.DataSource = bs
End Sub





share|improve this answer
























  • your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

    – pankaj babbar
    Nov 21 '18 at 18:56











  • @pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

    – Mary
    Nov 21 '18 at 19:01











  • ok thanks for helping me.

    – pankaj babbar
    Nov 21 '18 at 19:04











  • Why the downvote? I like to know where my errors are.

    – Mary
    Nov 21 '18 at 22:45











  • Oh sorry i have upvoted. It is by mistake

    – pankaj babbar
    Nov 22 '18 at 4:14














0












0








0








  1. The constructor of a DataAdapter takes a string and a connection as arguments. `New SqlDataAdapter(string, SqlConnection). You have passed in a string (constr) not a connection.

  2. Only retrieve the data you need, not all the fields with "*".

  3. Set up your DataGridView before you bind it.

  4. Not sure what itemcode is doing in a customerdata table. Seems like a database design issue.

  5. It seems you are setting the DataSource property twice.


I am not good at DataGridView (where is @Plutonix when I need him) so this could need some tweaking.



Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim cn As New SqlConnection(constr)
'Get the real column names from your database for StockId, ItemCode and Item
adapter = New SqlDataAdapter("Select StockId, ItemCode, Item From stockdata Where itemcode = @itemcode;", cn)
adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
'DataGridView1.DataSource = dt
bs.DataSource = dt
DataGridView1.DataSource = bs
End Sub





share|improve this answer














  1. The constructor of a DataAdapter takes a string and a connection as arguments. `New SqlDataAdapter(string, SqlConnection). You have passed in a string (constr) not a connection.

  2. Only retrieve the data you need, not all the fields with "*".

  3. Set up your DataGridView before you bind it.

  4. Not sure what itemcode is doing in a customerdata table. Seems like a database design issue.

  5. It seems you are setting the DataSource property twice.


I am not good at DataGridView (where is @Plutonix when I need him) so this could need some tweaking.



Private Const constr As String = "server= PANKAJSQLEXPRESS; database = pankaj billing software; integrated security=true"
Private bs As BindingSource = New BindingSource
Private dt As DataTable = New DataTable
Dim adapter As SqlDataAdapter
Private cb As SqlCommandBuilder
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim cn As New SqlConnection(constr)
'Get the real column names from your database for StockId, ItemCode and Item
adapter = New SqlDataAdapter("Select StockId, ItemCode, Item From stockdata Where itemcode = @itemcode;", cn)
adapter.SelectCommand.Parameters.AddWithValue("@itemcode", TextBox2.Text)
cb = New SqlCommandBuilder(adapter)
adapter.Fill(dt)
'Set AutoGenerateColumns False
DataGridView1.AutoGenerateColumns = False

'Set Columns Count
DataGridView1.ColumnCount = 3

'Add Columns
DataGridView1.Columns(0).Name = "stockId"
DataGridView1.Columns(0).HeaderText = "Stock Id"
DataGridView1.Columns(0).DataPropertyName = "stockID"

DataGridView1.Columns(1).Name = "itemcode"
DataGridView1.Columns(1).HeaderText = "Item Code"
DataGridView1.Columns(1).DataPropertyName = "itemcode"

DataGridView1.Columns(2).Name = "item"
DataGridView1.Columns(2).HeaderText = "Item"
DataGridView1.Columns(2).DataPropertyName = "item"
'DataGridView1.DataSource = dt
bs.DataSource = dt
DataGridView1.DataSource = bs
End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 18:45









MaryMary

3,1622718




3,1622718













  • your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

    – pankaj babbar
    Nov 21 '18 at 18:56











  • @pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

    – Mary
    Nov 21 '18 at 19:01











  • ok thanks for helping me.

    – pankaj babbar
    Nov 21 '18 at 19:04











  • Why the downvote? I like to know where my errors are.

    – Mary
    Nov 21 '18 at 22:45











  • Oh sorry i have upvoted. It is by mistake

    – pankaj babbar
    Nov 22 '18 at 4:14



















  • your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

    – pankaj babbar
    Nov 21 '18 at 18:56











  • @pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

    – Mary
    Nov 21 '18 at 19:01











  • ok thanks for helping me.

    – pankaj babbar
    Nov 21 '18 at 19:04











  • Why the downvote? I like to know where my errors are.

    – Mary
    Nov 21 '18 at 22:45











  • Oh sorry i have upvoted. It is by mistake

    – pankaj babbar
    Nov 22 '18 at 4:14

















your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

– pankaj babbar
Nov 21 '18 at 18:56





your code is working fine. if i want to add some columns more which are not in sql database but on form.vb then what should i do? i have two textboxes and i want to add them both after these 3 columns from sql database.

– pankaj babbar
Nov 21 '18 at 18:56













@pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

– Mary
Nov 21 '18 at 19:01





@pankajbabbar I am sorry, I am not sure. I would suggest a new question on adding columns to a bound DataGridView. Show what you have tried and how it doesn't work.

– Mary
Nov 21 '18 at 19:01













ok thanks for helping me.

– pankaj babbar
Nov 21 '18 at 19:04





ok thanks for helping me.

– pankaj babbar
Nov 21 '18 at 19:04













Why the downvote? I like to know where my errors are.

– Mary
Nov 21 '18 at 22:45





Why the downvote? I like to know where my errors are.

– Mary
Nov 21 '18 at 22:45













Oh sorry i have upvoted. It is by mistake

– pankaj babbar
Nov 22 '18 at 4:14





Oh sorry i have upvoted. It is by mistake

– pankaj babbar
Nov 22 '18 at 4:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418035%2fhow-to-bind-specific-sql-colums-in-datagrid-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin