Start New Discussion within our Software Development Community

I have a data table with query inside, then i need to sent value of data table exactly in datagridview i made before. Anyone can do this?:-/

Dim objdatatable As DataTable = objDataSet.Tables("TableName")

                      With DataGridView1
      		              .AutoGenerateColumns = True
                	      .DataSource = objDataSet
	                      .DataMember = "TableName"

                    ' Declare and set the alternating rows style...
                    Dim objAlternatingCellStyle As New DataGridViewCellStyle()
                    objAlternatingCellStyle.BackColor = Color.WhiteSmoke
                    searchDataGridView.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle

                    End With

Here my code

Imports SistemInformasiKepegawaian_SI.DbSIKDataSetTableAdapters
Public Class ucAbsensiTahunan

    Private Sub ucAbsensiTahunan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dta As New Tb_AbsensiTableAdapter
        Dim dttb As New DataTable
        dttb = dta.GetData() 'the data table had data from dataset
        DataGridView1.DataSource = dttb 'it doesn't work

    End Sub
End Class

How to fill datagridview with datatable?

Is there any other way? I don't really understand with that code (Sorry I'm newbie :( )
Could you explain that?

Hello nore !
you can do like this , just take a bindingsource and use this coding

Sub MyGridData()
dim mycon as new sqlConnection("your conn string")
dim myDataTable as new DataTable
Dim myDataAdapter as new SqlDataAdapter("your query ", mycon) 
BindingSource1.datasource = myDataTable.table("MyTable")
GataGridView.DataSource = bindingsource1

Use this sub .

Hope this will helps u

M.Waqas Aslam

Imports System.Data.Odbc
Imports System.IO
Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types

Public Class Form1

Dim conn As New OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=;User Id=SYSTEM;Password=admin;")
Dim cb As New OracleCommandBuilder
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sql As String = "select DNAME from DEPT"
    Dim cmd As New OracleCommand(sql, conn)
    cmd.CommandType = CommandType.Text
    Dim dr As OracleDataReader = cmd.ExecuteReader()

    While dr.Read
        ' + " --> " + dr.Item("dname").ToString)
    End While

End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
End Sub
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
    If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
        Dim selectedRow = DataGridView1.Rows(e.RowIndex)
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles department.SelectedIndexChanged
    Dim ds As New DataSet()
    Dim str As String
    Dim da As OracleDataAdapter
    str = "select e.ename,e.sal,e.deptno,d.dname,e.Job,d.loc from DEPT d,EMP e where d.dname = '" + department.SelectedItem.ToString + "' and d.deptno = e.deptno"
     da = New OracleDataAdapter(str, conn)
     da.Fill(ds, "DEPT")
    Dim dt As DataTable
    dt = ds.Tables("DEPT") 'creati
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.DataSource = dt
    DataGridView1.Columns(0).DataPropertyName = dt.Columns(0).ColumnName
    DataGridView1.Columns(1).DataPropertyName = dt.Columns(3).ColumnName
    DataGridView1.ReadOnly = True
    'DataGridView1.DataSource = ds.Tables("DEPT")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End Sub

End Class

da = New OracleDataAdapter(str, conn)
 dt = New DataTable()
 DataGridView1.DataSource = dt
This question has already been answered. Start a new discussion instead.