Datagrid Retrieve Edit TextBox String

cyclonebri

Regular
Joined
Jul 30, 2003
Messages
93
Location
Ames, IA, USA

Hey everyone. I was wondering if there is a quick and painless way of getting the data entered by the user in the textboxes that are generated in a datagrid when the edit button is clicked?

In other words, I have used the editor to create an edit/update/cancel button column, and have hardmapped all the other columns in the designer, without editing the HTML.

I therefore have no idea what the textboxes in the columns are named or what their ID is, so I can't do a find control on them. I can get the original values using this line:
Visual Basic:
m_myOldVals(0) = m_myDS.Tables(0).Rows(e.Item.ItemIndex).Item("AccountStatus")

now all I need to do is get the values as added by the user in the generated textboxes, and I can perform the actual update. Any help would be greatly appreciated.

In addition, I have also tried the following but it did not work:
Visual Basic:
m_myNewVals(0) = e.Item.Cells(0).Text

Thanks in advance for any help!

Brian
 
Try this...

dim txt as textbox = CType(e.Item.Cells(1).Controls(0), TextBox)
myNewSomething = CType(txt.Text, Integer)

or in a EditItemTemplate of a TemplateColumn

dim dd as dropdownlist = CType(e.Item.FindControl("myDropDown"), DropDownList)
myNEwSomwthing = CType(dd.SelectedItem.Value, Integer)
 
Robby said:
Try this...

dim txt as textbox = CType(e.Item.Cells(1).Controls(0), TextBox)
myNewSomething = CType(txt.Text, Integer)

or in a EditItemTemplate of a TemplateColumn

dim dd as dropdownlist = CType(e.Item.FindControl("myDropDown"), DropDownList)
myNEwSomwthing = CType(dd.SelectedItem.Value, Integer)

Robby,

Thank you very much! You rock! I implemented your code, but I was getting the old values so I had to use a session variable to stop the grid from reloading on postback, but once I got that working I was able to easily retrieve the values using your example. Thank you!
 
Instead of using Session, do this....

in your Page_Load event....
If not IsPostBack Then
LoadMyDataGridRoutine()
End If
 
Robby said:
Instead of using Session, do this....

in your Page_Load event....
If not IsPostBack Then
LoadMyDataGridRoutine()
End If

Yeah, I will look into that tomorrow when I get back to work, the thing is that I think I need it to rebind on postback for other events, like paging and sorting, which I have implemented, but I'll check it out just in case...

Thanks again for your help!

Brian
 
This is what I dont understand...i'm new to .net so bear with me...

1. In my page load, I call databind() routine and populate the DataGrid.

2. In my Update rotuine..when the changes are entered in the text boxes, I read the CURRENT values in the Datagrid and do my update.

questions
3. Where/when do I get the previous values of the DataGrid?? I dont see "tables()" as a member of datagrid

This is what I did to hold the value of the Datagrid before being update..I do NOT think this is a good idea..I do a select, populate datagrid, then open the connection again and read the values again in session vars..

Code:
 cmd.CommandText = "Select callerid,....., from..."

        cnn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)


        dgViewContactDetail.DataSource = dr
        dgViewContactDetail.DataBind()

         If Not IsPostBack Then [b]'this holds the orig value of datagrid[/b]
            cnn.Open()
            Dim dr1 As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If dr1.Read() Then
              Session("test") = dr1.GetString(5)
            End If
        End If

        dr.Close()
        cnn.Close()
 
eramgarden said:
This is what I dont understand...i'm new to .net so bear with me...

1. In my page load, I call databind() routine and populate the DataGrid.

2. In my Update rotuine..when the changes are entered in the text boxes, I read the CURRENT values in the Datagrid and do my update.

questions
3. Where/when do I get the previous values of the DataGrid?? I dont see "tables()" as a member of datagrid

This is what I did to hold the value of the Datagrid before being update..I do NOT think this is a good idea..I do a select, populate datagrid, then open the connection again and read the values again in session vars..

Code:
 cmd.CommandText = "Select callerid,....., from..."

        cnn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)


        dgViewContactDetail.DataSource = dr
        dgViewContactDetail.DataBind()

         If Not IsPostBack Then [b]'this holds the orig value of datagrid[/b]
            cnn.Open()
            Dim dr1 As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If dr1.Read() Then
              Session("test") = dr1.GetString(5)
            End If
        End If

        dr.Close()
        cnn.Close()

The tables() method is a member of the dataset object. If you have used a dataset to generate the values for the grid, you could get the values from the dataset.tables(0)...It seems to me, however, that you are using a datareader to generate the data for your datagrid. This means that you are taking a disconnected snapshot of the database, which is ok but it makes it more difficult to work with. One thing I noticed also, is that you said
Visual Basic:
If dr1.Read() Then
  Session("test") = dr1.GetString(5)
End If
From what I've seen, it's usually pretty conventional to do this instead
Visual Basic:
while dr1.read()
  'use if logic if you are reading in more than one row of records
  Session("test") = dr1.getString(5) 
wend

but, to make it possible for you to use the dataset logic, you could use your current command object and do something like this:
Visual Basic:
dim myDS as DataSet = new DataSet("WhateverYouWantHere")
dim myDA as DataAdapter = new DataAdapter
dim sTest as string = ""

'do all the same code to set up your connection and your command, and then:
try
  cnn.Open()
  myDA.SelectCommand = cmd
  myDA.fill(myDS)
  sTest = myDS.Tables(0).Rows(rownumber).Item("FieldName")
catch ex as exception
  'some error reporting
  System.Diagnostics.Trace.WriteLine(ex.ToString())
finally
  if not cnn.State = ConnectionState.Closed then
    cnn.close()
  endif
end try

Good luck!
 
Wow, yeah, got it working...

But i have to put this code in...

If Not IsPostBack Then

...

end if

otherwise, the orig value of datagrid will be replaced by the new value..right?

This is great!
 
eramgarden said:
Wow, yeah, got it working...

But i have to put this code in...

If Not IsPostBack Then

...

end if

otherwise, the orig value of datagrid will be replaced by the new value..right?

This is great!

If you don't have a condition for the postback and you do a databind on each page load, you will lose the new data in the textboxes before you can capture it, at least from my experience. Your new data will not show up in the fields of the grid until it is updated to the server and the dataset is refilled from your sql query. Other than that, sounds like you have it. Have a great weekend!
 
Back
Top