Pulling value from datagrid

B

Bill Blancett

Pardon me, I am new to the .NET environment but I am stuck with a problem
here. Hopefully there is a simple solution. I have a datagrid control with 9
fields that it pulls from the database. It pulls these fields correctly. But
I added a 10th field, a template column to the datagrid. Inside this
template column is a web form control that is a textbox. The idea is to pull
the records from the database and allow the user to update mutliple rows of
a field using a textbox. The problem is that I do not know how to retrieve
the value from the textbox in the datagrid. The plan was to loop through
each row in the datagrid and grab the value from the texbox field and update
the database using a SQL UPDATE statement. But I can't do that since I don't
know the command or syntax to grab the value from that last cell, textbox.
Does anyone know how to get this ?

Here is what the datagrid looks like in the HTML view:

<asp:datagrid id="dgEmp" style="Z-INDEX: 101; LEFT: 432px;POSITION:
absolute; TOP: 200px" runat="server"AutoGenerateColumns="False">

<Columns>

<asp:BoundColumn DataField="EmployeeID"
HeaderText="EmployeeID"></asp:BoundColumn>

<asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>

<asp:BoundColumn DataField="FirstName"
HeaderText="FirstName"></asp:BoundColumn>

<asp:BoundColumn DataField="Title"
HeaderText="Title"></asp:BoundColumn>

<asp:BoundColumn DataField="Address"
HeaderText="Address"></asp:BoundColumn>

<asp:BoundColumn DataField="City"
HeaderText="City"></asp:BoundColumn>

<asp:BoundColumn DataField="Region"
HeaderText="Region"></asp:BoundColumn>

<asp:BoundColumn DataField="PostalCode"
HeaderText="PostalCode"></asp:BoundColumn>

<asp:BoundColumn DataField="HomePhone"
HeaderText="HomePhone"></asp:BoundColumn>

------------------------MY TEMPLATE COLUMN
BELOW---------------------------------------------------------------

<asp:TemplateColumn HeaderText="Notes">

<ItemTemplate>

<asp:textbox id="txtNotes" runat="server"></asp:textbox>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:datagrid>

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

Now when I go to code view underneath the cmdUpdate_Click action of my
update button I have this:

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUpdate.Click

Dim rowCount As Integer = 0

Dim strSQL As String

Dim intID As Integer

'Loop through each row in the datagrid and update values in the database
with values from the textbox field.

Dim dgEmpItem As DataGridItem

Dim cmd As New SqlCommand

cmd.Connection = cn

For Each dgEmpItem In dgEmp.Items

-------------------BELOW IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE----------------

Dim myTextbox As TextBox =
CType(dgEmpItem.Cells(9).Controls(1),TextBox) ------------COULD YOU GIVE
ME THE CORRECT WAY TO PULL THE VALUE

-------------------ABOVE IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE-----------------

Dim myTextString As String = Str(myTextbox.Text)

rowCount += 1

intID = CInt(dgEmp.DataKeys(dgEmpItem.ItemIndex).ToString())

strSQL = "UPDATE Employees SET Notes = '" & myTextString & "' WHERE

EmployeeID =" & intID

cmd.CommandText = strSQL

cmd.ExecuteNonQuery()

Next


End Sub
 
S

Scott Fant

Try this...

Dim MyVal as String = CType(dgEmpItem.Item(0, 9), String)

I haven't actually tried this, but you can use the "Item" property/method to
get the object in the specified cell (row, column). If you actually need to
get at the text box used for editing then you'll probably have to create a
DataGridTextBoxColum class to use as the last column so you can access the
TextBox. This way you can subclass the TextBox and get at its events...
 
B

Bill Blancett

Thanks Scott for your reply. I believe your solution is correct. But looking
further into my problem I find that my problem is that I cannot pull any
values from my datagrid. Even when using the method that you suggested. I
actually have seen your method suggested other places as well. I am able to
pull regular textbox values and label values, but nothing from the datagrid.
Do you have any idea why this is happening? Is there some property that I am
forgetting to set or what ?
 
S

Scott

I don't believe I've seen this problem before, but I've only used the
datagrid in standard VB.NET applications. I haven't really done any
development work in ASP.NET so this may be an ASP issue. Here are some links
to information on the datagrid and some other newsgroups you many want to
check in...

http://www.syncfusion.com/FAQ/WinForms/default.asp#44
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

microsoft.public.dotnet.framework.aspnet
microsoft.public.dotnet.framework.aspnet.datagridcontrol
 
B

Bill Blancett

For future reference I figured out what the problem was. Apparently in the
page_load event of my application I called a function called loadgrid() that
I created. This basically loads the data from the database and binds it to
the grid. Well I didn't have a If Not IsPostBack condition in this event and
so my page was loading a new datagrid everytime. So this is what made the
datagrid function properly:

Private Sub Page_Load()
If Not IsPostBack Then
Call LoadGrid()
End if

Thanks anyway for your reply Scott, I appreciate it.

Scott said:
I don't believe I've seen this problem before, but I've only used the
datagrid in standard VB.NET applications. I haven't really done any
development work in ASP.NET so this may be an ASP issue. Here are some links
to information on the datagrid and some other newsgroups you many want to
check in...

http://www.syncfusion.com/FAQ/WinForms/default.asp#44
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

microsoft.public.dotnet.framework.aspnet
microsoft.public.dotnet.framework.aspnet.datagridcontrol


Bill Blancett said:
Thanks Scott for your reply. I believe your solution is correct. But looking
further into my problem I find that my problem is that I cannot pull any
values from my datagrid. Even when using the method that you suggested. I
actually have seen your method suggested other places as well. I am able to
pull regular textbox values and label values, but nothing from the datagrid.
Do you have any idea why this is happening? Is there some property that
I
am
forgetting to set or what ?


property/method need
to
create
a
DataGridTextBoxColum class to use as the last column so you can access the
TextBox. This way you can subclass the TextBox and get at its events...


Pardon me, I am new to the .NET environment but I am stuck with a problem
here. Hopefully there is a simple solution. I have a datagrid
control
with
9
fields that it pulls from the database. It pulls these fields correctly.
But
I added a 10th field, a template column to the datagrid. Inside this
template column is a web form control that is a textbox. The idea is to
pull
the records from the database and allow the user to update mutliple rows
of
a field using a textbox. The problem is that I do not know how to retrieve
the value from the textbox in the datagrid. The plan was to loop through
each row in the datagrid and grab the value from the texbox field and
update
the database using a SQL UPDATE statement. But I can't do that since I
don't
know the command or syntax to grab the value from that last cell, textbox.
Does anyone know how to get this ?

Here is what the datagrid looks like in the HTML view:

<asp:datagrid id="dgEmp" style="Z-INDEX: 101; LEFT: 432px;POSITION:
absolute; TOP: 200px" runat="server"AutoGenerateColumns="False">

<Columns>

<asp:BoundColumn DataField="EmployeeID"
HeaderText="EmployeeID"></asp:BoundColumn>

<asp:BoundColumn DataField="LastName"
HeaderText="LastName"></asp:BoundColumn>

<asp:BoundColumn DataField="FirstName"
HeaderText="FirstName"></asp:BoundColumn>

<asp:BoundColumn DataField="Title"
HeaderText="Title"></asp:BoundColumn>

<asp:BoundColumn DataField="Address"
HeaderText="Address"></asp:BoundColumn>

<asp:BoundColumn DataField="City"
HeaderText="City"></asp:BoundColumn>

<asp:BoundColumn DataField="Region"
HeaderText="Region"></asp:BoundColumn>

<asp:BoundColumn DataField="PostalCode"
HeaderText="PostalCode"></asp:BoundColumn>

<asp:BoundColumn DataField="HomePhone"
HeaderText="HomePhone"></asp:BoundColumn>

------------------------MY TEMPLATE COLUMN
BELOW---------------------------------------------------------------

<asp:TemplateColumn HeaderText="Notes">

<ItemTemplate>

<asp:textbox id="txtNotes"
--------------------------------------------------------------------------
--
------------------------------------------------------------

Now when I go to code view underneath the cmdUpdate_Click action of my
update button I have this:

Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdUpdate.Click

Dim rowCount As Integer = 0

Dim strSQL As String

Dim intID As Integer

'Loop through each row in the datagrid and update values in the
database
with values from the textbox field.

Dim dgEmpItem As DataGridItem

Dim cmd As New SqlCommand

cmd.Connection = cn

For Each dgEmpItem In dgEmp.Items

-------------------BELOW IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE----------------

Dim myTextbox As TextBox =
CType(dgEmpItem.Cells(9).Controls(1),TextBox) ------------COULD YOU
GIVE
ME THE CORRECT WAY TO PULL THE VALUE

-------------------ABOVE IS HOW I AM CURRENTLY TRYING TO PULL THE
VALUE-----------------

Dim myTextString As String = Str(myTextbox.Text)

rowCount += 1

intID = CInt(dgEmp.DataKeys(dgEmpItem.ItemIndex).ToString())

strSQL = "UPDATE Employees SET Notes = '" & myTextString & "'
WHERE

EmployeeID =" & intID

cmd.CommandText = strSQL

cmd.ExecuteNonQuery()

Next


End Sub

-------------------------------------------------------------------------- suggestions
will
be appreciated.
 

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