364,112 Members | 2289 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

how to assign array values to datagridview

bharathi228
P: 28
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
Share this Question
Share on Google+
3 Replies


mldisibio
Expert 100+
P: 174
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

vinidimple
P: 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

DrLuiji
P: 1
The above code works fine, it only need the catch after the DataGridView Refresh.
Many Thanks.
Dr.Luiji
Feb 10 '09 #4

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET