Values inside a DataGrid

D

Derek

Another DGrid question...

I can not access the values of a cell in a datagrid unless
I make them visible. I do not want these fields
displayed. I only want to use the values to run a stored
procedure off of. I access them right now using the
item.cell().control method.


Here's the code:

Dim intTrxid As Integer
Dim rda As RadioButtonList
Dim vrda As String
Dim txtrem As TextBox
Dim remarks As String
Dim icn As Integer
Dim newicn As Integer
Dim datcurrent As Date
Dim FY As String
Dim newyear As Date
Dim strnewicn As String

Dim txtimptype As TextBox
Dim imptype As String
Dim txtuns As TextBox
Dim uns As String
Dim GPCcontrolno As String
Dim imp As String


'get value of unitid to find icn#
Dim txtunit As TextBox
Dim strUnit As String
Dim intunit As Integer
txtunit = e.Item.Cells(8).Controls(0)
strunit = txtunit.Text
lblUnitid.Text = strUnit
intunit = CType(strUnit, Integer)
'intUnit = 1
SqlSelectCommand5.Parameters("@unitid").Value =
intUnit
SqlDataAdapter5.Fill(DsICN1)
lblControlNo.DataBind()

datcurrent = Date.Now.ToString("d")
newyear = DateAdd(DateInterval.Year, 1, datcurrent)
FY = DatePart(DateInterval.Year, newyear)

icn = CType(lblControlNo.Text, Integer)
newicn = icn + 1
strnewicn = CStr(newicn)
lblnewicn.Text = newicn
intTrxid = dgApproval.DataKeys(e.Item.ItemIndex)
rda = e.Item.FindControl("rdApproval")
vrda = rda.SelectedItem.Value
txtrem = e.Item.FindControl("txtccmremarks")
remarks = txtrem.Text

txtimptype = e.Item.Cells(9).Controls(0)
imptype = txtimptype.Text
txtuns = e.Item.Cells(10).Controls(0)
uns = txtuns.Text
imp = Left(txtimptype.Text, 1)



GPCcontrolno = uns & FY & imp & strnewicn
If vrda = "A" Then
SqlUpdateCommand2.Parameters("@icn").Value =
GPCcontrolno
SqlUpdateCommand2.Parameters("@ta").Value = 1
SqlUpdateCommand2.Parameters("@td").Value = 0
Else
If vrda = "D" Then
SqlUpdateCommand2.Parameters("@icn").Value
= "0"
SqlUpdateCommand2.Parameters("@ta").Value
= 0
SqlUpdateCommand2.Parameters("@td").Value
= 1
End If
End If
SqlUpdateCommand2.Parameters("@remarks").Value =
remarks
SqlUpdateCommand2.Parameters("@tid").Value =
intTrxid
SqlConnection1.Open()
SqlUpdateCommand2.ExecuteNonQuery()

dgApproval.EditItemIndex = -1
BindApproval()
dgdetails.Visible = False

SqlInsertCommand1.Parameters("@unitid").Value =
intunit
SqlInsertCommand1.Parameters("@FY").Value = FY
SqlInsertCommand1.Parameters("@newicn").Value =
newicn
SqlInsertCommand1.ExecuteNonQuery()
SqlConnection1.Close()


End Sub



Thanks in advance...

Derek M
 
T

Tian Min Huang

Hello Derek,

Thanks for your post. As I understand, you are using DataGrid in a ASP .NET
Web form. The problem you are facing is that you cannot access the value of
a cell when it is invisible in datagrid. Please correct me is there is any
misunderstanding. Now I'd like to share the following information with you:

In which event are you trying to access the item.cell().control?
If it is in the edit event and you have set the corresponding column as
visible=false, you may not get any values because there is no corresponding
textbox shown in the browser since the column is hidden. When a column is
hidden, it's values are not written to the table that displays the DataGrid
in the browser. That is why if you try to access a corresponding control
from the posted data, you will not get anything back. Based on my
experience, there are two methods to work around this problem:

Method 1
========
As you know, the values for the hidden column are stored in the viewstate
and they will be available during the postback. To work around this
problem, you can access them by the following method:

DataGrid.Items().Cells().Text

Method 2
========
You can persist the dataset that was bound to the dataGrid and access the
values for your hidden column from it.

Please feel free to let me know if you have any probles or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top