Converting each datagrid line into a string in vb

  • Thread starter Thread starter Eric Broers
  • Start date Start date
E

Eric Broers

LS,

Assume I have a datagrid, which is filled by an SQL statement (Select * from
Customer). The Customers table contains 4 columns and 10 rows.

Now, I want to fill 10 strings with the datagrid rows. So, sting1 should
become the first database row, string2 the 2nd, etc, etc

So, I tried this:

For i = 0 To MyDataGrid.Items.Count - 1
string1 = ????????????????????????
Next

How do I read the datagrid and put it into a string? I hope someone can help
me out with this one.

Many thanks!!

Eric
 
Eric Broers said:
Assume I have a datagrid, which is filled by an SQL statement (Select
* from Customer). The Customers table contains 4 columns and 10
rows.

Now, I want to fill 10 strings with the datagrid rows. So, sting1
should become the first database row, string2 the 2nd, etc, etc

So, I tried this:

For i = 0 To MyDataGrid.Items.Count - 1
string1 = ????????????????????????
Next

How do I read the datagrid and put it into a string? I hope someone
can help me out with this one.

Why not read from the bound datasource?
 
Hi Eric,

The Datagrid is something that mostly shows data. That is always from
something that is inheritted from the iList and mostly a datatable. (Maybe
using a dataview).

Dont try to grab the data from the datagrid, take it from the datatable.
Trough that you can easily itterate by (and I show you an example with a
dataset wich have one table)

\\\roughly written watch typoes
dim i, y as integer
dim mystring as string (ds.tables(0).rows.count - 1)
for i = 0 to ds.tables(0).rows.count - 1
mystring(i) = ds.tables(0).rows(i).item(0).tostring
for y = 1 to 3
mystring(i) = mystring & " " & ds.tables(0).rows(i).item(y).tostring
next y
next
////

I hope this helps?

Cor
 
Hi Cor,

ok, I get it. But I am obly able to fill a datagrid by the following
statements (see the bottom of this message).

So now my question is, how do I fill the dataset by calling a stored
procedure??

Many thanks!

Eric



shop.aspx:
Private Sub Page_Load(.....)
MyDataGrid.DataSource = cart.GetItems(cartId)
MyDataGrid.DataBind()
....


procs.vb:
Public Function GetItems(ByVal cartID As String) As SqlDataReader

' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New SqlCommand("ShoppingCartList",
myConnection)

' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure

' Add Parameters to SPROC
Dim parameterCartID As SqlParameter = New
SqlParameter("@CartID", SqlDbType.NVarChar, 50)
parameterCartID.Value = cartID
myCommand.Parameters.Add(parameterCartID)

' Execute the command
myConnection.Open()
Dim result As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

' Return the datareader result
Return result

End Function
 
Hi Eric,

I use for this a dataset instead of the datareader, now you have to make the
underlaying tables, while the dataset makes that direct.

You need for that also a sqlDataadapter that uses the command.

I hope this helps?

Cor
 
Back
Top