How to bind datagridview

Posted by Oswaldlily under VB.NET on 4/7/2014 | Points: 10 | Views : 1406 | Status : [Member] | Replies : 3
dataset looks like

Name id
(blank) (blank)

Error: Still in gridview not showing this column name and empty row.Why gridview not getting bind??

Note:manually created datagridview as
Dim gridview As New DataGridView

gridview.DataSource = dataset.Tables(0)




Responses

Posted by: Allemahesh on: 4/7/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Well you code is right.

Just Add the below code.

gridview.DataSource = dataset.Tables(0) 

gridview.DataBind()


See the below example:-

<div>

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>


Dim table As New DataTable()

Dim column As New DataColumn()
column.DataType = System.Type.[GetType]("System.Int32")
column.ColumnName = "Sr. No."
column.AutoIncrement = True
column.AutoIncrementSeed = 1
column.AutoIncrementStep = 1
table.Columns.Add(column)

table.Columns.Add("Name", GetType(String))

Dim dr As DataRow = table.NewRow()
dr(1) = "dotnet"
table.Rows.Add(dr)

Dim gridview As New GridView()
gridview.DataSource = table
gridview.DataBind()

Panel1.Controls.Add(gridview)


Happy coding.

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 4/7/2014 [Member] Starter | Points: 25

Up
0
Down
@Allemahesh
In vb.net ,i m using datagridview..there is no databinding for this datagridview..

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 4/7/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Oswaldlily,

Ok got it. Here you need to add the columns dynamically.

See the below code:-

Private employees As New List(Of Employee)

Private tasks As New List(Of Task)

Initializes the data source and populates the DataGridView control.
Private Sub Form1_Load(ByVal sender As Object, _

ByVal e As EventArgs) Handles Me.Load

PopulateLists()
dataGridView1.AutoGenerateColumns = False
dataGridView1.DataSource = tasks
AddColumns()

End Sub

Populates the employees and tasks lists.
Private Sub PopulateLists()

employees.Add(New Employee("Harry"))
employees.Add(New Employee("Sally"))
employees.Add(New Employee("Roy"))
employees.Add(New Employee("Pris"))
tasks.Add(New Task(1, employees(1)))
tasks.Add(New Task(2))
tasks.Add(New Task(3, employees(2)))
tasks.Add(New Task(4))
End Sub


Configures columns for the DataGridView control.

Private Sub AddColumns()

Dim idColumn As New DataGridViewTextBoxColumn()
idColumn.Name = "Task"
idColumn.DataPropertyName = "Id"
idColumn.ReadOnly = True

Dim assignedToColumn As New DataGridViewComboBoxColumn()

' Populate the combo box drop-down list with Employee objects.
For Each e As Employee In employees
assignedToColumn.Items.Add(e)
Next

' Add "unassigned" to the drop-down list and display it for
' empty AssignedTo values or when the user presses CTRL+0.
assignedToColumn.Items.Add("unassigned")
assignedToColumn.DefaultCellStyle.NullValue = "unassigned"

assignedToColumn.Name = "Assigned To"
assignedToColumn.DataPropertyName = "AssignedTo"
assignedToColumn.AutoComplete = True
assignedToColumn.DisplayMember = "Name"
assignedToColumn.ValueMember = "Self"

' Add a button column.
Dim buttonColumn As New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Name = "Status Request"
buttonColumn.Text = "Request Status"
buttonColumn.UseColumnTextForButtonValue = True

dataGridView1.Columns.Add(idColumn)
dataGridView1.Columns.Add(assignedToColumn)
dataGridView1.Columns.Add(buttonColumn)

End Sub


Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response