1.11M Members

How to fill DataGridView data with Data Table?

 
0
 

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?:-/

 
0
 

Try to show some of your code...

 
0
 
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
 
0
 

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?

 
0
 

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

 
0
 

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
myconn.open()
Dim myDataAdapter as new SqlDataAdapter("your query ", mycon) 
myDataTable.table("MyTable").clear
MyDataAdapter.fill(myDataTable,"MyTable")
BindingSource1.datasource = myDataTable.table("MyTable")
GataGridView.DataSource = bindingsource1
EndSub

Use this sub .

Hope this will helps u

Regards
M.Waqas Aslam

 
0
 

thanks :)

Question Answered as of 2 Years Ago by Netcode, M.Waqas Aslam and aerohn
 
0
 

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=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));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
    conn.Open()
    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
        department.Items.Add(dr.Item("DNAME").ToString)
        ' + " --> " + dr.Item("dname").ToString)
    End While

End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    MsgBox("hello")
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

 
0
 
da = New OracleDataAdapter(str, conn)
 dt = New DataTable()
 da.Fill(dt)
 DataGridView1.DataSource = dt
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article