473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

displaying Array data in a Data Grid in VB.net

18 New Member
I'm trying to get an array to display in a data grid form in vb.net

The array has been imported from a csv, such that every array item is [date,time,flag] but I can split or join as needed to change the format of the array.

I want date, time, and flag to be seperate columns and each array item to be a new row.

I don't know where to start with this problem. I have looked through the msdn website and done some googling to no avail. If someone can point me to some better resources than I've been finding, it would be much appreciated.

Cheers
Mar 12 '08 #1
16 12299
saran23
28 New Member
I'm trying to get an array to display in a data grid form in vb.net

The array has been imported from a csv, such that every array item is [date,time,flag] but I can split or join as needed to change the format of the array.

I want date, time, and flag to be seperate columns and each array item to be a new row.

I don't know where to start with this problem. I have looked through the msdn website and done some googling to no avail. If someone can point me to some better resources than I've been finding, it would be much appreciated.

Cheers
Hi,
I have an idea to implement , Just try Im not sure this might be a correct solution,
here goes..
1.First create a datatable with rows and columns based on the array structure, 2.using a foreach loop, write the array items to this datatable,
3. Then bind this datatable to the gridview.

After trying this Let me know It works or not....

Thanks
Saran
Mar 12 '08 #2
crusson
18 New Member
Thanks.

My program is a notetaking program for surveying, so I'm trying to make it super robust. The array that is holding the data, reopens the text file each time it's going to do soemthing to it and restores it after every change it makes. Towards the robust attempt, I'm trying to avoid using a database for storage altogether. Perhaps if it's just an intermediary step, It's not such a big deal, but if I could go straight from an array into the data base, It would definitely save a step.
Mar 12 '08 #3
Plater
7,872 Recognized Expert Expert
DataTable is an object in memory, no need for a database.
Mar 12 '08 #4
crusson
18 New Member
DataTable was the keyword. Thanks a bunch.
Mar 12 '08 #5
crusson
18 New Member
So using a DataTable will work,

In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Thisarray()
  2.         Dim table1 As DataTable
  3.         table1 = New DataTable("stuff")
  4.         Try
  5.             Dim colA As DataColumn = New DataColumn("a")
  6.             colA.DataType = System.Type.GetType("System.String")
  7.             table1.Columns.Add(colA)
  8.             Dim colB As DataColumn = New DataColumn("b")
  9.             colB.DataType = System.Type.GetType("System.String")
  10.             table1.Columns.Add(colB)
  11.             Dim colC As DataColumn = New DataColumn("c")
  12.             colC.DataType = System.Type.GetType("System.String")
  13.             table1.Columns.Add(colC)
  14.  
  15.  
  16.             Dim j As Integer
  17.             For j = 1 To 10
  18.                 Dim row&i As DataRow
  19.                 row&i = table1.NewRow()
  20.                 row&i.Item("a") = "a"
  21.                 row&i.Item("b") = "b"
  22.                 row&i.Item("c") = "c"
  23.                 table1.Rows.Add(row&i)
  24.             Next
  25.  
  26.         Catch
  27.         End Try
  28.  
  29.         Dim ds As New DataSet()
  30.         ds = New DataSet()
  31.         ds.Tables.Add(table1)
  32.         DataGrid1.SetDataBinding(ds, "letters")
  33.  
  34.     End Sub
  35.  
Mar 12 '08 #6
saran23
28 New Member
So using a DataTable will work,

In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...

Private Sub Thisarray()
Dim table1 As DataTable
table1 = New DataTable("stuf f")
Try
Dim colA As DataColumn = New DataColumn("a")
colA.DataType = System.Type.Get Type("System.St ring")
table1.Columns. Add(colA)
Dim colB As DataColumn = New DataColumn("b")
colB.DataType = System.Type.Get Type("System.St ring")
table1.Columns. Add(colB)
Dim colC As DataColumn = New DataColumn("c")
colC.DataType = System.Type.Get Type("System.St ring")
table1.Columns. Add(colC)


Dim j As Integer
For j = 1 To 10
Dim row&i As DataRow
row&i = table1.NewRow()
row&i.Item("a") = "a"
row&i.Item("b") = "b"
row&i.Item("c") = "c"
table1.Rows.Add (row&i)
Next

Catch
End Try

Dim ds As New DataSet()
ds = New DataSet()
ds.Tables.Add(t able1)
DataGrid1.SetDa taBinding(ds, "letters")

End Sub
I suggest using the Datatable u created as the datasource for the grid.instead of using the dataset.

just try this

Expand|Select|Wrap|Line Numbers
  1. Datagrid.Datasource=Datatablename;
Mar 13 '08 #7
Plater
7,872 Recognized Expert Expert
So using a DataTable will work,

In order to create a table with an unknown number of rows, I need to loop to create Row objects. How do I create objects in a loop such that each has a different name. Or am I missing something and there is a better way to create a bunch of rows?

my idea was soemthing along the lines of this...but &i doesn't work as i wished it did...
When it comes to that sort of thing, you don't NEED different names for them, the variable name you assign goes out of scope the next itteration through the loop.

Simply doing this should be fine:
Expand|Select|Wrap|Line Numbers
  1.  Dim j As Integer
  2.             For j = 1 To 10
  3.                 Dim row As DataRow
  4.                 row = table1.NewRow()
  5.                 row.Item("a") = "a"
  6.                 row.Item("b") = "b"
  7.                 row.Item("c") = "c"
  8.                 table1.Rows.Add(row)
  9.             Next
  10.  
Mar 13 '08 #8
crusson
18 New Member
So I am now able to display the DataGrid with all of my data.

I want to set individual column widths, but code such as:
colB.width = 100 does not work, i think maybe because I'm running vb.net 1.0?

I can set a datagridcolumns tyle, but how do i apply this to a column?

Expand|Select|Wrap|Line Numbers
  1. Dim styleA As DataGridColumnStyle
  2.             styleA.Width() = 1200
  3.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub displayArray()
  2.  
  3.         arrayMasterLen = terrarray.getDisplayArrayLen()   
  4.  
  5.         Dim dataTable1 As DataTable
  6.         dataTable1 = New DataTable("LineLog")
  7.         Try
  8.             Dim colA As DataColumn = New DataColumn("Date/Time")
  9.             colA.DataType = System.Type.GetType("System.String")
  10.             dataTable1.Columns.Add(colA)
  11.             Dim colB As DataColumn = New DataColumn("Flag")
  12.             colB.DataType = System.Type.GetType("System.String")
  13.             dataTable1.Columns.Add(colB)
  14.             Dim colC As DataColumn = New DataColumn("Code")
  15.             colC.DataType = System.Type.GetType("System.String")
  16.             dataTable1.Columns.Add(colC)
  17.             Dim colD As DataColumn = New DataColumn("Comments")
  18.             colD.DataType = System.Type.GetType("System.String")
  19.             dataTable1.Columns.Add(colD)
  20.  
  21.             Dim j As Integer = 0
  22.             For j = 0 To arrayMasterLen - 1
  23.  
  24.                 Dim row As DataRow
  25.                 row = dataTable1.NewRow()
  26.                 row.Item("Date/Time") = terrarray.returnDateEntry(j)
  27.                 row.Item("Flag") = terrarray.returnFlag(j)
  28.                 row.Item("Code") = terrarray.returnCode(j)
  29.                 row.Item("Comments") = terrarray.returnComment(j)
  30.                 dataTable1.Rows.Add(row)
  31.             Next
  32.  
  33.         Catch
  34.         End Try
  35.  
  36.         DataGrid1.DataSource = dataTable1
  37.         DataGrid1.Refresh()
  38.  
  39.     End Sub
I also want to change the select method so that when I click anywhere in a row, the whole row becomes highlighted. Is this a simple property, or do I need to code this somehow?
Mar 13 '08 #9
Plater
7,872 Recognized Expert Expert
Your datagrid columns should have a .style or DefaultCellStyl e or something like that as a property already, you can change them withen there.
Mar 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1174
by: Mike | last post by:
I'm creating a asp.net application in C#. (i'm new to this process). All the examples i see that are displaying data are using the DataGridControl. Can anyone point me to some examples that is displaying data from a SQL database without using the DataGrid? Thx
7
269
by: C Newby | last post by:
I have a data grid control taht contains a data bound coulmn called "Type". Depending on the value of "Type" I want to display a corresponding image in the rendered table cell. So, if the value of type is 1, I want to show image1.gif. If the value is 2, I want to show image2.gif. Is there an easy way to do this? TIA//
1
2663
by: Anthony | last post by:
I am defining the datagridtable style for a grid .... Dim coursekeycol As New DataGridTextBoxColumn coursekeycol.MappingName = COURSEKEYCOLNAME coursekeycol.HeaderText = "" coursekeycol.Width = COURSEKEYCOLWIDTH .... I am wondering, how to I set the cells so that the user
14
4385
by: Susan Rice | last post by:
I want to create a readonly array of data, then a readonly array of a structure. This is data I access but I want it protected against accidental change. The following is my test code. #include "stdafx.h" struct LVC { unsigned short int lo; unsigned short int hi;
4
4525
by: daqmaneng | last post by:
does anyone have any suggestions for writing a two dimensional array of data to a file. Currently, I have an array of 5 columns, with 1000 elements per array. I know that I can use a for next loop to go through each data point in the array and use the streamwriter class for each individual data point and write each point individually to the...
15
8418
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array a={int,float,char,int*..............................}, so that a should return me int and a should return me
3
1491
by: makis | last post by:
hello, How can i send data from grid component to messagebox? which code or process we need?
0
991
blossam
by: blossam | last post by:
hi friends, i want to add data in grid at runtime means when i click enter or tab i want a new row in grid so i can add value in that i know how to do this in windows application but i want to do this in web application please help me if you know i dont want to create row by using TableRow =new TableRow() and add it to table
5
10038
by: billelev | last post by:
I have a large array of data (1000 x 40 x 3) that I am inserting into a database table. It is incredibly slow, and so I was wondering if there is a quicker way of inserting array data into a table. Here is the code I am currently using: ' ,, With rs For nSec = 0 To UBound(vtData, 2) For ndate = 0 To...
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.