Results 1 to 5 of 5

Thread: creating an array from datagridview rows. possible?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    7

    creating an array from datagridview rows. possible?

    im new to programming and i really dont know how arrays work. so i have this code below:

    Dim dt As New DataTable

    Dim da As New SqlClient.SqlDataAdapter("SELECT FEE_NAME, convert(varchar(50), convert(money, FEE_AMOUNT), 1) FROM FEE_TABLE ", m_Connection)

    da.Fill(dt)

    DataGridView2.DataSource = dt



    now this will show in the datagridview as:

    __________________________
    FEE_NAME | FEE_AMOUNT |
    ------------------------------
    registration | 8,000.00 |
    ------------------------------
    books | 4,500.00 |
    ------------------------------
    uniform | 7,300.00 |
    ------------------------------
    library | 3,000.00 |
    ------------------------------

    is it possible to put all the rows in fee_name into an array when its already in the datagridview like when i click a button.

    so the array will show
    array1 = ["registration" , "uniform" , "books" , "library" ]

    i have read that i need to use a for each statement like:

    For Each row As DataGridViewRow In DataGridView1.Rows

    DONT KNOW WHAT TO PUT HERE

    Next

    because i need to add these rows in a different table in the database or create a view (in ms sql server 05). a sample code will really really help me. thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: creating an array from datagridview rows. possible?

    Yes, you can create an array but I don't see that there's any point. You've already got the data in a DataTable so if you want to access it just access it from there. This is merely an example to illustrate a principle but you can view every value from every field in a DataTable like this:
    vb.net Code:
    1. For Each row As DataRow In myDataTable.Rows
    2.     For Each column As DataColumn In myDataTable.Columns
    3.         MessageBox.Show(row(column.ColumnName).ToString(), column.ColumnName)
    4.     Next
    5. Next
    If you only want the values from the fist column then obviously you don't have to loop through all the columns. You just specify a column index of zero or else specify the name of the first column.

    If you really do want to create an array then you wouldn't use a For Each loop. You'd first of all create an array of the appropriate size. You'd then use a For loop to transfer the data from the source to the desitination. The For loop has a loop counter that you will use as an index into the source to get the data and an index into the destination to set the data.

    For arrays, I suggest that you start reading here. For ADO.NET, I suggest that you start by following the Data Access link in my signature. As for this question, I don't really see how an array is going to help you achieve your stated aim. Can you provide a more complete description of what you're trying to achieve?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    7

    Re: creating an array from datagridview rows. possible?

    actually, i joined two tables that will show in the datagridview. so thats two tables. and rows in the datagrid can be deleted. now, i have to save the data in the datagridview in a different table. the reason i have to save it is because it will be used for future references, and specially if there were rows deleted. so the next time the user will view it, the deleted rows will not be there. but i cant delete it permanently from the database, just in the view. i think if i create a view out of the rows and columns in the datagrid, it will be best? not sure. how do i do it thou. thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: creating an array from datagridview rows. possible?

    I think you're on the wrong track. A view doesn't contain data itself. It just provides a view of the data in one or more tables. You can't delete data from a view and expect it to still exist in the source table(s). In fact, I don't think you even can execute a DELETE statement against a view.

    Also, the fact that the data in your DataTable comes from multiple database tables is not relevant to whether you need to create an array or not. You can create one or more DataAdapters to save the changes from a single DataTable to one or more database tables. What really matters is the SQL code you write to do it.

    I'm afraid that I still don't really know what you're trying to achieve. If you want a more specific solution than I've already provided then you're going to have to provide a FULL and CLEAR description of what you're trying to achieve.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    7

    Re: creating an array from datagridview rows. possible?

    how bout this.

    i have a datagridview that was populated from the database(ms sql server 05) and this is what shows in the datagridview

    this came from FEE_TABLE

    FEE_PK....| FEE_NAME.. | FEE_AMOUNT
    --------------------------------------
    reg01......| Registration | 2,500.00
    acc02.....| Account......| 1,000.00
    mat03.....| Materials.....| 5,000.00
    ins04......| Insurance....| 4,500.00
    --------------------------------------

    is it possible to add the rows only under FEE_PK in a new table (TEMP_TABLE), so than if the user deletes a row (if the user wont pay for that certain fee), only the remaining rows will be saved in TEMP_TABLE when i click button1.

    so for example if from the table above i deleted data 'mat03', what will show in TEMP_TABLE is:

    TEMP_PK |FEE_PK....| STUDENT_ID
    ----------------------------------
    00001.....|reg01......| 2345678
    00002.....|acc02......| 2345678
    00004.....|ins04.......| 2345678
    ----------------------------------

    so, i need to add only the data from the FEE_PK to the new table

    i already have TEMP_TABLE (i have not thought of a proper table name yet) created in sql server 2005
    TEMP_PK is autonumber

    the purpose is so that when i wanna view in the future which the student only paid for, i can. and also, yes, in TEMP_TABLE, there will be a lot of student_IDs there, so ill just be using the WHERE clause when i wanna view the fees that certain student paid for.

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