Hi,
I get some data from Excel as a data array using the automation. I like to show those data in vb forms by using the datagrid.Shall I do that? If so , how? I need the coding..urgent!!!
Thanks
:)
Printable View
Hi,
I get some data from Excel as a data array using the automation. I like to show those data in vb forms by using the datagrid.Shall I do that? If so , how? I need the coding..urgent!!!
Thanks
:)
Hi
The DataGrid like its name suggests is designed to display bound data only. But you use it to display unbound manually.
the following code assume you have a datagrid on a form called DataGrid1.
On the form class declaration put this code
VB Code:
Private m_DataTable As New DataTable("MyTableName") Private Sub InitData() 'Set up the DataTable m_DataTable = New DataTable("TableName") m_DataTable.Columns.Add(New System.Data.DataColumn("Column0")) m_DataTable.Columns.Add(New System.Data.DataColumn("Column1")) m_DataTable.Columns.Add(New System.Data.DataColumn("Column2")) 'Set up the datagrid control DataGrid1.DataSource = m_DataTable End Sub Private Sub AddRow(ByVal sCol0 As String, ByVal sCol1 As String, ByVal sCol2 As String) Dim NewRow As System.Data.DataRow = m_DataTable.NewRow() NewRow(0) = sCol0 NewRow(1) = sCol1 NewRow(2) = sCol2 m_DataTable.Rows.Add(NewRow) End Sub
on the form_load event put this :
VB Code:
InitData() AddRow("1", "2", "3") AddRow("a", "b", "c") AddRow("cat", "dog", "mouse")
You can modify InitData and AddRow to include as many columns as you wish...
Regards
Jorge
Hi Jorge,
Thanks for your reply. Its very useful for me. One more thing, can I validate the user's input in datagrid???
regrads,
haihems.