Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Dataset Row Values 1

Status
Not open for further replies.

spidervenom

Programmer
Mar 8, 2003
19
0
0
US
Hi,

I have the following stored in a database and display on a datagrid

ID Name Subject Detail
1 test_name test This is a test message

This is a row that return as a dataset by a SELECT statement. I would like to extract specific values from this row and store it in a variable. Then pass those variable into another table in the database.

For example, I would like to store test_name in variable var_name, and test in var_subject

Can anyone tell me how to do that in VB.NET?

Thank you very much for the help!


 
First off, everyone should read this article, it is roughly 15 parts and it goes into great detail about the datagrid, and what you can program it to do. I have printed the entire article and use it as a reference at my desk.


You can read the row values one of two ways, either by iterating through the rows and performing some action on the data or when the dataset is bound to the grid by programming the Datagrid_itemdatabound event. I have included code for both of these methods.

First lets just look at the dataset and iterate throught the rows.
---------------------------------------------------------
Dim oDs As Data.DataSet
Dim ssql as String = "Select * from Pubs"
'get the data into the dataset
oDs = tpapp.GenericDataset(ssql)' this gets a dataset

If oDs.Tables(0).Rows.Count > 0 Then
Dim drDataRow As Data.DataRow

For Each drDataRow In oDs.Tables(0).Rows
' do something usefull with the data
Response.Write(drDataRow.ItemArray(0))
Response.Write(drDataRow.ItemArray(1))

Next

End If


---------------------------------------------------------
now lets look at how we can program the datagrid_itemdatbaound event

---------------------------------------------------------

Sub dgStoreSupport_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgStoreSupport.ItemDataBound

Select Case e.Item.ItemType
Case ListItemType.AlternatingItem, ListItemType.Item
'See if we have a new header
Dim lbltext As Label = CType(e.Item.Cells.Item(3).FindControl("lblPhNUmber"), Label)
If lbltext.Text.Equals("NewHead") Then
'Format the data, then align the text of the cells to the left
'remove the 2 right cells

e.Item.Cells.RemoveAt(3)
e.Item.Cells.RemoveAt(2)
'set the cell colspan=2
e.Item.Cells(1).ColumnSpan = 2
' turn the edit button off (i hope :) )
e.Item.Cells.Item(0).FindControl("Button1").Visible = False
'align the cell to the left, set the ext to bold and set the bgcolor
e.Item.Cells(1).Attributes.Add("align", "left")
e.Item.Cells(1).Font.Bold = True
e.Item.Cells(1).Font.Underline = True
e.Item.BackColor = Color.FromArgb(204, 204, 255)
End If

If Session(&quot;ISGroup&quot;) <> &quot;True&quot; Then 'User is not able to edit remove the controls
e.Item.Cells.Item(0).FindControl(&quot;Button1&quot;).Visible = False
End If

Case ListItemType.Footer
If Session(&quot;ISGroup&quot;) <> &quot;True&quot; Then
' remove all the the controls, lets try to first make the footer invisable
Dim x As Integer
For x = 0 To 4
e.Item.Cells(x).Visible = False
Next

End If


End Select
End Sub

---------------------------------------------------------

hope this helps :)


George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
Hi George,

Thanks for the help. I got it working just as I expected using rows iteration, the 2nd method is not bad too.
Also, thanks for the nice article about datagrid.
I agree that everyone should read it, it really helps a lot in understanding datagrid.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top