Results 1 to 10 of 10

Thread: need assistance writing to datagridview cells

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    need assistance writing to datagridview cells

    I am building a report app that will dump the results to the screen and to a file. Since it is a lot of data, I decided to go with the dataGridView because to me it just made sense (I do not have a lot of vb experience). The problem is I do not know how to manually write to this thing. If someone could give me an example of a few things with it, I can figure out the rest fairly easy. Apparently this is not done often as I cannot find a single reference in all my books on how to do this. Here is some information:

    1) the name of the datagrid is resultsGrid
    2) The grid has about 50 colums, all named
    3) Some of the column names are: orderNum, proNum, poSo
    4) I'd like to know if i could reference columns by name or by row/col location
    5) I can put info in a column at a time or a row at a time if that is possible
    6) I need to know how to make a new row once the information is in there.

    Thanks in advance

    EDIT: I forgot to mention, I am using vb 2005
    Last edited by rolltide0; Nov 11th, 2008 at 10:36 AM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: need assistance writing to datagridview cells

    DataGridView.Columns give you the column collection
    DataGridView.Rows give you the row collection
    To assign a value to any specific datagridviewcell when you know the cell's column and row index, you just do
    DataGridView.Item(columnIndex, rowIndex).Value = [your value here]
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    Re: need assistance writing to datagridview cells

    Sweet thanks for the info

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    Re: need assistance writing to datagridview cells

    Ok I ran into a couple of problems.

    1) as I was assigning values I got no error messages but the data did not show up in the datagrid

    2) when i came time to write to the next line, I got a subscript out of range error

    here is my code to write to the cells

    Code:
    resultsGrid.Item(0, loopCount).Value = orderArray(loopCount).orderNumber
                resultsGrid.Item(1, loopCount).Value = orderArray(loopCount).poSoNumber
                resultsGrid.Item(2, loopCount).Value = orderArray(loopCount).poSoNumber
                resultsGrid.Item(3, loopCount).Value = orderArray(loopCount).bol
                resultsGrid.Item(4, loopCount).Value = orderArray(loopCount).pickDate
                resultsGrid.Item(5, loopCount).Value = orderArray(loopCount).delDate
                resultsGrid.Item(6, loopCount).Value = orderArray(loopCount).schedTransitTime
                resultsGrid.Item(7, loopCount).Value = orderArray(loopCount).actTransitTIme
                resultsGrid.Item(8, loopCount).Value = orderArray(loopCount).transTimeDiff
                resultsGrid.Item(9, loopCount).Value = orderArray(loopCount).reasonForDiff
                resultsGrid.Item(10, loopCount).Value = orderArray(loopCount).miles
                resultsGrid.Item(11, loopCount).Value = orderArray(loopCount).fullTruck
                resultsGrid.Item(12, loopCount).Value = orderArray(loopCount).feetUsed
                resultsGrid.Item(13, loopCount).Value = orderArray(loopCount).shipname
                resultsGrid.Item(14, loopCount).Value = orderArray(loopCount).shipCity
                resultsGrid.Item(15, loopCount).Value = orderArray(loopCount).shipState
                resultsGrid.Item(16, loopCount).Value = orderArray(loopCount).shipZip
                resultsGrid.Item(17, loopCount).Value = orderArray(loopCount).consignee
                resultsGrid.Item(18, loopCount).Value = orderArray(loopCount).cCity
                resultsGrid.Item(19, loopCount).Value = orderArray(loopCount).cState
                resultsGrid.Item(20, loopCount).Value = orderArray(loopCount).cZip
                resultsGrid.Item(21, loopCount).Value = orderArray(loopCount).weight
                resultsGrid.Item(22, loopCount).Value = orderArray(loopCount).pieces
                resultsGrid.Item(23, loopCount).Value = orderArray(loopCount).freightCharge
                resultsGrid.Item(24, loopCount).Value = orderArray(loopCount).fuel
                resultsGrid.Item(25, loopCount).Value = orderArray(loopCount).acc1Type
                resultsGrid.Item(26, loopCount).Value = orderArray(loopCount).acc1Charge
                resultsGrid.Item(27, loopCount).Value = orderArray(loopCount).acc2Type
                resultsGrid.Item(28, loopCount).Value = orderArray(loopCount).acc2Charge
                resultsGrid.Item(29, loopCount).Value = orderArray(loopCount).acc3Type
                resultsGrid.Item(30, loopCount).Value = orderArray(loopCount).acc3Charge
                resultsGrid.Item(31, loopCount).Value = orderArray(loopCount).acc4Type
                resultsGrid.Item(32, loopCount).Value = orderArray(loopCount).acc4Charge
                resultsGrid.Item(33, loopCount).Value = orderArray(loopCount).acc5Type
                resultsGrid.Item(34, loopCount).Value = orderArray(loopCount).acc5Charge
                resultsGrid.Item(35, loopCount).Value = orderArray(loopCount).acc6Type
                resultsGrid.Item(36, loopCount).Value = orderArray(loopCount).acc6Charge
                resultsGrid.Item(37, loopCount).Value = orderArray(loopCount).totalAcc
                resultsGrid.Item(38, loopCount).Value = orderArray(loopCount).netRev

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: need assistance writing to datagridview cells

    Where do you set the value of loopCount? What the upper bound for it? what's the upperbound of orderArray? How many rows do you have in resultsGrid? Those are the questions you should ask yourself whenever you run into "index out of range" exception. And keep in mind that collections/arrays are zero based index, so for example, if you have an array with 50 elements, the index goes from 0 to 49 instead of 1 to 50.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    Re: need assistance writing to datagridview cells

    The initial resultsGrid is empty. I am trying to populate it a column at a time. There is only 1 row a data per loop cycle.

    loopCount is initiated just before the loop that this code is in. The loop does a lot.

    basically this app runs 5 queries. The first one gets most of the information, and the other 4 get the rest of the information based on the initial query.

    I hate having a lot of big loops as I see it a waste, so instead of gathering all the information in several loops then adding another one to display it, I created one loop to get the initial information, and one big loop to do the rest (the other queries are rather small compared to the first one).

    I tested the validity of the loop before adding the output statements to make sure all the count variables work and so on. I tend to do things in small steps.

    Since there will be only one row of data per loop cycle, I used the loopCount to save for making another variable.

    I do not get the subscript out of range error until the second pass. First pass generated no errors.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: need assistance writing to datagridview cells

    Wait a second... Are you saying that you're running some queries? Are they database queries? If they are, you can use databinding and it will take care the rest for you... I need more info here. What's the source of the data? How do you you get the data from that source? Post any relevant code too.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    Re: need assistance writing to datagridview cells

    I got it to work. I used a different technique. Someone suggested that i create an object with the exact size of the row i was writing too, add the data into the object and use the rows.add method to add an entire row at a time.

    However, I still want to know how to add data a column at a time and see the results, in addition to creating a different row. This info will be useful on projects in which I can't add entire rows at a time.


    I had started to go the way of databinding, but I did know know enough to use them. I also tend to stay away from databinding as I feel I have more control doing it manual (I am sure that will change as I get more comfortable with visual basic as I am a Java guy).

    The data comes form an oracle 8i database from which I only have read access too.

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: need assistance writing to datagridview cells

    There is nothing to be afraid of using data bindings. And since you said the data comes from an Oracle8i DB, I assume that you're using ADO.Net to get the data. If you're not, I strongly suggest that you do. It'll make your life a lot easier. Basically you create a DataSet or DataTable, create a connection object, and a DataAdapter object. You then use the DataAdapter to fill your dataset/datatable with the data that you query for. Once you have the dataset/datatable filled, you simply assign it to the DataGridView.DataSource property and databindings will take place. To modify the data in the grid, you modify the datasource of the grid, which is the datatable it is bound to. It's a lot simpler than what you may think it is.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    10

    Re: need assistance writing to datagridview cells

    I'll play around with it to get more familiar with it. After all, I do like things that make life easier.

    Thanks for all the suggestions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width