473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to assign array values to datagridview

hi,

how to assign a array as datasource to datagridview in vb.net.


my problem is

iam having an array with 60 values


For intVALUE_INDEX = 0 To (muiiobufCEMCON(intCEMCON).DIGITAL_INPUTS - 1
Dim intarray(mblnDIGITAL_INPUTS.Length) As Integer
Array.Copy(mblnDIGITAL_INPUTS, intarray, mblnDIGITAL_INPUTS.Length)

here in intarray iam having values


for displaying these values iam using a datagridview

here my requirement is iam displaying 16 values in each row

so i want to add 16 columns

here 60 values are there

so 60/16

4 rows

16 values -------- in 3 rows
another 12 values are ----------- in 4 th row

so how to dynamically creating rows and columns

so how it is possible

please any one give me reply

its very urgent

4 days iam wasting my time with this.but no solution


iam new to .net.please any one help me with code in vb.net.it is windows application
Oct 7 '08 #1
3 27870
mldisibio
190 Expert 100+
Your proposal is an incorrect use of a DataSource.

If the DataSource is a collection, then it represents a column in the DataGridView, and each item in the collection represents the row value for that column.

Since your array contains simple values, if you use it as a DataSource, it would represent one column. The DataGridView would display one columns with 60 int values.

If you require 16 columns you will have to manually add the columns to the Grid ColumnCollection. You will also have to manually add the rows to the RowCollection.

Then loop through your array and programatically set each cell value.

DataGridView[col, row].Value = myArray[i]

However, such a solution makes no use of the DataGridView or of data binding, or of any good programming practice.

To accomplish what you think DataBinding should do, you would create a class with 16 unique properties, and you would have an array of four instances of this object. Then when you bind to the array, it will create 16 columns, one for each property, and four rows, one for each object in the array. However, that is also a theoretical solution that makes no sense for what you are doing.

If you just want to display a list of integers spread out evenly, consider using a FlowLayoutPanel that wraps at 16 TextBoxes or Labels: How To Anchor and Dock Child Controls in a FlowLayoutPanel
Oct 7 '08 #2
Expand|Select|Wrap|Line Numbers
  1. EventLog eventlog = new EventLog();
  2.         string strWhichMachine = System.Net.Dns.GetHostName();
  3.         Hashtable ht;
  4.         DataSet ds = new DataSet();
  5.         DataTable dt = new DataTable();
  6.         DataRow drow;
  7. private void filllog()
  8.         {
  9.             DataColumn fIndex = new DataColumn();
  10.             dt.Columns.Add(fIndex);
  11.             DataColumn fMessage = new DataColumn();
  12.             dt.Columns.Add(fMessage);
  13.             DataColumn fSource = new DataColumn();
  14.             dt.Columns.Add(fSource);
  15.  
  16.             try
  17.             {
  18.                 eventlog.MachineName = strWhichMachine;
  19.                eventlog.Log = lbEventLogs.Items[lbEventLogs.SelectedIndex].ToString();
  20.                for (int i = 0; i < eventlog.Entries.Count; i++)
  21.                {
  22.                   // dgEventViewer[0, i].Value = eventlog.Entries[i];
  23.  
  24.                    drow = dt.NewRow();
  25.  
  26.                    fIndex.DataType = System.Type.GetType("System.String");
  27.                    fIndex.ColumnName = "Index";
  28.  
  29.                    fMessage.DataType = System.Type.GetType("System.String");
  30.                    fMessage.ColumnName = "Message";
  31.  
  32.                    fMessage.DataType = System.Type.GetType("System.String");
  33.                    fMessage.ColumnName = "Source";
  34.  
  35.                    drow[0] = eventlog.Entries[i].Index;
  36.                    drow[1] = eventlog.Entries[i].Message;
  37.                    drow[2] = eventlog.Entries[i].Source;
  38.                    dt.Rows.Add(drow);
  39.  
  40.                }
  41.                 dgEventViewer.DataSource=dt;                
  42.                 dgEventViewer.Refresh();
  43.  
  44.             }
  45.  protected void getlogfiles()
  46.         {
  47.             foreach (EventLog objEventLog in EventLog.GetEventLogs(strWhichMachine))
  48.             {
  49.                 lbEventLogs.Items.Add(new ListBoxItem(objEventLog.LogDisplayName, objEventLog.Log));
  50.                 lbEventLogs.SelectedIndex = 0;
  51.             }
  52.         }
Nov 27 '08 #3
The above code works fine, it only need the catch after the DataGridView Refresh.
Many Thanks.
Dr.Luiji
Feb 10 '09 #4

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
7
by: Chris Wertman | last post by:
I am so lost on this one. Dim Ary as Integer = New Integer(4) {0,1,2,3} FAILS with the error: Array initializer has 1 too few elements. So I try Dim Ary as Integer = New Integer(3)...
6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
9
by: active | last post by:
I need a control that displays a grid of rectangles. That is, like a brick wall except the rectangles are lined up. In VB6 I used such a control (may have been called FlexGrid but I'm not...
9
by: =?Utf-8?B?UGV0ZXJX?= | last post by:
I have a TabControl on a Windows form in which I have various tab pages each with a DataGridView, the first column of which is a DataGridViewCheckBoxColumn and subsequent columns being...
1
by: pbrown | last post by:
So I've got an array of string values which I'd like to display in an unbound DataGridView. What is the process necessary to simply load strings into cells of a datagrid? Ideally, we would go...
0
by: hydro123 | last post by:
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following: // initialize varaibles from...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.