views:

377

answers:

3

Hi All,

I'm working on a piece of code at the moment that uses an older style DataGrid to allow the user to enter information into the table. It already has an add and delete button. Currently the user enters information into 3 textboxes that are in the footer, and the other rows use labels to display the information.

Essentially what I am wanting to do is take the line that the user has clicked the edit button on, and move the text from there in to the footer (deleteing the row that it was displayed on) so the user can make changes and then click the add button again. At the moment I have tried using FindControls to find the textbox and setting the text that way but it doesnt like it. Any ideas?

A: 

How are you using FindControl? It always helps to post your code ;)

You should be able to execute a FindControl() on the footer row and get the textboxes with no problem.

GridViewRow row = GridView1.FooterRow;
TextBox txt1 = ((TextBox)row.FindControl,"TextBox1"));
womp
THanks for the reply. Unfortunately I can't find a way to directly access the row of the datagrid (I can't create a DataGridRow object), but I have found a solution which I will post below.
Ryan French
A: 

Essentially I changed what I had originally planned. Instead of moving the text from the row to the footer textboxes and deleting the row I instead used the EditCommandColumn to create a link button like so

<asp:EditCommandColumn ButtonType="LinkButton" ItemStyle-ForeColor="Blue" EditText='[edit]' UpdateText='[update]' CancelText='[cancel]'></asp:EditCommandColumn>

I also had to add EditItemTemplate to each column, with a textbox in each, and bind the data to the textboxes in the same way that it was bound to the labels in the ItemTemplates.

Then using the ItemCommand event handler I added some code behind to set the EditItemIndex to the row that was being edited.

Private Sub GoodsList_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgdGoods.ItemCommand

    Select Case e.CommandName


        Case "Edit"

            dgdGoods.EditItemIndex = e.Item.ItemIndex

        Case "Cancel"

            dgdGoods.EditItemIndex = -1

        Case "Update"

            dgdGoods.EditItemIndex = -1
            'Update details here

Then, at the end of this rebind the data to the datagrid. With the EditCommandColumn it will automatically change from displaying the Edit button to showing the Update and Cancel buttons on the line that is being edited.

Ryan French
A: 

A few things need to be clarified - are you referring to the older .NET "DataGrid" control or the newer "GridView" control. Also, is this a web or winforms app?

My suggestions -

Have you tried to programmatically add and set the TextBox controls by handling the GridView1_RowEditing event?

To remove the row that's been edited - remove it from the datasource and rebind the grid.

list.Remove(itemToRemove);
GridView1.DataSource = list;
GridView1.DataBind();

Then, take the data from itemToRemove and use it to programmatically create and set textboxes in the footer.

If the third column of your data grid is called "Name" and contains the name data, you would create and set the third column's footer row TextBox value like this -

GridView1.FooterRow.Cells[2].Controls.Add(new TextBox { ID = "tbName", Text = item.Name });
sujata