how to assign array values to datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bharathi228
    New Member
    • Jul 2008
    • 28

    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).DIG ITAL_INPUTS - 1
    Dim intarray(mblnDI GITAL_INPUTS.Le ngth) As Integer
    Array.Copy(mbln DIGITAL_INPUTS, intarray, mblnDIGITAL_INP UTS.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
    Last edited by Frinavale; Feb 11 '09, 03:49 PM. Reason: Moved to VB.NET Answers from .NET
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    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 ColumnCollectio n. 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

    Comment

    • vinidimple
      New Member
      • Sep 2006
      • 2

      #3
      Code:
      EventLog eventlog = new EventLog();
              string strWhichMachine = System.Net.Dns.GetHostName();
              Hashtable ht;
              DataSet ds = new DataSet();
              DataTable dt = new DataTable();
              DataRow drow;
      private void filllog()
              {
                  DataColumn fIndex = new DataColumn();
                  dt.Columns.Add(fIndex);
                  DataColumn fMessage = new DataColumn();
                  dt.Columns.Add(fMessage);
                  DataColumn fSource = new DataColumn();
                  dt.Columns.Add(fSource);
                  
                  try
                  {
                      eventlog.MachineName = strWhichMachine;
                     eventlog.Log = lbEventLogs.Items[lbEventLogs.SelectedIndex].ToString();
                     for (int i = 0; i < eventlog.Entries.Count; i++)
                     {
                        // dgEventViewer[0, i].Value = eventlog.Entries[i];
      
                         drow = dt.NewRow();
      
                         fIndex.DataType = System.Type.GetType("System.String");
                         fIndex.ColumnName = "Index";
      
                         fMessage.DataType = System.Type.GetType("System.String");
                         fMessage.ColumnName = "Message";
      
                         fMessage.DataType = System.Type.GetType("System.String");
                         fMessage.ColumnName = "Source";
      
                         drow[0] = eventlog.Entries[i].Index;
                         drow[1] = eventlog.Entries[i].Message;
                         drow[2] = eventlog.Entries[i].Source;
                         dt.Rows.Add(drow);
      
                     }
                      dgEventViewer.DataSource=dt;                
                      dgEventViewer.Refresh();
                      
                  }
       protected void getlogfiles()
              {
                  foreach (EventLog objEventLog in EventLog.GetEventLogs(strWhichMachine))
                  {
                      lbEventLogs.Items.Add(new ListBoxItem(objEventLog.LogDisplayName, objEventLog.Log));
                      lbEventLogs.SelectedIndex = 0;
                  }
              }
      Last edited by Frinavale; Feb 11 '09, 03:47 PM. Reason: added [code] tags

      Comment

      • DrLuiji
        New Member
        • Feb 2009
        • 1

        #4
        Thanks

        The above code works fine, it only need the catch after the DataGridView Refresh.
        Many Thanks.
        Dr.Luiji

        Comment

        Working...