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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP 2.0 Editing GridView problem

Status
Not open for further replies.

joggi

Programmer
Nov 8, 2006
44
YU
I have gridview bound to datatable. There are 2 columns: ID and NAME.

When I click Edit button ID field is read only and NAME is textbox that I can edit.

Then on OnRowEditing Event I would like to have code like this:

datatable.Rows(0).Item("NAME")= gridview.rows.(e.rowindex).cells(1).text

but this doesn’t work.

1. How to catch new NAME value from the editing text box ?
2. Is it possible to access gridview field by name, not by index (in VB 6 I would wrote datagrid.Columns("NAME").value). How to do this in vb.net 2.0?


Thanks in advance
 
It doesn't work because the text is not in the cell. It's in the textbox, which is in the cell. (There's a hooole, there's a hole... ahem).

So first, you need to grab a reference to the textbox, and then you can access the .Text property of it.

I also suggest that you use a TemplateColumn (no hocus pocus), so assuming you setup a TemplateColumn that has an EditItemTemplate that looks more or less like this:
Code:
...
  <EditItemTemplate>
    <asp:TextBox id="txtName" runat="server" Text='<%# //Databinding expression here %>' />
  </EditItemTemplate>
...
Then you can access the value of the textbox like this:
Code:
'Grab the textbox & downcast it
TextBox txtName = CType(gridview.Rows(e.RowIndex).FindControl("txtName"), TextBox)
'Grab the text value
datatable.Rows(0)("NAME")= txtName.Text
The cast there is done in an unsafe manner. I'm not sure what the equivalent VB syntax is, but here's how you would safely cast in C# (maybe someone can post w/ equivalent VB):
Code:
//Grab the textbox & query for it's type
TextBox txtName = gridview.Rows(e.RowIndex).FindControl("txtName") as TextBox;
if(txtName != null)
{
  datatable.Rows(0)("NAME")= txtName.Text
}//if
else
{
  //Take some action because things are not as you expect
}//else


penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thank you, but I don't know what is wrong with my code, it doesn't work.


<asp:GridView ID="gridview" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="id" ReadOnly="True" />
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>


Dim tb As TextBox = New TextBox
tb = gridview.Rows(e.RowIndex).FindControl("txtName")
Dim name As String = tb.Text

datatable.Rows(0).Item("Name") = name

This code works but....

I have in field Name value "John" and I changed it to "Tom", but my string NAME has the value 'John'.

How to get the NEW VALUE of that textbox?
 
I also try with e.NewValues("txtName") but it doesn't work.
It returns nothing, I bound gridview to datatable, perhaps this is the reason.
I don't know how to solve this "simple" problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top